silk.crypto.sha3.
Compute SHA-3 digests (SHA3-224, SHA3-256, SHA3-384, SHA3-512) and the SHAKE128 and SHAKE256 extendable-output functions (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.
Shake128 and Shake256 return as many bytes as the caller asks for. finishInto fills
the whole output slice, so a 16-byte slice gives the 16-byte SHAKE128 output that Node's
createHash("shake128") returns by default. A shorter output is a prefix of a longer one.
Gotchas#
SHA-3 is not Keccak-256 as Ethereum uses it: the padding is different, so the digests are different.
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: 8.
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.
Implementation Digest for Sha3_224#
impl Digest for Sha3_224Operation make#
make: Sha3_224.makeReturns a SHA3-224 state for an empty message.
Operation update#
update: Sha3_224.updateAdds bytes to the end of the message.
Operation blockLength#
fn blockLength(self: &Self) -> usizeReturns 144, the SHA3-224 block length in bytes.
Operation digestLength#
fn digestLength(self: &Self) -> usizeReturns 28, the SHA3-224 digest length in bytes.
Operation objectIdentifier#
fn objectIdentifier(self: &Self) -> &[u8]Returns the content bytes of the SHA3-224 object identifier, 2.16.840.1.101.3.4.2.7.
Operation copy#
fn copy(self: &Self) -> SelfReturns an independent copy of the state.
Operation finishInto#
fn finishInto(self: Self, output: &mut [u8]) -> ()Writes the 28-byte SHA3-224 digest to the start of output and consumes the state.
Implementation Digest for Sha3_256#
impl Digest for Sha3_256Operation make#
make: Sha3_256.makeReturns a SHA3-256 state for an empty message.
Operation update#
update: Sha3_256.updateAdds bytes to the end of the message.
Operation blockLength#
fn blockLength(self: &Self) -> usizeReturns 136, the SHA3-256 block length in bytes.
Operation digestLength#
fn digestLength(self: &Self) -> usizeReturns 32, the SHA3-256 digest length in bytes.
Operation objectIdentifier#
fn objectIdentifier(self: &Self) -> &[u8]Returns the content bytes of the SHA3-256 object identifier, 2.16.840.1.101.3.4.2.8.
Operation copy#
fn copy(self: &Self) -> SelfReturns an independent copy of the state.
Operation finishInto#
fn finishInto(self: Self, output: &mut [u8]) -> ()Writes the 32-byte SHA3-256 digest to the start of output and consumes the state.
Implementation Digest for Sha3_384#
impl Digest for Sha3_384Operation make#
make: Sha3_384.makeReturns a SHA3-384 state for an empty message.
Operation update#
update: Sha3_384.updateAdds bytes to the end of the message.
Operation blockLength#
fn blockLength(self: &Self) -> usizeReturns 104, the SHA3-384 block length in bytes.
Operation digestLength#
fn digestLength(self: &Self) -> usizeReturns 48, the SHA3-384 digest length in bytes.
Operation objectIdentifier#
fn objectIdentifier(self: &Self) -> &[u8]Returns the content bytes of the SHA3-384 object identifier, 2.16.840.1.101.3.4.2.9.
Operation copy#
fn copy(self: &Self) -> SelfReturns an independent copy of the state.
Operation finishInto#
fn finishInto(self: Self, output: &mut [u8]) -> ()Writes the 48-byte SHA3-384 digest to the start of output and consumes the state.
Implementation Digest for Sha3_512#
impl Digest for Sha3_512Operation make#
make: Sha3_512.makeReturns a SHA3-512 state for an empty message.
Operation update#
update: Sha3_512.updateAdds bytes to the end of the message.
Operation blockLength#
fn blockLength(self: &Self) -> usizeReturns 72, the SHA3-512 block length in bytes.
Operation digestLength#
fn digestLength(self: &Self) -> usizeReturns 64, the SHA3-512 digest length in bytes.
Operation objectIdentifier#
fn objectIdentifier(self: &Self) -> &[u8]Returns the content bytes of the SHA3-512 object identifier, 2.16.840.1.101.3.4.2.10.
Operation copy#
fn copy(self: &Self) -> SelfReturns an independent copy of the state.
Operation finishInto#
fn finishInto(self: Self, output: &mut [u8]) -> ()Writes the 64-byte SHA3-512 digest to the start of output and consumes the state.
Shake128#
pub struct Shake128A streaming SHAKE128 state: an extendable-output function that returns any number of bytes.
Call Shake128.update for each part of the message, then Shake128.finishInto with a slice
of the output length you need. Use Shake128.hash when the full message is in one slice.
The state is a fixed-size value and does not allocate.
Gotchas#
It does not conform to Digest, because the output length is the caller's choice. SHAKE128
gives at most 64 bits of collision resistance whatever the output length, and
128-bit preimage resistance only for outputs of at least 16 bytes.
Implementation Shake128#
impl Shake128Associated function make#
pub fn make() -> Shake128Returns a SHAKE128 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
output. An empty slice changes nothing.
Method copy#
pub fn copy(self: &Self) -> Shake128Returns an independent copy of the state. Updates to either one do not change the other.
Method finishInto#
pub fn finishInto(self: Self, output: &mut [u8]) -> ()Fills output with the first output.length bytes of the SHAKE128 output and consumes
the state. An empty slice is allowed. Node's default output length is 16 bytes.
Method finish#
pub fn finish(self: Self) -> Shake128ReaderEnds the message and returns a reader for the SHAKE128 output, consuming the state.
Successive Shake128Reader.read calls return successive bytes of the output, so their
concatenation equals what Shake128.finishInto writes for the same total length.
Associated function hash#
pub fn hash(bytes: &[u8], output: &mut [u8]) -> ()Fills output with the SHAKE128 output of bytes.
Associated function objectIdentifier#
pub fn objectIdentifier() -> &'static [u8]Returns the content bytes of the SHAKE128 object identifier, 2.16.840.1.101.3.4.2.11.
Shake128Reader#
pub struct Shake128ReaderThe output side of a finished Shake128: reads the SHAKE128 output a part at a time.
Get one from Shake128.finish. Each Shake128Reader.read continues where the last one stopped,
with any part sizes, for as many bytes as the caller wants. Use it where the needed length is
not known up front, as in the rejection sampling of ML-KEM and ML-DSA. It does not allocate.
Implementation Shake128Reader#
impl Shake128ReaderMethod read#
pub fn read(self: &mut Self, output: &mut [u8]) -> ()Fills output with the next output.length bytes of the output. An empty slice is allowed.
Shake256#
pub struct Shake256A streaming SHAKE256 state: an extendable-output function that returns any number of bytes.
Call Shake256.update for each part of the message, then Shake256.finishInto with a slice
of the output length you need. Use Shake256.hash when the full message is in one slice.
The state is a fixed-size value and does not allocate.
Gotchas#
It does not conform to Digest, because the output length is the caller's choice. SHAKE256
gives at most 128 bits of collision resistance whatever the output length, and
256-bit preimage resistance only for outputs of at least 32 bytes.
Implementation Shake256#
impl Shake256Associated function make#
pub fn make() -> Shake256Returns a SHAKE256 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
output. An empty slice changes nothing.
Method copy#
pub fn copy(self: &Self) -> Shake256Returns an independent copy of the state. Updates to either one do not change the other.
Method finishInto#
pub fn finishInto(self: Self, output: &mut [u8]) -> ()Fills output with the first output.length bytes of the SHAKE256 output and consumes
the state. An empty slice is allowed. Node's default output length is 32 bytes.
Method finish#
pub fn finish(self: Self) -> Shake256ReaderEnds the message and returns a reader for the SHAKE256 output, consuming the state.
Successive Shake256Reader.read calls return successive bytes of the output, so their
concatenation equals what Shake256.finishInto writes for the same total length.
Associated function hash#
pub fn hash(bytes: &[u8], output: &mut [u8]) -> ()Fills output with the SHAKE256 output of bytes.
Associated function objectIdentifier#
pub fn objectIdentifier() -> &'static [u8]Returns the content bytes of the SHAKE256 object identifier, 2.16.840.1.101.3.4.2.12.
Shake256Reader#
pub struct Shake256ReaderThe output side of a finished Shake256: reads the SHAKE256 output a part at a time.
Get one from Shake256.finish. Each Shake256Reader.read continues where the last one stopped,
with any part sizes, for as many bytes as the caller wants. Use it where the needed length is
not known up front, as in the rejection sampling of ML-KEM and ML-DSA. It does not allocate.
Implementation Shake256Reader#
impl Shake256ReaderMethod read#
pub fn read(self: &mut Self, output: &mut [u8]) -> ()Fills output with the next output.length bytes of the output. An empty slice is allowed.
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, x86_64-pc-windows-msvc, wasm64-unknown-unknown.