Crate chksum_hash_sha2_384

source ·
Expand description

This crate provides an implementation of the SHA-2 384 hash function based on FIPS PUB 180-4: Secure Hash Standard.

§Setup

To use this crate, add the following entry to your Cargo.toml file in the dependencies section:

[dependencies]
chksum-hash-sha2-384 = "0.0.1"

Alternatively, you can use the cargo add subcommand:

cargo add chksum-hash-sha2-384

§Batch Processing

The digest of known-size data can be calculated with the hash function.

use chksum_hash_sha2_384 as sha2_384;

let digest = sha2_384::hash("example data");
assert_eq!(
    digest.to_hex_lowercase(),
    "12ecdfd463a85a301b7c29a43bf4b19cdfc6e5e86a5f40396aa6ae3368a7e5b0ed31f3bef2eb3071577ba610b4ed1cb8"
);

§Stream Processing

The digest of data streams can be calculated chunk-by-chunk with a consumer created by calling the default function.

// Import all necessary items
use std::fs::File;
use std::io::Read;

use chksum_hash_sha2_384 as sha2_384;

// Create a hash instance
let mut hash = sha2_384::default();

// Open a file and create a buffer for incoming data
let mut file = File::open(path)?;
let mut buffer = vec![0; 64];

// Iterate chunk by chunk
while let Ok(count) = file.read(&mut buffer) {
    // EOF reached, exit loop
    if count == 0 {
        break;
    }

    // Update the hash with data
    hash.update(&buffer[..count]);
}

// Calculate the digest
let digest = hash.digest();
// Cast the digest to hex and compare
assert_eq!(
    digest.to_hex_lowercase(),
    "12ecdfd463a85a301b7c29a43bf4b19cdfc6e5e86a5f40396aa6ae3368a7e5b0ed31f3bef2eb3071577ba610b4ed1cb8"
);

§Internal Buffering

An internal buffer is utilized due to the unknown size of data chunks.

The size of this buffer is at least as large as one hash block of data processed at a time.

To mitigate buffering-related performance issues, ensure the length of processed chunks is a multiple of the block size.

§Input Type

Anything that implements AsRef<[u8]> can be passed as input.

use chksum_hash_sha2_384 as sha2_384;

let digest = sha2_384::default()
    .update("str")
    .update(b"bytes")
    .update([0x75, 0x38])
    .digest();
assert_eq!(
    digest.to_hex_lowercase(),
    "fdf06709928130b6c22c579287e5633a1a9fc52b944c3be878211a8fa0c22a4c7f84acc6a5e86ae7017d61ed434f04d9"
);

Since Digest implements AsRef<[u8]>, digests can be chained to calculate hash of a hash digest.

use chksum_hash_sha2_384 as sha2_384;

let digest = sha2_384::hash(b"example data");
let digest = sha2_384::hash(digest);
assert_eq!(
    digest.to_hex_lowercase(),
    "e145be437140b10cd13e7e4e8b9c3ca5838381a58da6510e5832ce9b9b2880545e9d3ef32f8740f6cbbbf447aa00891d"
);

§License

This crate is licensed under the MIT License.

Modules§

  • Module contains items related to the input data block.
  • Module contains items related to the Digest structure.
  • Module contains items related to the State structure which allows to the direct MD5 state manipulation.

Structs§

  • A hash digest.
  • A finalized hash state.
  • A low-level hash state.
  • A hash state containing an internal buffer that can handle an unknown amount of input data.

Constants§

Functions§

  • Creates a default hash.
  • Computes the hash of the given input.
  • Creates a new hash.