struct SecretBox [src]
Alias for std.crypto.salsa20.SecretBox
NaCl-compatible secretbox API.
A secretbox contains both an encrypted message and an authentication tag to verify that it hasn't been tampered with.
A secret key shared by all the recipients must be already known in order to use this API.
Nonces are 192-bit large and can safely be chosen with a random number generator.
Members
- key_length (Constant)
- nonce_length (Constant)
- open (Function)
- seal (Function)
- tag_length (Constant)
Source
pub const SecretBox = struct {
/// Key length in bytes.
pub const key_length = XSalsa20Poly1305.key_length;
/// Nonce length in bytes.
pub const nonce_length = XSalsa20Poly1305.nonce_length;
/// Authentication tag length in bytes.
pub const tag_length = XSalsa20Poly1305.tag_length;
/// Encrypt and authenticate `m` using a nonce `npub` and a key `k`.
/// `c` must be exactly `tag_length` longer than `m`, as it will store both the ciphertext and the authentication tag.
pub fn seal(c: []u8, m: []const u8, npub: [nonce_length]u8, k: [key_length]u8) void {
debug.assert(c.len == tag_length + m.len);
XSalsa20Poly1305.encrypt(c[tag_length..], c[0..tag_length], m, "", npub, k);
}
/// Verify and decrypt `c` using a nonce `npub` and a key `k`.
/// `m` must be exactly `tag_length` smaller than `c`, as `c` includes an authentication tag in addition to the encrypted message.
pub fn open(m: []u8, c: []const u8, npub: [nonce_length]u8, k: [key_length]u8) AuthenticationError!void {
if (c.len < tag_length) {
return error.AuthenticationFailed;
}
debug.assert(m.len == c.len - tag_length);
return XSalsa20Poly1305.decrypt(m, c[tag_length..], c[0..tag_length].*, "", npub, k);
}
}