silk.crypto.md5_sha1.

Compute the 36-byte MD5-SHA1 digest that TLS 1.0 and 1.1 use: MD5 followed by SHA-1.

When to use#

Use Md5Sha1 only where a legacy format names it, such as Node's md5-sha1 hash or the TLS 1.0 and 1.1 RSA signatures. Use SHA-256 elsewhere.

Details#

The digest is the 16-byte MD5 digest of the message followed by its 20-byte SHA-1 digest. The streaming state does not allocate. Call make, update, and finish, or use hash.

Gotchas#

MD5 and SHA-1 both permit deliberate collisions, so the pair does not protect data against tampering. The hash has no object identifier, so Digest.objectIdentifier returns an empty slice.

Examples#

Hash a message#

silk
import silk.crypto.md5_sha1 { Md5Sha1 }

pub fn main() -> i32 {
  let digest = Md5Sha1.hash(b"abc")
  // MD5("abc") starts with 90 and SHA-1("abc") with a9.
  if digest[0] != 0x90 || digest[16] != 0xa9 || digest[35] != 0x9d {
    return 1
  }
  return 0
}

Import as Md5Sha1 with import silk.crypto.md5_sha1 { Md5Sha1 }.

Public declarations: 1.

Md5Sha1#

silk
pub struct Md5Sha1

A streaming MD5-SHA1 state that returns a 36-byte digest. It does not allocate.

Implementation Md5Sha1#

silk
impl Md5Sha1

Associated function make#

silk
pub fn make() -> Self

Returns a state for an empty message.

Method update#

silk
pub fn update(self: &mut Self, bytes: &[u8]) -> ()

Adds bytes to the message.

Method finish#

silk
pub fn finish(self: Self) -> [u8; 36]

Consumes the state and returns the MD5 digest followed by the SHA-1 digest.

Associated function hash#

silk
pub fn hash(bytes: &[u8]) -> [u8; 36]

Returns the 36-byte MD5-SHA1 digest of bytes. It does not allocate.

Implementation Digest for Md5Sha1#

silk
impl Digest for Md5Sha1

Operation make#

silk
make: Md5Sha1.make

Returns an MD5-SHA1 state for an empty message.

Operation update#

silk
update: Md5Sha1.update

Adds bytes to the end of the message.

Operation blockLength#

silk
fn blockLength(self: &Self) -> usize

Returns 64, the block length of both MD5 and SHA-1 in bytes.

Operation digestLength#

silk
fn digestLength(self: &Self) -> usize

Returns 36, the MD5-SHA1 digest length in bytes.

Operation objectIdentifier#

silk
fn objectIdentifier(self: &Self) -> &[u8]

Returns an empty slice, because MD5-SHA1 has no object identifier.

Operation copy#

silk
fn copy(self: &Self) -> Self

Returns an independent copy of the state.

Operation finishInto#

silk
fn finishInto(self: Self, output: &mut [u8]) -> ()

Writes the 36-byte MD5-SHA1 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.

View the source of this page