Import std.crypto.derive
std.crypto.derive
HMAC, HKDF, and PBKDF2: message authentication and key derivation.
Hmac, Hkdf, and Pbkdf2, over mbedTLS: a MAC, which a key makes and checks, a key from other key material, and a key from a password: HMAC and PBKDF2 by SHA-1, SHA-256, or SHA-512, and HKDF by SHA-256 or SHA-512.
std/crypto/derive.fb
Types
Hmac
struct
Public Struct Hmac
HMAC (RFC 2104) of data with key, of any length: a MAC as long as its hash, which only who has the key can make. Check one with ConstantTimeEquals.
Examples
Local message = "The quick brown fox jumps over the lazy dog"
Local mac = Hmac.Sha256("key".Bytes, message)
Expect(Hex.Encode(mac) == "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8")
// A MAC that came with a message is checked without telling where they differ.
Expect(ConstantTimeEquals(mac, Hmac.Sha256("key".Bytes, message)))-
HMAC-SHA1's 20 bytes, for the formats that ask for it.
-
HMAC-SHA1 of
text's UTF-8 bytes withkey. -
HMAC-SHA256's 32 bytes.
-
HMAC-SHA256 of
text's UTF-8 bytes withkey. -
HMAC-SHA512's 64 bytes.
-
HMAC-SHA512 of
text's UTF-8 bytes withkey.
Hkdf
struct
Public Struct Hkdf
HKDF (RFC 5869): length bytes of new key from material, a secret like a shared one from X25519, with salt, which can be empty, and info, which says what the key is for, so keys for different uses differ.
Examples
Local shared = SecureRandom.Bytes(32)
Local key = Hkdf.Sha256(shared, [], "session keys".Bytes, 32)
Expect(key.Count == 32)
Expect(key == Hkdf.Sha256(shared, [], "session keys".Bytes, 32))-
@PanicsAtCaller Public Static Method Sha256(material: UInt8[], salt: UInt8[], info: UInt8[], length: Int): UInt8[]static method #lengthbytes of key frommaterial,salt, andinfo, by HMAC-SHA256. Panics unlesslengthis from 0 to 8160, 255 hashes. -
@PanicsAtCaller Public Static Method Sha512(material: UInt8[], salt: UInt8[], info: UInt8[], length: Int): UInt8[]static method #lengthbytes of key frommaterial,salt, andinfo, by HMAC-SHA512. Panics unlesslengthis from 0 to 16320, 255 hashes.
Pbkdf2
struct
Public Struct Pbkdf2
PBKDF2 (RFC 8018) with HMAC: length bytes of key from password and salt, in iterations rounds, which make a guess at the password cost that many. For keeping passwords, Argon2id is stronger, since it needs memory too. Each panics unless iterations is 1 or more and length isn't negative.
Examples
Local salt = SecureRandom.Bytes(16)
Local key = Pbkdf2.Sha256("correct horse", salt, 1000, 32)
Expect(key.Count == 32)
Expect(key == Pbkdf2.Sha256("correct horse", salt, 1000, 32))std/crypto/derive.fb, line 107
-
@PanicsAtCaller Public Static Method Sha1(password: String, salt: UInt8[], iterations: Int, length: Int): UInt8[]static method #By SHA-1, for the formats that ask for it.
-
@PanicsAtCaller Public Static Method Sha1(password: UInt8[], salt: UInt8[], iterations: Int, length: Int): UInt8[]static method #A key derived from the bytes of
passwordby SHA-1, as the one that takes a String does. -
@PanicsAtCaller Public Static Method Sha256(password: String, salt: UInt8[], iterations: Int, length: Int): UInt8[]static method #A key from
passwordandsalt, initerationsrounds of HMAC-SHA256. -
@PanicsAtCaller Public Static Method Sha256(password: UInt8[], salt: UInt8[], iterations: Int, length: Int): UInt8[]static method #A key derived from the bytes of
passwordby SHA-256, as the one that takes a String does. -
@PanicsAtCaller Public Static Method Sha512(password: String, salt: UInt8[], iterations: Int, length: Int): UInt8[]static method #A key from
passwordandsalt, initerationsrounds of HMAC-SHA512. -
@PanicsAtCaller Public Static Method Sha512(password: UInt8[], salt: UInt8[], iterations: Int, length: Int): UInt8[]static method #A key derived from the bytes of
passwordby SHA-512, as the one that takes a String does.
Report an issue with this page or with FutureBlitz on GitHub.