# chksum
[](https://crates.io/crates/chksum)
[](https://github.com/chksum-rs/lib/actions/workflows/rust.yml)
[](https://docs.rs/chksum/)
[](https://github.com/chksum-rs/lib/blob/master/Cargo.toml)
[](https://deps.rs/crate/chksum/0.4.0)
[](https://github.com/rust-secure-code/safety-dance)
[](https://github.com/chksum-rs/lib/blob/master/LICENSE)
An implementation of various hash functions with a straightforward interface for computing digests of bytes, files, directories, and more.
## Setup
To use this crate, add the following entry to your `Cargo.toml` file in the `dependencies` section:
```toml
[dependencies]
chksum = "0.4.0"
```
Alternatively, you can use the [`cargo add`](https://doc.rust-lang.org/cargo/commands/cargo-add.html) subcommand:
```shell
cargo add chksum
```
## Usage
Use the `chksum` function with the desired algorithm to calculate digest of file, directory and so on.
```rust
use chksum::{chksum, SHA2_256};
let file = File::open(path)?;
let digest = chksum::<SHA2_256>(file)?;
assert_eq!(
digest.to_hex_lowercase(),
"44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061"
);
```
Alternatively, use the `chksum` function directly from the chosen hash module.
```rust
use chksum::sha2_256;
let file = File::open(path)?;
let digest = sha2_256::chksum(file)?;
assert_eq!(
digest.to_hex_lowercase(),
"44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061"
);
```
For more usage examples, refer to the documentation available at [docs.rs](https://docs.rs/chksum/).
### Asynchronous Runtime
Use `async-runtime-tokio` feature to enable Tokio asynchronous runtime.
```shell
cargo add chksum --features async-runtime-tokio
```
Use the `async_chksum` function with the desired algorithm to calculate digest of file, directory and so on.
```rust
use chksum::{async_chksum, SHA2_256};
use tokio::fs::File;
let file = File::open(path)?.await;
let digest = chksum::<SHA2_256>(file)?.await;
assert_eq!(
digest.to_hex_lowercase(),
"44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061"
);
```
Alternatively, use the `async_chksum` function directly from the chosen hash module.
```rust
use chksum::sha2_256;
let file = File::open(path)?;
let digest = sha2_256::async_chksum(file)?;
assert_eq!(
digest.to_hex_lowercase(),
"44752f37272e944fd2c913a35342eaccdd1aaf189bae50676b301ab213fc5061"
);
```
For more usage examples, refer to the documentation available at [docs.rs](https://docs.rs/chksum/).
## Hash Algorithms
This crate provides implementations for the following hash algorithms:
* MD5 - [RFC 1321: The MD5 Message-Digest Algorithm](https://tools.ietf.org/html/rfc1321)
* SHA-1 - [RFC 3174: US Secure Hash Algorithm 1 (SHA1)](https://tools.ietf.org/html/rfc3174)
* SHA-2 family (SHA-224, SHA-256, SHA-386, SHA-512) - [FIPS PUB 180-4: Secure Hash Standard](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf)
## Features
### Algorithms
Cargo features are utilized to enable or disable specific hash algorithms.
* `md5`: Enables MD5 hash algorithm.
* `sha1`: Enables SHA-1 hash algorithm.
* `sha2`: Enables SHA-2 hash family algorithms.
* `sha2-224`: Enables only SHA-2 224 hash algorithm.
* `sha2-256`: Enables only SHA-2 256 hash algorithm.
* `sha2-384`: Enables only SHA-2 384 hash algorithm.
* `sha2-512`: Enables only SHA-2 512 hash algorithm.
By default, all of them are enabled.
### Extra Options
Cargo features are also utilized to enable extra options.
* `reader` enables the `reader` module with the `Reader` struct within each variant module.
* `writer` enables the `writer` module with the `Writer` struct within each variant module.
By default, neither of these features is enabled.
### 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.