silk.crypto.sm3.
Compute SM3 digests (GB/T 32905-2016), the hash of the Chinese commercial cryptography suite.
When to use#
Use Sm3 where a protocol or format names SM3, such as SM2 signatures or the TLS 1.3
ShangMi suites of RFC 8998. Use SHA-256 elsewhere.
Details#
The streaming state does not allocate. Call make, update, and finish, or use hash. The
digest is 32 bytes.
Examples#
Hash a message in parts#
import silk.crypto.sm3 { Sm3 }
pub fn main() -> i32 {
let mut state = Sm3.make()
state.update(b"a")
state.update(b"bc")
let digest = state.finish()
// SM3("abc") is 66c7f0f4...a8e0.
if digest[0] != 0x66 || digest[31] != 0xe0 {
return 1
}
return 0
}See also#
RFC 8998 and the SM3 draft describe the algorithm.
Import as Sm3 with import silk.crypto.sm3 { Sm3 }.
Public declarations: 1.
Sm3#
pub struct Sm3A streaming SM3 state that returns a 32-byte digest. It does not allocate.
Implementation Sm3#
impl Sm3Associated function make#
pub fn make() -> SelfReturns a state for an empty message.
Method update#
pub fn update(self: &mut Self, bytes: &[u8]) -> ()Adds bytes to the message. The length in bits wraps modulo 2^64.
Method finish#
pub fn finish(self: Self) -> [u8; 32]Consumes the state and returns its 32-byte digest.
Associated function hash#
pub fn hash(bytes: &[u8]) -> [u8; 32]Returns the 32-byte SM3 digest of bytes. It does not allocate.
Implementation Digest for Sm3#
impl Digest for Sm3Operation make#
make: Sm3.makeReturns an SM3 state for an empty message.
Operation update#
update: Sm3.updateAdds bytes to the end of the message.
Operation blockLength#
fn blockLength(self: &Self) -> usizeReturns 64, the SM3 block length in bytes.
Operation digestLength#
fn digestLength(self: &Self) -> usizeReturns 32, the SM3 digest length in bytes.
Operation objectIdentifier#
fn objectIdentifier(self: &Self) -> &[u8]Returns the content bytes of the SM3 object identifier, 1.2.156.10197.1.401.
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 SM3 digest to the start of output and consumes the state.
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.