Skip to content

Commit

Permalink
Remove tag, add support of all hash functions from digest
Browse files Browse the repository at this point in the history
  • Loading branch information
survived committed Jun 3, 2024
1 parent d253182 commit e752e12
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 93 deletions.
37 changes: 37 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
.PHONY: docs docs-open

docs:
RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --no-deps --all-features

docs-open:
RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --no-deps --all-features --open

docs-private:
RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --no-deps --all-features --document-private-items

readme:
cargo readme -i src/lib.rs -r udigest/ -t ../docs/readme.tpl \
| perl -ne 's/\[(.+?)\]\((?!https).+?\)/\1/g; print;' \
Expand Down
10 changes: 10 additions & 0 deletions cspell.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
words:
- digestable
- udigest
- VecDeque
- bytestring
- bytestrings
- biglen
- sublist
- docsrs
- concated
14 changes: 11 additions & 3 deletions udigest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,35 @@ readme = "../README.md"

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
digest = { version = "0.10", default-features = false }
digest = { version = "0.10", default-features = false, optional = true }

udigest-derive = { version = "0.1", optional = true }

[dev-dependencies]
sha2 = "0.10"
hex = "0.4"

sha2 = "0.10"
sha3 = "0.10"
blake2 = "0.10"

[features]
default = ["digest", "std"]

std = ["alloc"]
alloc = []
derive = ["dep:udigest-derive"]

digest = ["dep:digest"]

[[test]]
name = "derive"
required-features = ["std", "derive"]

[[example]]
name = "derivation"
required-features = ["std", "derive"]
required-features = ["std", "derive"]
6 changes: 1 addition & 5 deletions udigest/examples/derivation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use sha2::Sha256;
use udigest::udigest;

#[derive(udigest::Digestable)]
#[udigest(tag = "udigest.example.Person.v1")]
struct Person {
Expand All @@ -15,7 +12,6 @@ fn main() {
job_title: "cryptographer".into(),
};

let tag = udigest::Tag::<Sha256>::new("udigest.example");
let hash = udigest(tag, &person);
let hash = udigest::hash::<sha2::Sha256, _>(&person);
println!("{}", hex::encode(hash));
}
36 changes: 26 additions & 10 deletions udigest/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! The core of the crate is functionality to unambiguously encode any structured data
//! into bytes. It's then used to digest any data into a hash. Note that this module
//! provides low-level implementation details which you normally don't need to know
//! unless you manually implemenet [Digestable](crate::Digestable) trait.
//! unless you manually implement [Digestable](crate::Digestable) trait.
//!
//! Any structured `value` is encoded either as a bytestring or as a list (each element
//! within the list is either a bytestring or a list). The simplified grammar can be seen as
Expand All @@ -18,7 +18,7 @@
//! Encoding goal is to distinguish `["12", "3"]` from `["1", "23"]`, `["1", [], "2"]`
//! from `["1", "2"]` and so on. Now, we only need to map any structured data onto
//! that grammar to have an unambiguous encoding. Below, we will show how Rust structures
//! can be mapped onto the lists, and then we descrive how exactly encoding works.
//! can be mapped onto the lists, and then we describe how exactly encoding works.
//!
//! # Mapping Rust types onto lists
//!
Expand Down Expand Up @@ -188,9 +188,25 @@ pub trait Buffer {
fn write(&mut self, bytes: &[u8]);
}

impl<D: digest::Digest> Buffer for D {
/// Wraps [`digest::Digest`] and implements [`Buffer`]
#[cfg(feature = "digest")]
pub struct BufferDigest<D: digest::Digest>(pub D);

#[cfg(feature = "digest")]
impl<D: digest::Digest> Buffer for BufferDigest<D> {
fn write(&mut self, bytes: &[u8]) {
self.0.update(bytes)
}
}

/// Wraps [`digest::Update`] and implements [`Buffer`]
#[cfg(feature = "digest")]
pub struct BufferUpdate<D: digest::Update>(pub D);

#[cfg(feature = "digest")]
impl<D: digest::Update> Buffer for BufferUpdate<D> {
fn write(&mut self, bytes: &[u8]) {
self.update(bytes)
self.0.update(bytes)
}
}

Expand Down Expand Up @@ -312,15 +328,15 @@ impl<'b, B: Buffer> EncodeStruct<'b, B> {
self
}

/// Adds a fiels to the structure
/// Adds a fields to the structure
///
/// Returns an encoder that shall be used to encode the fiels value
/// Returns an encoder that shall be used to encode the fields value
pub fn add_field(&mut self, field_name: impl AsRef<[u8]>) -> EncodeValue<B> {
self.list.add_leaf().chain(field_name);
self.list.add_item()
}

/// Finilizes the encoding, puts the necessary metadata to the buffer
/// Finalizes the encoding, puts the necessary metadata to the buffer
///
/// It's an alias to dropping the encoder
pub fn finish(self) {}
Expand Down Expand Up @@ -377,7 +393,7 @@ impl<'b, B: Buffer> EncodeLeaf<'b, B> {
.expect("leaf length overflows `usize`")
}

/// Finilizes the encoding, puts the necessary metadata to the buffer
/// Finalizes the encoding, puts the necessary metadata to the buffer
///
/// It's an alias to dropping the encoder
pub fn finish(self) {}
Expand Down Expand Up @@ -452,7 +468,7 @@ impl<'b, B: Buffer> EncodeList<'b, B> {
self.add_item().encode_list()
}

/// Finilizes the encoding, puts the necessary metadata to the buffer
/// Finalizes the encoding, puts the necessary metadata to the buffer
///
/// It's an alias to dropping the encoder
pub fn finish(self) {}
Expand All @@ -475,7 +491,7 @@ impl<'b, B: Buffer> Drop for EncodeList<'b, B> {

/// Encodes length of list or leaf
///
/// Altough we expose how the length is encoded, normally you should use [EncodeList]
/// Although we expose how the length is encoded, normally you should use [EncodeList]
/// and [EncodeLeaf] which use this function internally
pub fn encode_len(buffer: &mut impl Buffer, len: usize) {
match u32::try_from(len) {
Expand Down
Loading

0 comments on commit e752e12

Please sign in to comment.