Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add common hash sizes From impls #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions blake2b/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,40 @@ fn bytes_to_hex(bytes: &[u8]) -> HexString {
s
}

impl From<[u8; 16]> for Hash {
fn from(bytes: [u8; 16]) -> Self {
let mut buf = [0u8; OUTBYTES];
buf[0..16].copy_from_slice(&bytes);
Self {
bytes: buf,
len: 16 as u8,
}
}
}

impl From<&[u8; 16]> for Hash {
fn from(bytes: &[u8; 16]) -> Self {
Self::from(*bytes)
}
}

impl From<[u8; 32]> for Hash {
fn from(bytes: [u8; 32]) -> Self {
let mut buf = [0u8; OUTBYTES];
buf[0..32].copy_from_slice(&bytes);
Self {
bytes: buf,
len: 32 as u8,
}
}
}

impl From<&[u8; 32]> for Hash {
fn from(bytes: &[u8; 32]) -> Self {
Self::from(*bytes)
}
}

impl From<[u8; OUTBYTES]> for Hash {
fn from(bytes: [u8; OUTBYTES]) -> Self {
Self {
Expand Down
20 changes: 20 additions & 0 deletions blake2b/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,24 @@ fn test_hash_from() {
let h = blake2b(b"foo");
assert_eq!(h, Hash::from(h.as_array()));
assert_eq!(h, Hash::from(*h.as_array()));

let h = crate::Params::new()
.hash_length(16)
.to_state()
.update(b"foo")
.finalize();
let mut h_s = [0u8; 16];
h_s[0..16].copy_from_slice(h.as_bytes());
assert_eq!(h, Hash::from(&h_s));
assert_eq!(h, Hash::from(h_s));

let h = crate::Params::new()
.hash_length(32)
.to_state()
.update(b"foo")
.finalize();
let mut h_s = [0u8; 32];
h_s[0..32].copy_from_slice(h.as_bytes());
assert_eq!(h, Hash::from(&h_s));
assert_eq!(h, Hash::from(h_s));
}
17 changes: 17 additions & 0 deletions blake2s/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,23 @@ fn bytes_to_hex(bytes: &[u8]) -> HexString {
s
}

impl From<[u8; 16]> for Hash {
fn from(bytes: [u8; 16]) -> Self {
let mut buf = [0u8; OUTBYTES];
buf[0..16].copy_from_slice(&bytes);
Self {
bytes: buf,
len: 16 as u8,
}
}
}

impl From<&[u8; 16]> for Hash {
fn from(bytes: &[u8; 16]) -> Self {
Self::from(*bytes)
}
}

impl From<[u8; OUTBYTES]> for Hash {
fn from(bytes: [u8; OUTBYTES]) -> Self {
Self {
Expand Down
10 changes: 10 additions & 0 deletions blake2s/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,14 @@ fn test_hash_from() {
let h = blake2s(b"foo");
assert_eq!(h, Hash::from(h.as_array()));
assert_eq!(h, Hash::from(*h.as_array()));

let h = crate::Params::new()
.hash_length(16)
.to_state()
.update(b"foo")
.finalize();
let mut h_s = [0u8; 16];
h_s[0..16].copy_from_slice(h.as_bytes());
assert_eq!(h, Hash::from(&h_s));
assert_eq!(h, Hash::from(h_s));
}