Skip to content

feat: shoten hash function implementations - #45

Open
LVivona wants to merge 2 commits into
antouhou:masterfrom
LVivona:patch-1
Open

LVivona wants to merge 2 commits into
antouhou:masterfrom
LVivona:patch-1

Conversation

@LVivona

@LVivona LVivona commented Jul 26, 2025

Copy link
Copy Markdown

Summary

When importing the trait sha2::Digest, you have access to the digest function, which both shortens the code into one line and does not require a mutable instantiation. :)

Gist Changes

fn hash(data: &[u8]) -> [u8; 32] {
-          let mut hasher = Sha256::new();
-          hasher.update(data);
-         <[u8; 32]>::from(hasher.finalize_fixed())
+         <[u8; 32]>::from(Sha256::digest(data))
}
fn hash(data: &[u8]) -> [u8; 48] {
-          let mut hasher = Sha384::new();
-          hasher.update(data);
-         <[u8; 48]>::from(hasher.finalize_fixed())
+         <[u8; 48]>::from(Sha384::digest(data))
}

I also added tests below my commits to show that operations are equivalent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant