#hashing #no-alloc #unambiguous-encoding

no-std udigest

Unambiguously digest structured data

1 unstable release

0.1.0 Nov 24, 2023

#1713 in Cryptography

Download history 14/week @ 2024-01-02 80/week @ 2024-01-09 105/week @ 2024-01-16 145/week @ 2024-01-23 145/week @ 2024-01-30 164/week @ 2024-02-06 337/week @ 2024-02-13 232/week @ 2024-02-20 250/week @ 2024-02-27 288/week @ 2024-03-05 260/week @ 2024-03-12 317/week @ 2024-03-19 283/week @ 2024-03-26 409/week @ 2024-04-02 153/week @ 2024-04-09 402/week @ 2024-04-16

1,320 downloads per month
Used in 5 crates

MIT/Apache

36KB
428 lines

Unambiguously digest structured data

udigest provides utilities for unambiguous hashing the structured data. Structured data can be anything that implements Digestable trait:

  • str, String, CStr, CString
  • Integers: i8, i16, i32, i64, i128, u8, u16, u32, u64, u128, char
  • Containers: Box, Arc, Rc, Cow, Option, Result
  • Collections: arrays, slices, Vec, LinkedList, VecDeque, BTreeSet, BTreeMap

The trait is intentionally not implemented for certain types:

  • HashMap, HashSet as they can not be traversed in determenistic order
  • usize, isize as their byte size varies on differnet platforms

The Digestable trait can be implemented for the struct using a macro:

use udigest::{Tag, udigest};
use sha2::Sha256;

#[derive(udigest::Digestable)]
struct Person {
    name: String,
    job_title: String,
}
let alice = &Person {
    name: "Alice".into(),
    job_title: "cryptographer".into(),
};

let tag = Tag::<Sha256>::new("udigest.example");
let hash = udigest(tag, &alice);

The crate intentionally does not try to follow any existing standards for unambiguous encoding. The format for encoding was desingned specifically for udigest to provide a better usage experience in Rust. The details of encoding format can be found in encoding module.

Features

  • std implements Digestable trait for types in standard library
  • alloc implements Digestable trait for type in alloc crate
  • derive enables Digestable proc macro

Dependencies

~265–445KB
~11K SLoC