hmac
import std::hash::hmac; · source
Keyed message authentication codes (RFC 2104) over SHA-256 and SHA-1.
HmacSha256 (HmacSha1) authenticates a message that arrives in pieces — new(key), update, then finish — or one held whole (mac), giving the code as a Sha256Digest (Sha1Digest). A key longer than a block is hashed first. verify recomputes a code and compares it with one received in time that does not depend on where they differ, which is how a received code must be checked: comparing with == stops at the first different byte and tells a caller how much of a forgery was right.
import std::hash::{ HmacSha256, Sha256Digest };
function main() -> int {
const key: Str = "key";
const message: Str = "The quick brown fox jumps over the lazy dog";
const tag: Sha256Digest = HmacSha256::mac(key.as_bytes(), message.as_bytes());
// tag.to_hex() == "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8"
const ok: boolean = HmacSha256::verify(key.as_bytes(), message.as_bytes(), tag.as_bytes());
return if (ok) { 0 } else { 1 };
}
Types
HmacSha256
type struct HmacSha256 {
private inner: Sha256;
private outer_key: [u8; 64];
}
Constructors
static new(key: &Slice<u8>) -> HmacSha256;
static new(key: &Slice<u8>) -> HmacSha256;A code under key over the empty message.
Static functions
static mac(key: &Slice<u8>, data: &Slice<u8>) -> Sha256Digest;
static mac(key: &Slice<u8>, data: &Slice<u8>) -> Sha256Digest;The code under key of data, a message held whole.
static verify(key: &Slice<u8>, data: &Slice<u8>, tag: &Slice<u8>) -> boolean;
static verify(key: &Slice<u8>, data: &Slice<u8>, tag: &Slice<u8>) -> boolean;Whether tag is the code under key of data, compared in time that does not depend on where a wrong tag differs.
Methods
update(mut &this, data: &Slice<u8>) -> void;
update(mut &this, data: &Slice<u8>) -> void;Append data to the message.
finish(mut this) -> Sha256Digest;
finish(mut this) -> Sha256Digest;The code of the message fed so far.
HmacSha1
type struct HmacSha1 {
private inner: Sha1;
private outer_key: [u8; 64];
}
Constructors
static new(key: &Slice<u8>) -> HmacSha1;
static new(key: &Slice<u8>) -> HmacSha1;A code under key over the empty message.
Static functions
static mac(key: &Slice<u8>, data: &Slice<u8>) -> Sha1Digest;
static mac(key: &Slice<u8>, data: &Slice<u8>) -> Sha1Digest;The code under key of data, a message held whole.
static verify(key: &Slice<u8>, data: &Slice<u8>, tag: &Slice<u8>) -> boolean;
static verify(key: &Slice<u8>, data: &Slice<u8>, tag: &Slice<u8>) -> boolean;Whether tag is the code under key of data, compared in time that does not depend on where a wrong tag differs.
Methods
update(mut &this, data: &Slice<u8>) -> void;
update(mut &this, data: &Slice<u8>) -> void;Append data to the message.
finish(mut this) -> Sha1Digest;
finish(mut this) -> Sha1Digest;The code of the message fed so far.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
type struct HmacSha256 {}HMAC-SHA-256 (RFC 2104 over FIPS 180-4), computed over a message that arrives in pieces. Any key length is accepted; one longer than 64 bytes is hashed first, as the RFC says.
std::hash::hmactype struct Sha256Digest with [Eq] {}A SHA-256 digest: the hash's 32 bytes, most significant byte of the first word first.
std::hash::sha256function name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
function main() -> intconst name: Type = value;An immutable binding: it cannot be reassigned after initialisation. The annotation may be omitted when an initialiser is present - the binding takes the initialiser's concrete type, inferred locally. Mutability is opt-in through mut.
const key: Strtype struct Str {}A read-only view of UTF-8 text owned elsewhere: a string literal, a String, a file's bytes. Copying a Str copies the view, not the text, and it is valid only while what it views is. Str::new("text") views a literal; String.as_str() views a String.
const message: Strconst tag: Sha256Digeststatic HmacSha256::mac(key: &Slice<u8>, data: &Slice<u8>) -> Sha256DigestThe code under key of data, a message held whole.
const key: StrStr::as_bytes(&this) -> &Slice<u8>Underlying byte view. Use this for byte-granularity algorithms (hashing, equality, substring search).
std::collections::strconst message: Strconst ok: booleanstatic HmacSha256::verify(key: &Slice<u8>, data: &Slice<u8>, tag: &Slice<u8>) -> booleanWhether tag is the code under key of data, compared in time that does not depend on where a wrong tag differs.
const tag: Sha256Digestreturn value;Leaves the current function with the given value. A function whose return type is not void must return one; a bare return; is for void. In an async function it completes the future with the value.
if (condition) { ... } else if (condition) { ... } else { ... }Branches on a parenthesised boolean condition. Bodies are always braced; there is no single-statement form. Used as an expression, both arms are required and must produce the same type: const sign = if (n < 0) { -1 } else { 1 };.
const ok: booleanif (condition) { ... } else { ... }The branch taken when every preceding if condition is false; else if chains another condition. In expression form the else arm is mandatory, so the expression always has a value.
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct HmacSha256 {}HMAC-SHA-256 (RFC 2104 over FIPS 180-4), computed over a message that arrives in pieces. Any key length is accepted; one longer than 64 bytes is hashed first, as the RFC says.
private type struct Name { ... }
private:Confined to the module that declares it - for items and members alike. A private field or function reached from another module is E0353, and a private type named there is E0503. private: opens a block of such members.
HmacSha256.inner: Sha256type struct Sha256 {}A SHA-256 computation in progress: the message so far, fed in order by update, and closed by finish.
HmacSha256.outer_key: [u8; 64]type struct HmacSha1 {}HMAC-SHA-1 (RFC 2104 over RFC 3174), for protocols that name it. Its shape is HmacSha256's; it gives a Sha1Digest.
HmacSha1.inner: Sha1type struct Sha1 {}A SHA-1 computation in progress: the message so far, fed in order by update, and closed by finish.
HmacSha1.outer_key: [u8; 64]namespace std::hash::hmac;hmac: keyed message authentication codes (RFC 2104) over SHA-256 and SHA-1.
module pagetype struct HmacSha1 {}HMAC-SHA-1 (RFC 2104 over RFC 3174), for protocols that name it. Its shape is HmacSha256's; it gives a Sha1Digest.
static HmacSha256::new(key: &Slice<u8>) -> HmacSha256A code under key over the empty message.
type struct Sha1Digest with [Eq] {}A SHA-1 digest: the hash's 20 bytes, most significant byte of the first word first.
std::hash::sha1static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
(parameter) key: &Slice<u8>type struct Slice<T> {}The elements are reached through ptr, declared read-only: through a mut &Slice<T> it reads as a writable pointer, through a &Slice<T> as the read-only one it is.
(parameter) data: &Slice<u8>(parameter) tag: &Slice<u8>mut &this
mut &TypeAn exclusive, mutating borrow. As a receiver, mut &this lets the method modify the fields - and callers can see that from the signature alone. As a parameter type, mut &T lets the callee mutate through the reference while the caller keeps ownership.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
mut thisA by-value receiver the body may also reassign. The receiver is moved into the method and consumed; the caller's value may not be used afterwards.
language reference