Expand description
§SHA3
https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf
// Hash some byte data
let mut hasher = Sha3_224::new();
let mut result = [0u8; SHA3_224_BYTES];
hasher.update(b"Hello, world!")?;
hasher.finalize(&mut result)?;
assert_eq!(result.len(), SHA3_224_BYTES);
let res = b2h(&BitVec::<u8, Lsb0>::from_slice(&result), false, false)?;
assert_eq!("6a33e22f20f16642697e8bd549ff7b759252ad56c05a1b0acc31dc69", res);
// ...or hash some bits
let mut hasher = Sha3_224::new();
let mut result = [0u8; SHA3_224_BYTES];
hasher.update_bits(bits![u8, Lsb0; 1, 0, 1])?;
hasher.finalize(&mut result)?;
assert_eq!(result.len(), SHA3_224_BYTES);
let res = b2h(&BitVec::<u8, Lsb0>::from_slice(&result), false, false)?;
assert_eq!("d115e9e3c619f6180c234dba721b302ffe0992df07eeea47464923c0", res);
// ...or generate an arbitrary number of bits
// bits can be generated for as long as the original hasher isn't dropped
let mut hasher = Shake128::new();
let mut result = BitVec::<u8, Lsb0>::with_capacity(32);
hasher.finalize()?;
hasher.get_bits(&mut result, 8)?;
assert_eq!(8, result.len());
let res = b2h(&result, false, false)?;
assert_eq!("7f", res);
hasher.get_bits(&mut result, 16)?;
assert_eq!(24, result.len());
let res = b2h(&result, false, false)?;
assert_eq!("7f9c2b", res);
hasher.get_bits(&mut result, 3)?;
assert_eq!(27, result.len());
let res = b2h(&result, false, false)?;
assert_eq!("7f9c2b04", res);
hasher.get_bits(&mut result, 5)?;
assert_eq!(32, result.len());
let res = b2h(&result, false, false)?;
assert_eq!("7f9c2ba4", res);
// ...or generate an arbitrary number of bytes through an iterator
let mut hasher = Shake256::new();
hasher.update_bits(bits![u8, Lsb0; 1, 0, 1]);
hasher.finalize()?;
let result = hasher.by_ref().take(4).collect::<Vec<u8>>();
assert_eq!(4, result.len());
let res = b2h(&BitVec::from_slice(&result), false, false)?;
assert_eq!("6f18287d", res);
let next = hasher.next();
assert_eq!(Some(0x53), next);
let next = hasher.next();
assert_eq!(Some(0x75), next);
// NOTE: Calling update or finalize after any hasher has been finalized
// is an error
let mut hasher = Sha3_224::new();
let mut result = [0u8; SHA3_224_BYTES];
hasher.update(b"Yoda!")?;
hasher.finalize(&mut result)?;
assert!(hasher.update(b"Hello, world!").is_err());
assert!(hasher.finalize(&mut result).is_err());
Macros§
Structs§
- BitSlice
- Bit-Addressable Memory
- BitVec
- Bit-Precision Dynamic Array
- Lsb0
- Least-Significant-First Bit Traversal
- Sha3_
224 - SHA3-224 hash function (
SHA3-224(M) = KECCAK[448](M||01, 224)
) - Sha3_
256 - SHA3-256 hash function (
SHA3-256(M) = KECCAK[512](M||01, 256)
) - Sha3_
384 - SHA3-384 hash function (
SHA3-384(M) = KECCAK[768](M||01, 384)
) - Sha3_
512 - SHA3-512 hash function (
SHA3-512(M) = KECCAK[1024](M||01, 512)
) - Shake128
- SHAKE128 XOF function (
SHAKE128(M, d) = KECCAK[256](M||1111, d)
) - Shake256
- SHAKE256 XOF function (
SHAKE256(M, d) = KECCAK[512](M||1111, d)
)
Enums§
- Sha3
Error - Sha3 Error
Constants§
- LANE_
COUNT - The number of lanes in the state array used by the keccak function
- SHA3_
224_ BYTES - The output size for the SHA3-224 hash function in bytes
- SHA3_
256_ BYTES - The output size for the SHA3-256 hash function in bytes
- SHA3_
384_ BYTES - The output size for the SHA3-384 hash function in bytes
- SHA3_
512_ BYTES - The output size for the SHA3-512 hash function in bytes
Traits§
- Hasher
- Trait for hashing data with a fixed output size and byte input.
- Hasher
Bits - Trait for hashing data with a fixed output size and
BitSlice
input. - XofHasher
- Trait for hashing data with an arbitrary output size and byte input data.
- XofHasher
Bits - Trait for hashing data with an arbitrary output size and
BitSlice
input data.
Functions§
- b2h
- bits to hex conversion defined at section B.1 in https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf
- f_200
- Keccak-f permutation with width 200 (
Keccak-f[200]
=Keccak-p[200, 18]
). - f_400
- Keccak-f permutation with width 400 (
Keccak-f[400]
=Keccak-p[400, 20]
). - f_800
- Keccak-f permutation with width 800 (
Keccak-f[800]
=Keccak-p[800, 22]
). - f_1600
- Keccak-f permutation with width 1600 (
Keccak-f[1600]
=Keccak-p[1600, 24]
). - p_200
- Keccak-p permutation with width 200 (
Keccak-p[200, nr]
) - p_400
- Keccak-p permutation with width 400 (
Keccak-p[400, nr]
) - p_800
- Keccak-p permutation with width 800 (
Keccak-p[800, nr]
) - p_1600
- Keccak-p permutation with width 1600 (
Keccak-p[1600, nr]
)