1 unstable release

Uses new Rust 2021

0.1.0 Sep 29, 2022

#389 in Cryptography


471 lines


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:

fingerprint-struct = "0.1.0"

or run:

cargo add fingerprint-struct


Hashing a string

use blake2::Blake2b512;
use fingerprint_struct::fingerprint;
use hex::ToHex;

let hash = fingerprint::<Blake2b512>("Hello world!");
let hash: String = hash.encode_hex_upper();

Hashing a custom data structure

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();

no_std support

This crate supports no_std environments. Simply disable the default std feature:

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:

fingerprint-struct = { version = "0.1.0", default-features = false, features = ["alloc", "derive"] }