Expand description
Implements SHA2 as per NIST FIPS 180-4.
§Examples
§Hash
Hash functionality is accessed via the bouncycastle_core_interface::traits::Hash trait, which is implemented by SHA224, SHA256, SHA384 and SHA512.
The simplest usage is via the static functions.
use bouncycastle_core_interface::traits::Hash;
let data: &[u8] = b"Hello, world!";
let output: Vec<u8> = sha2::SHA256::new().hash(data);More advanced usage will require creating a SHA3 or SHAKE object to hold state between successive calls, for example if input is received in chunks and not all available at the same time:
use bouncycastle_core_interface::traits::Hash;
use bouncycastle_sha2 as sha2;
let data: &[u8] = b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F
\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F
\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F
\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F";
let mut sha2 = sha2::SHA256::new();
for chunk in data.chunks(16) {
sha2.do_update(chunk);
}
let output: Vec<u8> = sha2.do_final();