digestible

Crates.iodigestible
lib.rsdigestible
version0.2.2
sourcesrc
created_at2023-08-26 16:09:25.727185
updated_at2023-10-13 12:21:55.879243
descriptionA more dynamic Hash and Hasher trait for Rust
homepage
repositoryhttps://github.com/wyatt-herkamp/digestible
max_upload_size
id955596
size47,764
Wyatt Herkamp (wyatt-herkamp)

documentation

https://docs.rs/digestible

README

Digestible Build Status Latest Version

A more dynamic Hash and Hasher trait for Rust


Key Difference over The Rust Hash and Hasher Traits

  • ByteOrder is built in. So you can digest number types in any byte order. ByteOrder
  • Output is a Generic Associated Type. So you can Digest into a ByteArray, String with Base64 or any type the Digester uses.
  • Skip Fields with #[digestible(skip)]
  • 'digest_with' and 'with' to override the default digest behavior. digest_with
  • Support for all Hashing Algorithms that implement digest::Digest such as SHA2, md-5, and many more.
  • Writing Type Headers to prevent collisions with similar types. (This is optional and can be disabled with #[digestible(type_header = none)])

Features

  • no_std Support
  • Digest to implement Digester for all types that implement digest::Digest
  • Float and Atomic Support using digest_with

Working with Hash

For types that do not Implement Digestible

you can add #[digestible(digest_with = digest_with_hash)] to your variable to tell Digestible to use Hash to digest it.

Implementing Hash with Digestible.

Adding #[digestible(hash)] to your struct or enum will implement Hash for it. Using the digest function. Allowing you to have a Hash and Digestible that are the same.

Example Using SHA2

#[derive(Digestible)]
pub struct MyStruct {
    pub id: u32,
    pub name: String,
    #[digestible(skip)]
    pub password: String,
}
fn digest_to_bytes(){
    let test = MyStruct{
        id: 0,
        name: "Test".to_string(),
        password: "Test".to_string(),
    };
    let mut hasher = sha2::Sha256::new();
    let result = hasher.digest_native(&test); // This will be the type of sha2 Output
}
fn digest_to_base64(){
    let test = MyStruct{
        id: 0,
        name: "Test".to_string(),
        password: "Test".to_string(),
    };
    let hasher = sha2::Sha256::new().into_base64();
    let result = hasher.digest_native(&test); // This is a base64 encoded string
}
Commit count: 28

cargo fmt