silk.crypto.sha3.
Compute SHA-3 digests: SHA3-224, SHA3-256, SHA3-384, and SHA3-512 (FIPS 202).
When to use#
Use these types when a protocol names SHA-3. For a general cryptographic hash, silk.crypto.sha2 is
more common. Use silk.collections.hash for the keys of a hash map.
Details#
Sha3_224, Sha3_256, Sha3_384, and Sha3_512 return 28, 32, 48, and 64 bytes. Each
type has the same functions: hash returns the digest of one slice, and make, update, and
finish hash a message that arrives in parts. The states are fixed-size values and nothing
allocates. There is no length limit.
Gotchas#
SHA-3 is not Keccak-256 as Ethereum uses it: the padding is different, so the digests are different. This module has no SHAKE or other extendable-output function.
Examples#
Hash a message in one call and in parts#
import silk.crypto.sha3 { Sha3_256 }
pub fn main() -> i32 {
let oneShot = Sha3_256.hash(b"abc")
let mut state = Sha3_256.make()
state.update(b"a")
state.update(b"bc")
let streamed = state.finish()
// SHA3-256("abc") is 3a985da7...1532.
if oneShot[0] != 0x3a || oneShot[31] != 0x32 || streamed[31] != oneShot[31] {
return 1
}
return 0
}Import as Sha3_256 with import silk.crypto.sha3 { Sha3_256 }.
Public declarations: 4.
Sha3_224#
pub struct Sha3_224A streaming SHA3-224 state that returns a 28-byte digest.
Call Sha3_224.update for each part of the message, then Sha3_224.finish. Use
Sha3_224.hash when the full message is in one slice. The state is a fixed-size value and
does not allocate.
Gotchas#
It is not Keccak-256 as Ethereum uses it: the padding is different, so the digests are different.
Implementation Sha3_224#
impl Sha3_224Associated function make#
pub fn make() -> Sha3_224Returns a SHA3-224 state for an empty message.
Method update#
pub fn update(self: &mut Self, bytes: &[u8]) -> ()Adds bytes to the end of the message. Any split of the message into parts gives the same
digest. An empty slice changes nothing.
Method finish#
pub fn finish(self: Self) -> [u8; 28]Returns the 28-byte SHA3-224 digest of the message and consumes the state.
Associated function hash#
pub fn hash(bytes: &[u8]) -> [u8; 28]Returns the 28-byte SHA3-224 digest of bytes.
Sha3_256#
pub struct Sha3_256A streaming SHA3-256 state that returns a 32-byte digest.
Call Sha3_256.update for each part of the message, then Sha3_256.finish. Use
Sha3_256.hash when the full message is in one slice. The state is a fixed-size value and
does not allocate.
Gotchas#
It is not Keccak-256 as Ethereum uses it: the padding is different, so the digests are different.
Implementation Sha3_256#
impl Sha3_256Associated function make#
pub fn make() -> Sha3_256Returns a SHA3-256 state for an empty message.
Method update#
pub fn update(self: &mut Self, bytes: &[u8]) -> ()Adds bytes to the end of the message. Any split of the message into parts gives the same
digest. An empty slice changes nothing.
Method finish#
pub fn finish(self: Self) -> [u8; 32]Returns the 32-byte SHA3-256 digest of the message and consumes the state.
Associated function hash#
pub fn hash(bytes: &[u8]) -> [u8; 32]Returns the 32-byte SHA3-256 digest of bytes.
Sha3_384#
pub struct Sha3_384A streaming SHA3-384 state that returns a 48-byte digest.
Call Sha3_384.update for each part of the message, then Sha3_384.finish. Use
Sha3_384.hash when the full message is in one slice. The state is a fixed-size value and
does not allocate.
Gotchas#
It is not Keccak-256 as Ethereum uses it: the padding is different, so the digests are different.
Implementation Sha3_384#
impl Sha3_384Associated function make#
pub fn make() -> Sha3_384Returns a SHA3-384 state for an empty message.
Method update#
pub fn update(self: &mut Self, bytes: &[u8]) -> ()Adds bytes to the end of the message. Any split of the message into parts gives the same
digest. An empty slice changes nothing.
Method finish#
pub fn finish(self: Self) -> [u8; 48]Returns the 48-byte SHA3-384 digest of the message and consumes the state.
Associated function hash#
pub fn hash(bytes: &[u8]) -> [u8; 48]Returns the 48-byte SHA3-384 digest of bytes.
Sha3_512#
pub struct Sha3_512A streaming SHA3-512 state that returns a 64-byte digest.
Call Sha3_512.update for each part of the message, then Sha3_512.finish. Use
Sha3_512.hash when the full message is in one slice. The state is a fixed-size value and
does not allocate.
Gotchas#
It is not Keccak-256 as Ethereum uses it: the padding is different, so the digests are different.
Implementation Sha3_512#
impl Sha3_512Associated function make#
pub fn make() -> Sha3_512Returns a SHA3-512 state for an empty message.
Method update#
pub fn update(self: &mut Self, bytes: &[u8]) -> ()Adds bytes to the end of the message. Any split of the message into parts gives the same
digest. An empty slice changes nothing.
Method finish#
pub fn finish(self: Self) -> [u8; 64]Returns the 64-byte SHA3-512 digest of the message and consumes the state.
Associated function hash#
pub fn hash(bytes: &[u8]) -> [u8; 64]Returns the 64-byte SHA3-512 digest of bytes.
Category: Cryptography.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2.