std.crypto
std.crypto: hash functions (SHA-256, SHA-1), HMAC-SHA-256, OS random bytes and a constant-time compare. The hashes are plain Aye.
Each hash is a struct fed with `update` and ended with `final`; the one-shot functions do both. Words are `u32`: sums wrap (`+%`) and rotations are two shifts, as FIPS 180-4 defines them. SHA-1 is broken as a signature or collision-resistant hash; it is here for protocols that name it (the WebSocket handshake).
RandomError
pub error RandomError { RandomFailed }
Sha256
pub struct Sha256 {
state: [8]u32
buf: [128]u8
used: usize
total: u64
pub fn init() Sha256
pub fn update(self: *mut Self, data: []u8)
pub fn final(self: *mut Self) [32]u8
}
SHA-256 (FIPS 180-4) state. Make it with `Sha256.init()`.
pub fn init() Sha256-
A hash of the empty message so far.
pub fn update(self: *mut Self, data: []u8)-
Adds `data` to the message.
pub fn final(self: *mut Self) [32]u8-
The digest of everything added. The hash is spent afterwards: make a new one with `init` for the next message.
sha256
pub fn sha256(data: []u8) [32]u8
The SHA-256 digest of `data`.
Sha1
pub struct Sha1 {
state: [5]u32
buf: [128]u8
used: usize
total: u64
pub fn init() Sha1
pub fn update(self: *mut Self, data: []u8)
pub fn final(self: *mut Self) [20]u8
}
SHA-1 (FIPS 180-4) state. Make it with `Sha1.init()`. Not for new security designs.
pub fn init() Sha1-
A hash of the empty message so far.
pub fn update(self: *mut Self, data: []u8)-
Adds `data` to the message.
pub fn final(self: *mut Self) [20]u8-
The digest of everything added. The hash is spent afterwards: make a new one with `init` for the next message.
sha1
pub fn sha1(data: []u8) [20]u8
The SHA-1 digest of `data`.
HmacSha256
pub struct HmacSha256 {
inner: Sha256
opad: [64]u8
pub fn init(key: []u8) HmacSha256
pub fn update(self: *mut Self, data: []u8)
pub fn final(self: *mut Self) [32]u8
}
HMAC-SHA-256 (RFC 2104, RFC 4231) state. Make it with `HmacSha256.init(key)`.
pub fn init(key: []u8) HmacSha256-
A MAC under `key`; any key length (a key over 64 bytes is hashed first).
pub fn update(self: *mut Self, data: []u8)-
Adds `data` to the message.
pub fn final(self: *mut Self) [32]u8-
The MAC of everything added. The state is spent afterwards.
hmacSha256
pub fn hmacSha256(key: []u8, msg: []u8) [32]u8
The HMAC-SHA-256 of `msg` under `key`.
randomBytes
pub fn randomBytes(buf: []mut u8) RandomError!void
Fills `buf` with random bytes from the operating system (`getentropy`, at most 256 bytes per call, so a long buffer takes several).
constantTimeEqual
pub fn constantTimeEqual(a: []u8, b: []u8) bool
Do `a` and `b` have the same bytes? Takes the same time for any content of two slices of one length; slices of different lengths are not equal at once (a length is not secret).