Crates.io | fingerprint-struct |
lib.rs | fingerprint-struct |
version | 0.1.0 |
source | src |
created_at | 2022-09-29 17:15:24.993639 |
updated_at | 2022-09-29 17:15:24.993639 |
description | Utilities for hashing data structutes |
homepage | |
repository | https://github.com/SvizelPritula/fingerprint-struct |
max_upload_size | |
id | 676787 |
size | 50,378 |
This crate allows for the computation of cryptographic hashes or arbitrary data structures.
It provides a Fingerprint
trait which represents a type whose hash can be computed. It's implemented by default for most common types from std
, such as primitives like u32
or bool
, collections like Vec
or BTreeSet
, pointers like Box
or Rc
or specialized types like IpAddress
. It also provides a derive macro which generates a Fingerprint
implementation for any struct or enum.
It relies on traits from the digest
crate, which means its compatible with all hash implementations from the Rust Crypto project.
Hashes are considered stable, changes to how a given data structure is hashed will cause a minor version bump. Note that making a change to your own type definitions might introduce hash collisions. To avoid this, you can include a version number in your data structures.
Add the following lines to Cargo.toml
:
[dependencies]
fingerprint-struct = "0.1.0"
or run:
cargo add fingerprint-struct
use blake2::Blake2b512;
use fingerprint_struct::fingerprint;
use hex::ToHex;
let hash = fingerprint::<Blake2b512>("Hello world!");
let hash: String = hash.encode_hex_upper();
println!("{hash}");
use blake2::Blake2b512;
use fingerprint_struct::{fingerprint, Fingerprint};
use hex::ToHex;
#[derive(Fingerprint, Default)]
struct Book {
title: String,
rating: f32,
authors: Vec<String>
}
let book = Book::default();
let hash = fingerprint::<Blake2b512>(book);
let hash: String = hash.encode_hex_upper();
println!("{hash}");
no_std
supportThis crate supports no_std
environments. Simply disable the default std
feature:
[dependencies]
fingerprint-struct = { version = "0.1.0", default-features = false, features = ["derive"] }
You can also optionally enable the alloc
feature on targets that don't support std
but support alloc
:
[dependencies]
fingerprint-struct = { version = "0.1.0", default-features = false, features = ["alloc", "derive"] }