std.crypto
Cryptographic hashes, HMAC and the system's random bytes: the Digest interface with Sha256
and Sha512 (FIPS 180-4), Sha1 (FIPS 180-4) and Md5 (RFC 1321) — the last two only for
formats that require them, never where an attacker chooses the input —, Hmac over any digest
(RFC 2104), sha256 for a whole input at once, randomBytes and randomUint64 from the
system, and constantTimeEq, a comparison of bytes whose time does not depend on where they
differ. Everything is written in Lyric; the system's random bytes come through the runtime's
lyr_random_u64.
Examples
import std.io { println };
import std.crypto { Sha256, constantTimeEq };
import std.encoding { Hex };
let h = Sha256.new();
h.update("abc".bytes());
let digest = h.finish();
println(Hex.encode(digest));
let again = Sha256.new();
again.update("abc".bytes());
println(constantTimeEq(digest, again.finish()));
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
true
Types
update as often as there are pieces, then finish for the hash of all of them.Hmac<Sha256>.new(key), then update and finish as with a digest.Functions
pub fn sha256(bytes: Slice<uint8>): uint8[32]
Returns the SHA-256 hash of bytes, computed at once.
pub fn randomBytes(n: int): uint8[]
Returns n random bytes from the system (getrandom, getentropy, RtlGenRandom).
Panics
Panics with LYR-RT0015 when the system provides no random bytes.
pub fn randomUint64(): uint64
Returns 64 random bits from the system.
Panics
Panics with LYR-RT0015 when the system provides no random bytes.
pub fn constantTimeEq(a: Slice<uint8>, b: Slice<uint8>): bool
Returns whether a and b hold the same bytes, in a time that depends on their length alone —
not on where they first differ, which a comparison that stops early reveals to whoever measures
it. Slices of different lengths return false at once; lengths are not treated as secret.