Add sha256 hash and ordering.

This commit is contained in:
Michael Mikovsky
2026-05-31 13:22:02 -06:00
parent 0b11f8609e
commit b2e2523860
7 changed files with 234 additions and 67 deletions
+54
View File
@@ -0,0 +1,54 @@
use alloc::string::String;
mod hash;
mod ordering;
pub use hash::sha256;
pub use ordering::feistel_shuffle;
#[macro_export]
macro_rules! hash_256 {
($s:literal) => {{
// string literal arm
const HASH: [u8; 32] = $crate::crypto::sha256($s.as_bytes());
HASH
}};
($n:expr) => {{
// integer/expression arm
const BYTES: [u8; 8] = ($n as u64).to_be_bytes();
const HASH: [u8; 32] = $crate::crypto::sha256(&BYTES);
HASH
}};
}
#[macro_export]
macro_rules! hash_32 {
($s:literal) => {{
// string literal arm
const HASH: [u8; 32] = $crate::crypto::sha256($s.as_bytes());
const RESULT: u32 = u32::from_be_bytes([HASH[0], HASH[8], HASH[16], HASH[24]]);
RESULT
}};
($n:expr) => {{
// integer/expression arm
const BYTES: [u8; 8] = ($n as u64).to_be_bytes();
const HASH: [u8; 32] = $crate::crypto::sha256(&BYTES);
const RESULT: u32 = u32::from_be_bytes([HASH[0], HASH[8], HASH[16], HASH[24]]);
RESULT
}};
}
pub fn hash_string_32(input: String) -> u32 {
let hash: [u8; 32] = sha256(input.as_bytes());
u32::from_be_bytes([hash[0], hash[8], hash[16], hash[24]])
}
pub fn hash_str_32(input: &str) -> u32 {
let hash: [u8; 32] = sha256(input.as_bytes());
u32::from_be_bytes([hash[0], hash[8], hash[16], hash[24]])
}
pub fn hash_32(input: u32) -> u32 {
let hash: [u8; 32] = sha256(&input.to_be_bytes());
u32::from_be_bytes([hash[0], hash[8], hash[16], hash[24]])
}