Crate chksum_sha1

Source
Expand description

This crate provides an implementation of the SHA-1 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_sha1 crate.

§Setup

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

[dependencies]
chksum-sha1 = "0.1.0"

Alternatively, you can use the cargo add subcommand:

cargo add chksum-sha1

§Usage

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

use std::fs::File;

use chksum_sha1 as sha1;

let file = File::open(path)?;
let digest = sha1::chksum(file)?;
assert_eq!(
    digest.to_hex_lowercase(),
    "9fc42adac31303d68b444e6129f13f6093a0e045"
);

§Asynchronous Runtime

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

use chksum_sha1 as sha1;
use tokio::fs::File;

let file = File::open(path).await?;
let digest = sha1::async_chksum(file).await?;
assert_eq!(
    digest.to_hex_lowercase(),
    "9fc42adac31303d68b444e6129f13f6093a0e045"
);

§Input Types

§Bytes

§Array

use chksum_sha1 as sha1;

let data = [0, 1, 2, 3];
let digest = sha1::chksum(data)?;
assert_eq!(
    digest.to_hex_lowercase(),
    "9fc42adac31303d68b444e6129f13f6093a0e045"
);

§Vec

use chksum_sha1 as sha1;

let data = vec![0, 1, 2, 3];
let digest = sha1::chksum(data)?;
assert_eq!(
    digest.to_hex_lowercase(),
    "9fc42adac31303d68b444e6129f13f6093a0e045"
);

§Slice

use chksum_sha1 as sha1;

let data = &[0, 1, 2, 3];
let digest = sha1::chksum(data)?;
assert_eq!(
    digest.to_hex_lowercase(),
    "9fc42adac31303d68b444e6129f13f6093a0e045"
);

§Strings

§str

use chksum_sha1 as sha1;

let data = "&str";
let digest = sha1::chksum(data)?;
assert_eq!(
    digest.to_hex_lowercase(),
    "9fc42adac31303d68b444e6129f13f6093a0e045"
);

§String

use chksum_sha1 as sha1;

let data = String::from("String");
let digest = sha1::chksum(data)?;
assert_eq!(
    digest.to_hex_lowercase(),
    "9fc42adac31303d68b444e6129f13f6093a0e045"
);

§File

use std::fs::File;

use chksum_sha1 as sha1;

let file = File::open(path)?;
let digest = sha1::chksum(file)?;
assert_eq!(
    digest.to_hex_lowercase(),
    "9fc42adac31303d68b444e6129f13f6093a0e045"
);

§Directory

use std::fs::read_dir;

use chksum_sha1 as sha1;

let readdir = read_dir(path)?;
let digest = sha1::chksum(readdir)?;
assert_eq!(
    digest.to_hex_lowercase(),
    "9fc42adac31303d68b444e6129f13f6093a0e045"
);

§Path

use std::path::PathBuf;

use chksum_sha1 as sha1;

let path = PathBuf::from(path);
let digest = sha1::chksum(path)?;
assert_eq!(
    digest.to_hex_lowercase(),
    "9fc42adac31303d68b444e6129f13f6093a0e045"
);

§Standard Input

use std::io::stdin;

use chksum_sha1 as sha1;

let stdin = stdin();
let digest = sha1::chksum(stdin)?;
assert_eq!(
    digest.to_hex_lowercase(),
    "9fc42adac31303d68b444e6129f13f6093a0e045"
);

§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-sha1 = { version = "0.1.0", features = ["reader", "writer"] }

Alternatively, you can use the cargo add subcommand:

cargo add chksum-sha1 --features reader,writer

§Asynchronous Runtime

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

By default, neither of these features is enabled.

§Disclaimer

The SHA-1 hash function should be used only for backward compatibility due to security issues.

Check RFC 6194: Security Considerations for the SHA-0 and SHA-1 Message-Digest Algorithms for more details.

§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_sha1 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.
SHA1
The SHA-1 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 SHA1 hash algorithm.
AsyncWriterwriter and async-runtime-tokio
A specialized AsyncWriter type with the SHA1 hash algorithm.
Readerreader
A specialized Reader type with the SHA1 hash algorithm.
Writerwriter
A specialized Writer type with the SHA1 hash algorithm.