5 releases

0.2.2 Oct 13, 2023
0.2.1 Sep 2, 2023
0.2.0 Sep 1, 2023
0.1.0 Aug 26, 2023

#1056 in Cryptography

MIT/Apache

46KB
783 lines

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
}

Dependencies

~350–660KB
~14K SLoC