Expand description
This crate provides an implementation of the MD5 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_md5 crate.
§Setup
To use this crate, add the following entry to your Cargo.toml file in the dependencies section:
[dependencies]
chksum-md5 = "0.1.0"Alternatively, you can use the cargo add subcommand:
cargo add chksum-md5§Usage
Use the chksum function to calculate digest of file, directory and so on.
use std::fs::File;
use chksum_md5 as md5;
let file = File::open(path)?;
let digest = md5::chksum(file)?;
assert_eq!(
digest.to_hex_lowercase(),
"5c71dbb287630d65ca93764c34d9aa0d"
);§Asynchronous Runtime
Use the async_chksum function to calculate digest of file, directory and so on.
use chksum_md5 as md5;
use tokio::fs::File;
let file = File::open(path).await?;
let digest = md5::async_chksum(file).await?;
assert_eq!(
digest.to_hex_lowercase(),
"5c71dbb287630d65ca93764c34d9aa0d"
);§Input Types
§Bytes
§Array
use chksum_md5 as md5;
let data = [0, 1, 2, 3];
let digest = md5::chksum(data)?;
assert_eq!(
digest.to_hex_lowercase(),
"5c71dbb287630d65ca93764c34d9aa0d"
);§Vec
use chksum_md5 as md5;
let data = vec![0, 1, 2, 3];
let digest = md5::chksum(data)?;
assert_eq!(
digest.to_hex_lowercase(),
"5c71dbb287630d65ca93764c34d9aa0d"
);§Slice
use chksum_md5 as md5;
let data = &[0, 1, 2, 3];
let digest = md5::chksum(data)?;
assert_eq!(
digest.to_hex_lowercase(),
"5c71dbb287630d65ca93764c34d9aa0d"
);§Strings
§str
use chksum_md5 as md5;
let data = "&str";
let digest = md5::chksum(data)?;
assert_eq!(
digest.to_hex_lowercase(),
"5c71dbb287630d65ca93764c34d9aa0d"
);§String
use chksum_md5 as md5;
let data = String::from("String");
let digest = md5::chksum(data)?;
assert_eq!(
digest.to_hex_lowercase(),
"5c71dbb287630d65ca93764c34d9aa0d"
);§File
use std::fs::File;
use chksum_md5 as md5;
let file = File::open(path)?;
let digest = md5::chksum(file)?;
assert_eq!(
digest.to_hex_lowercase(),
"5c71dbb287630d65ca93764c34d9aa0d"
);§Directory
use std::fs::read_dir;
use chksum_md5 as md5;
let readdir = read_dir(path)?;
let digest = md5::chksum(readdir)?;
assert_eq!(
digest.to_hex_lowercase(),
"5c71dbb287630d65ca93764c34d9aa0d"
);§Path
use std::path::PathBuf;
use chksum_md5 as md5;
let path = PathBuf::from(path);
let digest = md5::chksum(path)?;
assert_eq!(
digest.to_hex_lowercase(),
"5c71dbb287630d65ca93764c34d9aa0d"
);§Standard Input
use std::io::stdin;
use chksum_md5 as md5;
let stdin = stdin();
let digest = md5::chksum(stdin)?;
assert_eq!(
digest.to_hex_lowercase(),
"5c71dbb287630d65ca93764c34d9aa0d"
);§Features
Cargo features are utilized to enable extra options.
readerenables thereadermodule with theReaderstruct.writerenables thewritermodule with theWriterstruct.
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-md5 = { version = "0.1.0", features = ["reader", "writer"] }Alternatively, you can use the cargo add subcommand:
cargo add chksum-md5 --features reader,writer§Asynchronous Runtime
async-runtime-tokio: Enables async interface for Tokio runtime.
By default, neither of these features is enabled.
§Disclaimer
The MD5 hash function should be used only for backward compatibility due to security issues.
Check RFC 6151: Updated Security Considerations for the MD5 Message-Digest and the HMAC-MD5 Algorithms for more details.
§License
This crate is licensed under the MIT License.
Re-exports§
pub use chksum_core::AsyncChksumable;async-runtime-tokiopub 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_md5 as hash;
Modules§
- reader
reader - This module is optional and can be enabled using the
readerCargo feature. - writer
writer - This module is optional and can be enabled using the
writerCargo feature.
Structs§
Functions§
- async_
chksum async-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§
- Async
Reader readerandasync-runtime-tokio - A specialized
AsyncReadertype with theMD5hash algorithm. - Async
Writer writerandasync-runtime-tokio - A specialized
AsyncWritertype with theMD5hash algorithm. - Reader
reader - A specialized
Readertype with theMD5hash algorithm. - Writer
writer - A specialized
Writertype with theMD5hash algorithm.