Module hacl::digest

source ·
Expand description

Hashing

This module implements the SHA 1 and SHA 2 hash functions.

§Usage

This module provides two APIs

§Stateful Hashing

use hacl::digest::{Digest, Algorithm};

let expected_digest_256 = [
    0xa5, 0x35, 0xf2, 0x6a, 0xff, 0xbc, 0x1f, 0x08, 0x73, 0xdb, 0x15, 0x15, 0x9d, 0xce, 0xbf,
    0x25, 0x99, 0x64, 0xbe, 0x42, 0xde, 0xa8, 0x4d, 0x29, 0x00, 0x38, 0x4b, 0xee, 0x15, 0x09,
    0xe4, 0x00,
];
let expected_digest_512 = [
    0x36, 0x97, 0x36, 0x7c, 0xc9, 0x1e, 0xda, 0xa7, 0x6d, 0xb8, 0x03, 0x39, 0x61, 0x5f, 0xc2,
    0x12, 0xe1, 0x5e, 0x64, 0x3e, 0x31, 0x30, 0xf7, 0x1f, 0x28, 0xd0, 0x3f, 0x34, 0x3d, 0xf4,
    0x88, 0x0a, 0xd3, 0x6c, 0x63, 0xe5, 0x35, 0x1f, 0x56, 0xe0, 0xf7, 0xe0, 0x4c, 0x24, 0x96,
    0xc0, 0xb3, 0x6b, 0xcf, 0x7c, 0x5d, 0xcb, 0xf3, 0x5e, 0x38, 0xe9, 0xbb, 0x44, 0xf8, 0xa0,
    0xc2, 0x83, 0x42, 0x4e,
];

let data = b"evercrypt-rust bindings";

let mut digest_256 = Digest::new(Algorithm::Sha256).unwrap();
if digest_256.update(data).is_err() {
    panic!("Error hashing.");
}
let digest_256_result = match digest_256.finish() {
    Ok(d) => d,
    Err(e) => panic!("Finish digest failed.\n{:?}", e),
};

let mut digest_512 = Digest::new(Algorithm::Sha512).unwrap();
if digest_512.update(data).is_err() {
    panic!("Error hashing.");
}
let digest_512_result = match digest_512.finish() {
    Ok(d) => d,
    Err(e) => panic!("Finish digest failed.\n{:?}", e),
};

assert_eq!(&digest_256_result[..], &expected_digest_256[..]);
assert_eq!(&digest_512_result[..], &expected_digest_512[..]);

§Single-shot API

use hacl::digest::{self, Algorithm};

let expected_digest_256 = [
    0xa5, 0x35, 0xf2, 0x6a, 0xff, 0xbc, 0x1f, 0x08, 0x73, 0xdb, 0x15, 0x15, 0x9d, 0xce, 0xbf,
    0x25, 0x99, 0x64, 0xbe, 0x42, 0xde, 0xa8, 0x4d, 0x29, 0x00, 0x38, 0x4b, 0xee, 0x15, 0x09,
    0xe4, 0x00,
];
let expected_digest_512 = [
    0x36, 0x97, 0x36, 0x7c, 0xc9, 0x1e, 0xda, 0xa7, 0x6d, 0xb8, 0x03, 0x39, 0x61, 0x5f, 0xc2,
    0x12, 0xe1, 0x5e, 0x64, 0x3e, 0x31, 0x30, 0xf7, 0x1f, 0x28, 0xd0, 0x3f, 0x34, 0x3d, 0xf4,
    0x88, 0x0a, 0xd3, 0x6c, 0x63, 0xe5, 0x35, 0x1f, 0x56, 0xe0, 0xf7, 0xe0, 0x4c, 0x24, 0x96,
    0xc0, 0xb3, 0x6b, 0xcf, 0x7c, 0x5d, 0xcb, 0xf3, 0x5e, 0x38, 0xe9, 0xbb, 0x44, 0xf8, 0xa0,
    0xc2, 0x83, 0x42, 0x4e,
];

let data = b"evercrypt-rust bindings";

let digest_256 = digest::hash(Algorithm::Sha256, data);
let digest_512 = digest::hash(Algorithm::Sha512, data);

assert_eq!(&digest_256[..], &expected_digest_256[..]);
assert_eq!(&digest_512[..], &expected_digest_512[..]);

let digest_256 = digest::sha256(data);
let digest_512 = digest::sha512(data);

assert_eq!(&digest_256[..], &expected_digest_256[..]);
assert_eq!(&digest_512[..], &expected_digest_512[..]);

§SHA 3

use hacl::digest::{self, Algorithm};

let data = b"evercrypt-rust bindings";
let expected_digest_3_256 = [
    0x49, 0x4b, 0xc2, 0xea, 0x73, 0x43, 0x4f, 0x88, 0x62, 0x56, 0x13, 0x39, 0xda, 0x1a, 0x6d,
    0x58, 0x05, 0xee, 0x34, 0x4b, 0x67, 0x5d, 0x18, 0xfb, 0x9a, 0x81, 0xca, 0x65, 0xa7, 0x8f,
    0xeb, 0x6e,
];
let expected_digest_3_512 = [
    0x7a, 0xaa, 0x97, 0x5c, 0x6b, 0x15, 0x5b, 0x55, 0xd3, 0x7b, 0xa6, 0x99, 0x3f, 0x7e, 0x14,
    0xd9, 0x8c, 0x28, 0x0d, 0x2b, 0x2f, 0xc2, 0x4a, 0xa7, 0x84, 0x07, 0xcf, 0x15, 0x2d, 0x0a,
    0xca, 0xbc, 0x32, 0xf2, 0x11, 0xf4, 0x64, 0x30, 0x19, 0x0a, 0x35, 0x26, 0x94, 0x76, 0x84,
    0x2a, 0x1f, 0x17, 0x41, 0xad, 0x46, 0x06, 0xf6, 0xc8, 0xc6, 0xad, 0x8d, 0x02, 0x2e, 0x85,
    0xb4, 0x9d, 0x6b, 0xd7,
];

assert_eq!(digest::hash(Algorithm::Sha3_256, data), expected_digest_3_256);
assert_eq!(
    digest::hash(Algorithm::Sha3_512, data)[..],
    expected_digest_3_512[..]
);

§SHAKE

use hacl::digest::{self, Algorithm};

let data = b"evercrypt-rust bindings";
let expected_digest_128 = [
    0xfd, 0x3b, 0x31, 0x35, 0x35, 0x05, 0x87, 0xd5, 0x36, 0x2a, 0xae, 0x4d, 0x1c, 0x8a, 0x25,
    0xba, 0xa4, 0xec, 0x82, 0xef, 0xff, 0xb8, 0x27, 0x1c, 0x91, 0x20, 0xa2, 0xed, 0x53, 0x17,
    0x2a, 0xcc, 0x97, 0x97, 0x34, 0x65, 0x1e, 0x69, 0xb3, 0xb3, 0x27, 0x09, 0x4c, 0xc0, 0x5e,
    0xde, 0x3b, 0x5d, 0xf9, 0x98, 0xe6, 0x37, 0xce, 0x06, 0xb3, 0xa0, 0x53, 0xdf, 0x81, 0x80,
    0x99, 0x8c, 0xfc, 0x95,
];
let expected_digest_256 = [
    0xf0, 0x85, 0x60, 0x6b, 0xed, 0xca, 0x25, 0xe4, 0x3c, 0x97, 0x05, 0x0f, 0xf2, 0x3e, 0xe0,
    0xd9, 0xe5, 0x89, 0x14, 0xff, 0xbb, 0x30, 0x5a, 0x00, 0x26, 0x30, 0x1c, 0x25, 0x7a, 0x5a,
    0xeb, 0x50, 0x7e, 0x4b, 0x21, 0x19, 0x53, 0x3f, 0xf7, 0x23, 0xc7, 0xe1, 0xad, 0xc5, 0xdf,
    0x2a, 0x62, 0x1d, 0xad, 0x18, 0xa4, 0x46, 0xaf, 0xeb, 0x2a, 0x54, 0xb3, 0xad, 0xfe, 0xc7,
    0x8e, 0x08, 0x6a, 0x6f,
];

let digest: [u8; 64] = digest::shake128(data);
assert_eq!(digest, expected_digest_128.as_slice());
let digest: [u8; 64] = digest::shake256(data);
assert_eq!(&digest, expected_digest_256.as_slice());

§Blake2b

use hacl::digest::{self, Algorithm};

let data = [
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
    0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
    0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c,
    0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
    0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
    0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
    0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
    0x69, 0x6a,
];
let expected_digest = [
    0x22, 0xef, 0xf8, 0xe6, 0xdd, 0x52, 0x36, 0xf5, 0xf5, 0x7d, 0x94, 0xed, 0xe8, 0x74, 0xd6,
    0xc9, 0x42, 0x8e, 0x8f, 0x5d, 0x56, 0x6f, 0x17, 0xcd, 0x6d, 0x18, 0x48, 0xcd, 0x75, 0x2f,
    0xe1, 0x3c, 0x65, 0x5c, 0xb1, 0x0f, 0xba, 0xaf, 0xf7, 0x68, 0x72, 0xf2, 0xbf, 0x2d, 0xa9,
    0x9e, 0x15, 0xdc, 0x62, 0x40, 0x75, 0xe1, 0xec, 0x2f, 0x58, 0xa3, 0xf6, 0x40, 0x72, 0x12,
    0x18, 0x38, 0x56, 0x9e,
];

assert_eq!(digest::hash(Algorithm::Blake2b, &data)[..], expected_digest[..]);

§Blake2s

use hacl::digest::{self, Algorithm};

let data = [
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
    0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
    0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c,
    0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
];
let expected_digest = [
    0xe2, 0x90, 0xdd, 0x27, 0x0b, 0x46, 0x7f, 0x34, 0xab, 0x1c, 0x00, 0x2d, 0x34, 0x0f, 0xa0,
    0x16, 0x25, 0x7f, 0xf1, 0x9e, 0x58, 0x33, 0xfd, 0xbb, 0xf2, 0xcb, 0x40, 0x1c, 0x3b, 0x28,
    0x17, 0xde,
];

assert_eq!(digest::hash(Algorithm::Blake2s, &data), expected_digest);

Structs§

  • The digest struct for stateful hashing.

Enums§

Functions§

  • Single-shot API with a fixed length output.
  • Single-shot API with a fixed length output.
  • Returns the output size of a digest.
  • Create the digest for the given data and mode alg. The output has length get_digest_size(alg).
  • Single-shot API with a fixed length output.
  • Single-shot API with a fixed length output.
  • Single-shot API with a fixed length output.
  • Single-shot API with a fixed length output.
  • Single-shot API with a fixed length output.
  • Single-shot API with a fixed length output.
  • Single-shot API with a fixed length output.
  • Single-shot API with a fixed length output.
  • Single-shot API with a fixed length output.
  • SHAKE 128
  • SHAKE 256