Crate chksum_sha2_256

Crate chksum_sha2_256 

Source
Expand description

This crate provides an implementation of the SHA-2 256 hash function with a straightforward interface for computing digests of bytes, files, directories, and more.

For a low-level interface, you can explore the chksum_hash_sha2_256 crate.

§Setup

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

[dependencies]
chksum-sha2-256 = "0.1.0"

Alternatively, you can use the cargo add subcommand:

cargo add chksum-sha2-256

§Usage

Use the chksum function to calculate digest of file, directory and so on.

use std::fs::File;

use chksum_sha2_256 as sha2_256;

let file = File::open(path)?;
let digest = sha2_256::chksum(file)?;
assert_eq!(
    digest.to_hex_lowercase(),
    "44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061"
);

§Asynchronous Runtime

Use the async_chksum function to calculate digest of file, directory and so on.

use chksum_sha2_256 as sha2_256;
use tokio::fs::File;

let file = File::open(path).await?;
let digest = sha2_256::async_chksum(file).await?;
assert_eq!(
    digest.to_hex_lowercase(),
    "44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061"
);

§Input Types

§Bytes

§Array

use chksum_sha2_256 as sha2_256;

let data = [0, 1, 2, 3];
let digest = sha2_256::chksum(data)?;
assert_eq!(
    digest.to_hex_lowercase(),
    "44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061"
);

§Vec

use chksum_sha2_256 as sha2_256;

let data = vec![0, 1, 2, 3];
let digest = sha2_256::chksum(data)?;
assert_eq!(
    digest.to_hex_lowercase(),
    "44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061"
);

§Slice

use chksum_sha2_256 as sha2_256;

let data = &[0, 1, 2, 3];
let digest = sha2_256::chksum(data)?;
assert_eq!(
    digest.to_hex_lowercase(),
    "44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061"
);

§Strings

§str

use chksum_sha2_256 as sha2_256;

let data = "&str";
let digest = sha2_256::chksum(data)?;
assert_eq!(
    digest.to_hex_lowercase(),
    "44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061"
);

§String

use chksum_sha2_256 as sha2_256;

let data = String::from("String");
let digest = sha2_256::chksum(data)?;
assert_eq!(
    digest.to_hex_lowercase(),
    "44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061"
);

§File

use std::fs::File;

use chksum_sha2_256 as sha2_256;

let file = File::open(path)?;
let digest = sha2_256::chksum(file)?;
assert_eq!(
    digest.to_hex_lowercase(),
    "44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061"
);

§Directory

use std::fs::read_dir;

use chksum_sha2_256 as sha2_256;

let readdir = read_dir(path)?;
let digest = sha2_256::chksum(readdir)?;
assert_eq!(
    digest.to_hex_lowercase(),
    "44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061"
);

§Path

use std::path::PathBuf;

use chksum_sha2_256 as sha2_256;

let path = PathBuf::from(path);
let digest = sha2_256::chksum(path)?;
assert_eq!(
    digest.to_hex_lowercase(),
    "44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061"
);

§Standard Input

use std::io::stdin;

use chksum_sha2_256 as sha2_256;

let stdin = stdin();
let digest = sha2_256::chksum(stdin)?;
assert_eq!(
    digest.to_hex_lowercase(),
    "44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061"
);

§Features

Cargo features are utilized to enable extra options.

By default, neither of these features is enabled.

To customize your setup, disable the default features and enable only those that you need in your Cargo.toml file:

[dependencies]
chksum-sha2-256 = { version = "0.1.0", features = ["reader", "writer"] }

Alternatively, you can use the cargo add subcommand:

cargo add chksum-sha2-256 --features reader,writer

§Asynchronous Runtime

  • async-runtime-tokio: Enables async interface for Tokio runtime.

By default, neither of these features is enabled.

§License

This crate is licensed under the MIT License.

Re-exports§

pub use chksum_core::AsyncChksumable;async-runtime-tokio
pub use chksum_core::Chksumable;
pub use chksum_core::Error;
pub use chksum_core::Hash;
pub use chksum_core::Hashable;
pub use chksum_core::Result;
pub use chksum_hash_sha2_256 as hash;

Modules§

readerreader
This module is optional and can be enabled using the reader Cargo feature.
writerwriter
This module is optional and can be enabled using the writer Cargo feature.

Structs§

Digest
A hash digest.
SHA2_256
The SHA-2 256 hash instance.

Functions§

async_chksumasync-runtime-tokio
Computes the hash of the given input.
chksum
Computes the hash of the given input.
default
Creates a default hash.
hash
Computes the hash of the given input.
new
Creates a new hash.

Type Aliases§

AsyncReaderreader and async-runtime-tokio
A specialized AsyncReader type with the SHA2_256 hash algorithm.
AsyncWriterwriter and async-runtime-tokio
A specialized AsyncWriter type with the SHA2_256 hash algorithm.
Readerreader
A specialized Reader type with the SHA2_256 hash algorithm.
Writerwriter
A specialized Writer type with the SHA2_256 hash algorithm.