An implementation of the BLAKE2 hash functions.
Based on the blake2-rfc crate.
Usage
Blake2b
can be used in the following way:
use ;
// create a Blake2b object
let mut hasher = new;
// write input message
hasher.input;
// read hash digest and consume hasher
let output = hasher.result;
println!;
Same example for Blake2s
:
use ;
let mut hasher = new;
hasher.input;
let output = hasher.result;
println!;
Variable output size
Both Blake2b
and Blake2s
support variable output sizes through
VariableOutput
trait. Input
trait has to be imported as well.
use Blake2b;
use ;
let mut hasher = new.unwrap;
// instead of `input` method here we should use `process`
hasher.process;
let mut buf = ;
hasher.variable_result.unwrap;
assert_eq!
Message Authentication Code (MAC)
BLAKE2 can be used as a MAC without any additionall constructs:
use Blake2b;
use Mac;
let mut hasher = new.unwrap;
hasher.input;
// `result` has type `MacResult` which is a thin wrapper around array of
// bytes for providing constant time equality check
let result = hasher.result;
// To get underlying array use `code` method, but be carefull, since
// incorrect use of the code value may permit timing attacks which defeat
// the security provided by the `MacResult`
let code_bytes = result.code;
// To verify the message it's recommended to use `verify` method
let mut hasher = new.unwrap;
hasher.input;
// `verify` return `Ok(())` if code is correct, `Err(MacError)` otherwise
hasher.verify.unwrap;