Hash: convert to hex string in a simpler way.
This commit is contained in:
parent
e81beb733e
commit
776438e866
|
@ -16,8 +16,8 @@
|
|||
//! unit tests on a big-endian platform can verify.
|
||||
|
||||
const BLOCK_SIZE: usize = 256 / 8; // Block size of the hash, in bytes.
|
||||
const UPDATE_MIX_ROUNDS: usize = 3; // Number of mix rounds after each block of data is added.
|
||||
const FINISH_MIX_ROUNDS: usize = 6; // Number of mix rounds used to finalize the hash.
|
||||
const UPDATE_MIX_ROUNDS: usize = 6; // Number of mix rounds after each block of data is added.
|
||||
const FINISH_MIX_ROUNDS: usize = 12; // Number of mix rounds used to finalize the hash.
|
||||
|
||||
/// A hasher. Consumes bytes and generates a 256-bit hash.
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
|
@ -90,6 +90,38 @@ impl LedHash256 {
|
|||
}
|
||||
}
|
||||
|
||||
/// Returns the printable hex string version of a digest.
|
||||
pub fn digest_to_string(digest: &[u8]) -> String {
|
||||
fn low_bits_to_char(n: u8) -> char {
|
||||
match n {
|
||||
0 => '0',
|
||||
1 => '1',
|
||||
2 => '2',
|
||||
3 => '3',
|
||||
4 => '4',
|
||||
5 => '5',
|
||||
6 => '6',
|
||||
7 => '7',
|
||||
8 => '8',
|
||||
9 => '9',
|
||||
10 => 'a',
|
||||
11 => 'b',
|
||||
12 => 'c',
|
||||
13 => 'd',
|
||||
14 => 'e',
|
||||
15 => 'f',
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
let mut s = String::new();
|
||||
for byte in digest.iter() {
|
||||
s.push(low_bits_to_char(byte >> 4u8));
|
||||
s.push(low_bits_to_char(byte & 0b00001111));
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
/// Adds message data to the hash state.
|
||||
///
|
||||
/// The data must be at least 32 bytes long. Only the first 32 bytes
|
||||
|
@ -129,7 +161,8 @@ fn mix_state(state: &mut [u64; 4], rounds: usize) {
|
|||
[58, 22, 32, 32],
|
||||
];
|
||||
|
||||
for round in 0..rounds {
|
||||
debug_assert!(rounds % 2 == 0);
|
||||
for round in 0..(rounds / 2) {
|
||||
let rot = ROTATIONS[round % ROTATIONS.len()];
|
||||
|
||||
// MIX function.
|
||||
|
@ -159,88 +192,44 @@ mod test {
|
|||
h.finish()
|
||||
}
|
||||
|
||||
fn digest_to_string(digest: [u8; 32]) -> String {
|
||||
fn low_bits_to_char(n: u8) -> char {
|
||||
match n {
|
||||
0 => '0',
|
||||
1 => '1',
|
||||
2 => '2',
|
||||
3 => '3',
|
||||
4 => '4',
|
||||
5 => '5',
|
||||
6 => '6',
|
||||
7 => '7',
|
||||
8 => '8',
|
||||
9 => '9',
|
||||
10 => 'a',
|
||||
11 => 'b',
|
||||
12 => 'c',
|
||||
13 => 'd',
|
||||
14 => 'e',
|
||||
15 => 'f',
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
let mut s = String::new();
|
||||
for byte in digest.iter().rev() {
|
||||
s.push(low_bits_to_char(byte >> 4u8));
|
||||
s.push(low_bits_to_char(byte & 0b00001111));
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hash_empty() {
|
||||
let correct_digest = "0000000000000000000000000000000000000000000000000000000000000000";
|
||||
assert_eq!(digest_to_string(hash(&[])), correct_digest);
|
||||
assert_eq!(digest_to_string(&hash(&[])), correct_digest);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hash_zero() {
|
||||
let correct_digest = "8311a9b6f5e6f4d31707eec6bab81a406db32289be10d2dd93b2b3c0cf6ddac3";
|
||||
assert_eq!(digest_to_string(hash(&[0u8])), correct_digest);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hash_one() {
|
||||
let correct_digest = "c111cf6d91fdbc240c34662af28b3425dfe137f1107a65a40ac0b1bc490e1433";
|
||||
assert_eq!(digest_to_string(hash(&[1u8])), correct_digest);
|
||||
let correct_digest = "c3da6dcfc0b3b293ddd210be8922b36d401ab8bac6ee0717d3f4e6f5b6a91183";
|
||||
assert_eq!(digest_to_string(&hash(&[0u8])), correct_digest);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hash_string_01() {
|
||||
let s = "abc";
|
||||
let correct_digest = "44b8f371ded8bd64a91302da43f0f69117b3795e12a2cd079049d70f675bf19e";
|
||||
assert_eq!(digest_to_string(hash(s.as_bytes())), correct_digest);
|
||||
let s = "0123456789";
|
||||
let correct_digest = "07ee14811136870400e801bcf2790f5c560d2d76fcc8bf83e4c33facb024ff3f";
|
||||
assert_eq!(digest_to_string(&hash(s.as_bytes())), correct_digest);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hash_string_02() {
|
||||
let s = "abcdefghijklmnopqrstuvwxyz";
|
||||
let correct_digest = "dc59a6c0f41de0fbe405de9947fda7bab362b4fff00b02f0cda56d47a42dbfa8";
|
||||
assert_eq!(digest_to_string(hash(s.as_bytes())), correct_digest);
|
||||
let correct_digest = "a8bf2da4476da5cdf0020bf0ffb462b3baa7fd4799de05e4fbe01df4c0a659dc";
|
||||
assert_eq!(digest_to_string(&hash(s.as_bytes())), correct_digest);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hash_string_03() {
|
||||
let s = "The quick brown fox jumps over the lazy dog.";
|
||||
let correct_digest = "7f69ce128ed82ed4bc8b8993b52555cf7e960669a8fd141b590b9e3d6e545b53";
|
||||
assert_eq!(digest_to_string(hash(s.as_bytes())), correct_digest);
|
||||
let correct_digest = "535b546e3d9e0b591b14fda86906967ecf5525b593898bbcd42ed88e12ce697f";
|
||||
assert_eq!(digest_to_string(&hash(s.as_bytes())), correct_digest);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hash_string_04() {
|
||||
let s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
let correct_digest = "1f8676c857099cf4fad280a6a9305f372e0d75341e52aa2b9cb3906324f9a4c7";
|
||||
assert_eq!(digest_to_string(hash(s.as_bytes())), correct_digest);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hash_string_05() {
|
||||
let s = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
|
||||
let correct_digest = "61675e48dd173196dc348114ef5dd1f38a04969e6436e5538d6da4ab0e6ab7cc";
|
||||
assert_eq!(digest_to_string(hash(s.as_bytes())), correct_digest);
|
||||
let correct_digest = "ccb76a0eaba46d8d53e536649e96048af3d15def148134dc963117dd485e6761";
|
||||
assert_eq!(digest_to_string(&hash(s.as_bytes())), correct_digest);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -252,7 +241,7 @@ mod test {
|
|||
let test_string4 = "cup";
|
||||
let test_string5 =
|
||||
"idatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
|
||||
let correct_digest = "61675e48dd173196dc348114ef5dd1f38a04969e6436e5538d6da4ab0e6ab7cc";
|
||||
let correct_digest = "ccb76a0eaba46d8d53e536649e96048af3d15def148134dc963117dd485e6761";
|
||||
|
||||
let mut hasher = LedHash256::new();
|
||||
hasher.update(test_string1.as_bytes());
|
||||
|
@ -262,7 +251,7 @@ mod test {
|
|||
hasher.update(test_string5.as_bytes());
|
||||
let digest = hasher.finish();
|
||||
|
||||
assert_eq!(digest_to_string(digest), correct_digest);
|
||||
assert_eq!(digest_to_string(&digest), correct_digest);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -276,16 +265,16 @@ mod test {
|
|||
let len_2 = &[0u8, 0];
|
||||
|
||||
assert_eq!(
|
||||
digest_to_string(hash(len_0)),
|
||||
digest_to_string(&hash(len_0)),
|
||||
"0000000000000000000000000000000000000000000000000000000000000000",
|
||||
);
|
||||
assert_eq!(
|
||||
digest_to_string(hash(len_1)),
|
||||
"8311a9b6f5e6f4d31707eec6bab81a406db32289be10d2dd93b2b3c0cf6ddac3",
|
||||
digest_to_string(&hash(len_1)),
|
||||
"c3da6dcfc0b3b293ddd210be8922b36d401ab8bac6ee0717d3f4e6f5b6a91183",
|
||||
);
|
||||
assert_eq!(
|
||||
digest_to_string(hash(len_2)),
|
||||
"20dafb63358a6a56d7b185e0c15093787397c551c8c90afc0bb5ee099aacb668",
|
||||
digest_to_string(&hash(len_2)),
|
||||
"68b6ac9a09eeb50bfc0ac9c851c59773789350c1e085b1d7566a8a3563fbda20",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user