#sha-256

simple-hash

A simple interface for hashing rust values

2 releases

0.1.1 May 29, 2022
0.1.0 May 29, 2022

#58 in #sha-256

MIT license

6KB
120 lines

A simple interface for hashing rust values

This crates defines two traits: [Hasher] and [Hashable].

The first represents an hashing algorithm and state, and is currently implemented for sha2::Sha256.

The second is implemented for any rust value that needs to be hashed. An Helper derive macro with the same name is provided to avoid boilerplate.

The current set of std types that implement [Hashable] is limited. PRs are welcome.

Example use:

use simple_hash::Hashable;
#[derive(Hashable)]
struct Foo {
    a: u8,
    b: u16,
    c: Vec<u32>,
}
let foo = Foo {
    a: 8,
    b: 99,
    c: vec![0,1,2,3],
};
let res = foo.digest::<sha2::Sha256>();
assert_eq!(res, hex_literal::hex!("929863ce588951eae0cc88755216f96951d431e7d15adbb836d8f1960bb65a9d"));

Dependencies

~2MB
~49K SLoC