Skip to content

Commit

Permalink
Implement ContentHash for u32 and u64
Browse files Browse the repository at this point in the history
This is for completeness and to avoid accidents such as someone calling
`ContentHash::hash(1234u32.to_le_bytes())` and expecting it to hash properly as
a u32 instead of a 4 byte slice, which produces a different hash due to hashing
the length of the slice before its contents.
  • Loading branch information
emesterhazy committed Feb 16, 2024
1 parent e1fd402 commit a80d018
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/src/content_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,24 @@ impl ContentHash for u8 {
}
}

impl ContentHash for u32 {
fn hash(&self, state: &mut impl digest::Update) {
state.update(&self.to_le_bytes());
}
}

impl ContentHash for i32 {
fn hash(&self, state: &mut impl digest::Update) {
state.update(&self.to_le_bytes());
}
}

impl ContentHash for u64 {
fn hash(&self, state: &mut impl digest::Update) {
state.update(&self.to_le_bytes());
}
}

impl ContentHash for i64 {
fn hash(&self, state: &mut impl digest::Update) {
state.update(&self.to_le_bytes());
Expand Down

0 comments on commit a80d018

Please sign in to comment.