Struct blake::Blake[][src]

pub struct Blake { /* fields omitted */ }
Expand description

Hashing state for multiple data sets.

Example

Hashing a string split into multiple chunks.

let mut state = Blake::new(256).unwrap();

state.update(b"Abolish ");
state.update(b"the ");
state.update(b"bourgeoisie");
state.update(b"!");

let mut result = [0; 32];
state.finalise(&mut result);
assert_eq!(Vec::from_iter(result.iter().map(|&i| i)),
           vec![0x35, 0xBF, 0x9C, 0x70, 0xFF, 0x63, 0xF1, 0x26,
                0x6A, 0xE7, 0x2C, 0xC9, 0x94, 0x6F, 0x59, 0xBB,
                0x0B, 0x21, 0xD8, 0xCC, 0x8E, 0x4D, 0xBB, 0x53,
                0x24, 0xDF, 0x10, 0xB7, 0x11, 0xF9, 0x82, 0x1C]);

A Write implementation is also provided:

let mut state = Blake::new(256).unwrap();
io::copy(&mut &b"The lazy fox jumps over the lazy dog."[..], &mut state).unwrap();

let mut result = [0; 32];
state.finalise(&mut result);
assert_eq!(Vec::from_iter(result.iter().map(|&i| i)),
           vec![0xF2, 0xE5, 0xA9, 0xD0, 0x93, 0xD8, 0xAA, 0x23,
                0x4E, 0x6C, 0x54, 0x50, 0x61, 0xE8, 0x17, 0xBE,
                0x83, 0x8B, 0x57, 0xD8, 0x99, 0x8F, 0x15, 0xDF,
                0x72, 0xE1, 0x03, 0x7F, 0xBF, 0xEB, 0x4F, 0xC7]);

Implementations

Create a new hash state and initialise it with the given bit length.

hashbitlen is the hash output length.
Valid values:

  • 224,
  • 256,
  • 384,
  • 512.

Returns:

  • Err(BlakeError::BadHashbitlen) if hashbitlen is not any of the mentioned above, or
  • Ok(Blake) if initialisation succeeds.
Examples

Incorrect hashbitlen

assert_eq!(Blake::new(0).map(|_| ()), Err(blake::BlakeError::BadHashbitlen));

Creating a 512-long state

Blake::new(512).unwrap();

Add a salt to the hash function.

Returns:

  • Err(BlakeError::Fail) if called after Blake::update(), or
  • Ok(()), if called before Blake::update().

The salt’s length depends on the hash function’s length.

hash function lengthsalt length
224 bits128 bits
256 bits128 bits
384 bits256 bits
512 bits256 bits
Examples
let mut result_unsalted = [0; 64];
let mut result_salted   = [0; 64];

let mut state_unsalted = Blake::new(512).unwrap();
let mut state_salted   = Blake::new(512).unwrap();

state_salted.add_salt(b"Violent  murder  of  the  proles").unwrap();

state_unsalted.update(&[]);
state_salted  .update(&[]);

state_unsalted.finalise(&mut result_unsalted);
state_salted  .finalise(&mut result_salted);

assert!(Vec::from_iter(result_unsalted.iter().map(|&i| i)) !=
        Vec::from_iter(result_salted  .iter().map(|&i| i)))

Append the provided data to the hash function.

Examples

Hashing a part of a short story

let mut result = [0; 64];

let mut state = Blake::new(512).unwrap();
state.update("    Serbiańcy znowu się pochlali, ale w sumie".as_bytes());
state.update("czegoż się po wschodnich słowianach spodziewać, swoją".as_bytes());
state.update("drogą. I, jak to wszystkim homo sapiensom się dzieje".as_bytes());
state.update("filozofować poczęli.".as_bytes());
state.finalise(&mut result);

assert_eq!(Vec::from_iter(result.iter().map(|&i| i)),
           vec![0xA2, 0x30, 0x50, 0x18, 0x10, 0x0D, 0x53, 0x61,
                0xC2, 0x2D, 0x61, 0x0A, 0x23, 0x4E, 0xA5, 0x28,
                0x18, 0x89, 0xA6, 0x44, 0x6E, 0xE1, 0xC4, 0x8A,
                0xDF, 0xD0, 0x6A, 0xDB, 0x1C, 0x00, 0x06, 0xA9,
                0x05, 0x0A, 0xCE, 0xB3, 0x43, 0x14, 0xB8, 0xB0,
                0x3F, 0xA3, 0xB7, 0x70, 0x5D, 0xFC, 0x14, 0xB9,
                0xAA, 0xCA, 0xDC, 0x5B, 0x34, 0x96, 0x0B, 0x3C,
                0x87, 0x1F, 0x69, 0x46, 0xCD, 0xC2, 0xB2, 0x14]);

Finish hashing and store the output result in the provided space.

The provided space must not be smaller than the hash function’s size, if the provided space is smaller than the hash function’s size, the behaviour is undefined.

Examples

Storing and verifying results of all possible sizes.

let mut result_224 = [0; 28];
let mut result_256 = [0; 32];
let mut result_384 = [0; 48];
let mut result_512 = [0; 64];

let mut state_224 = Blake::new(224).unwrap();
let mut state_256 = Blake::new(256).unwrap();
let mut state_384 = Blake::new(384).unwrap();
let mut state_512 = Blake::new(512).unwrap();

state_224.update(b"The lazy fox jumps over the lazy dog.");
state_256.update(b"The lazy fox jumps over the lazy dog.");
state_384.update(b"The lazy fox jumps over the lazy dog.");
state_512.update(b"The lazy fox jumps over the lazy dog.");

state_224.finalise(&mut result_224);
state_256.finalise(&mut result_256);
state_384.finalise(&mut result_384);
state_512.finalise(&mut result_512);

assert_eq!(Vec::from_iter(result_224.iter().map(|&i| i)),
           vec![0x34, 0x97, 0x89, 0x0F, 0xBC, 0x6A, 0x98, 0x1C,
                0xD2, 0x21, 0x34, 0x97, 0xE4, 0xA8, 0x0A, 0x66,
                0xD6, 0x5F, 0x4C, 0x05, 0x3D, 0x71, 0x0F, 0x7E,
                0xAB, 0x81, 0xA4, 0x2F]);
assert_eq!(Vec::from_iter(result_256.iter().map(|&i| i)),
           vec![0xF2, 0xE5, 0xA9, 0xD0, 0x93, 0xD8, 0xAA, 0x23,
                0x4E, 0x6C, 0x54, 0x50, 0x61, 0xE8, 0x17, 0xBE,
                0x83, 0x8B, 0x57, 0xD8, 0x99, 0x8F, 0x15, 0xDF,
                0x72, 0xE1, 0x03, 0x7F, 0xBF, 0xEB, 0x4F, 0xC7]);
assert_eq!(Vec::from_iter(result_384.iter().map(|&i| i)),
           vec![0xDD, 0x68, 0x1E, 0x3B, 0x56, 0xE4, 0x80, 0x01,
                0x39, 0x5A, 0xF7, 0xB7, 0x36, 0x7E, 0x50, 0xD2,
                0x74, 0x61, 0x2B, 0xC8, 0xCB, 0xFB, 0x42, 0xEE,
                0x0C, 0xEC, 0x30, 0x45, 0x9C, 0x8D, 0x01, 0x66,
                0xFC, 0xB5, 0x42, 0xE2, 0x8C, 0xB0, 0x59, 0x72,
                0x8D, 0x7B, 0x0A, 0x16, 0x05, 0x4E, 0xB2, 0xEB]);
assert_eq!(Vec::from_iter(result_512.iter().map(|&i| i)),
           vec![0x9A, 0xD4, 0x66, 0xCF, 0x81, 0x8B, 0x46, 0x9D,
                0x29, 0x8C, 0x62, 0x00, 0xAC, 0xD3, 0x06, 0xF9,
                0xA2, 0xF4, 0xA4, 0x9E, 0x26, 0x8C, 0xA1, 0x17,
                0xB5, 0x8F, 0x37, 0x84, 0x86, 0x35, 0x1B, 0x0A,
                0x71, 0x1B, 0x60, 0xD4, 0x1B, 0x68, 0x7F, 0xD3,
                0x5F, 0x30, 0xBE, 0x2E, 0x00, 0xA8, 0x25, 0xD6,
                0x66, 0x6D, 0x9C, 0x4C, 0x23, 0xA5, 0x23, 0xD3,
                0x10, 0xA0, 0x58, 0x3F, 0x1E, 0x7C, 0xCC, 0xFE]);

Trait Implementations

Executes the destructor for this type. Read more

The Write implementation updates the state with the provided data.

For example, to hash a file:

let mut state = Blake::new(256).unwrap();
io::copy(&mut File::open("LICENSE").unwrap(), &mut state).unwrap();

let mut result = [0; 32];
state.finalise(&mut result);
assert_eq!(Vec::from_iter(result.iter().map(|&i| i)),
           vec![0xED, 0xE4, 0xD8, 0xF8, 0x49, 0x25, 0xD0, 0xBD,
                0x06, 0xA4, 0xDC, 0x1C, 0xFD, 0x1B, 0x45, 0x62,
                0xA4, 0xBD, 0x35, 0x25, 0x76, 0x9B, 0x97, 0xF1,
                0x9B, 0x21, 0xC8, 0xDF, 0xDC, 0x4A, 0x80, 0xB1]);

Write a buffer into this writer, returning how many bytes were written. Read more

Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more

Like write, except that it writes from a slice of buffers. Read more

🔬 This is a nightly-only experimental API. (can_vector)

Determines if this Writer has an efficient write_vectored implementation. Read more

Attempts to write an entire buffer into this writer. Read more

🔬 This is a nightly-only experimental API. (write_all_vectored)

Attempts to write multiple buffers into this writer. Read more

Writes a formatted string into this writer, returning any error encountered. Read more

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.