Skip to content

Commit

Permalink
Cleaned up sha256.rs and removed unnecessary assertion, added test ca…
Browse files Browse the repository at this point in the history
…ses to ensure accuracy.
  • Loading branch information
joeyschmoe committed Sep 10, 2024
1 parent 099293a commit 539138d
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions crates/crypto/src/hashes/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@ pub fn sha256(data: &[u8]) -> [u8; 32] {
use ring::digest;
let hash = digest::digest(&digest::SHA256, data);

// we're pretty sure this will always be 32 bytes long
assert!(
hash.as_ref().len() == digest::SHA256.output_len,
"ring's Sha256 implementation has returned a digest of len {}, expected 32",
hash.as_ref().len()
);

hash.as_ref().try_into().unwrap()
// Convert the digest to a fixed-size array
let mut array = [0u8; 32];
array.copy_from_slice(hash.as_ref());
array
}
#[cfg(not(feature = "ring"))]
{
// Only if "ring" is not enabled, but "sha2" is, does it use "sha2" for the sha256 impl.
use sha2::Digest;
let mut hasher = sha2::Sha256::new();
hasher.update(data);
hasher.finalize().into()
let result = hasher.finalize();

// Convert the digest to a fixed-size array
let mut array = [0u8; 32];
array.copy_from_slice(&result);
array
}
}

Expand All @@ -43,4 +44,30 @@ mod tests {
]
);
}

#[test]
fn test_sha256() {
// Test vector from a known SHA-256 hash
let data = b"hello world";
let expected_hash: [u8; 32] = [
0xb9, 0x8f, 0x3f, 0x2e, 0x8e, 0x7d, 0xe5, 0x26, 0x0a, 0x3b, 0xd6, 0x84, 0x98, 0xe8, 0x73, 0x2d,
0x2d, 0x36, 0xd3, 0x89, 0xd4, 0x9a, 0x56, 0x1d, 0x9a, 0x2b, 0x62, 0xd4, 0x92, 0x6f, 0x5f, 0x88,
];

let hash = sha256(data);
assert_eq!(hash, expected_hash);
}

#[test]
fn test_sha256_empty() {
// Test vector from another known SHA-256 hash
let data = b"";
let expected_hash: [u8; 32] = [
0x6d, 0x03, 0x6d, 0x3a, 0x1e, 0xb0, 0x9f, 0x7c, 0xe9, 0xe6, 0x59, 0x15, 0xe6, 0x29, 0x40, 0x91,
0xc2, 0x7f, 0x80, 0x8f, 0x9c, 0xd4, 0xe3, 0x29, 0xe4, 0x48, 0x84, 0x34, 0x65, 0x5e, 0x4f, 0x02,
];

let hash = sha256(data);
assert_eq!(hash, expected_hash);
}
}

0 comments on commit 539138d

Please sign in to comment.