-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb0c121
commit 10e8734
Showing
8 changed files
with
249 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use std::fmt::Display; | ||
|
||
use strum::EnumIter; | ||
use utils::byte_formatting::ByteFormat; | ||
|
||
use crate::traits::ClassicHasher; | ||
|
||
use super::{ | ||
ripemd0::RipeMd0, ripemd128::RipeMd128, ripemd160::RipeMd160, ripemd256::RipeMd256, | ||
ripemd320::RipeMd320, | ||
}; | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq, EnumIter)] | ||
pub enum RipeMdVariant { | ||
Md0, | ||
Md128, | ||
Md160, | ||
Md256, | ||
Md320, | ||
} | ||
|
||
impl Display for RipeMdVariant { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
RipeMdVariant::Md0 => write!(f, "RIPEMD (original)"), | ||
RipeMdVariant::Md128 => write!(f, "RIPEMD-128"), | ||
RipeMdVariant::Md160 => write!(f, "RIPEMD-160"), | ||
RipeMdVariant::Md256 => write!(f, "RIPEMD-256"), | ||
RipeMdVariant::Md320 => write!(f, "RIPEMD-320"), | ||
} | ||
} | ||
} | ||
|
||
impl RipeMdVariant { | ||
pub fn hasher(&self) -> Box<dyn ClassicHasher> { | ||
match self { | ||
RipeMdVariant::Md0 => Box::new(RipeMd0::default()), | ||
RipeMdVariant::Md128 => Box::new(RipeMd128::default()), | ||
RipeMdVariant::Md160 => Box::new(RipeMd160::default()), | ||
RipeMdVariant::Md256 => Box::new(RipeMd256::default()), | ||
RipeMdVariant::Md320 => Box::new(RipeMd320::default()), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct RipeMd { | ||
pub input_format: ByteFormat, | ||
pub output_format: ByteFormat, | ||
pub variant: RipeMdVariant, | ||
} | ||
|
||
impl Default for RipeMd { | ||
fn default() -> Self { | ||
Self { | ||
input_format: ByteFormat::Utf8, | ||
output_format: ByteFormat::Hex, | ||
variant: RipeMdVariant::Md160, | ||
} | ||
} | ||
} | ||
|
||
impl ClassicHasher for RipeMd { | ||
fn hash(&self, bytes: &[u8]) -> Vec<u8> { | ||
self.variant.hasher().hash(bytes) | ||
} | ||
|
||
crate::hash_bytes_from_string! {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
use crate::traits::ClassicHasher; | ||
use utils::{byte_formatting::ByteFormat, padding::md_strengthening_64_le}; | ||
|
||
// Similiar but not identical to the strengthened versions | ||
pub const PERM: [usize; 48] = [ | ||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, | ||
14, 2, 11, 8, 3, 10, 2, 4, 9, 15, 8, 1, 14, 7, 0, 6, 11, 13, 5, 12, | ||
]; | ||
|
||
// Similiar but not identical to the strengthened versions | ||
pub const ROL: [u32; 48] = [ | ||
11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8, 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, | ||
9, 7, 11, 13, 12, 11, 13, 14, 7, 14, 9, 13, 15, 6, 8, 13, 6, 12, 5, 7, 5, | ||
]; | ||
|
||
// Selectable boolean function | ||
fn f(j: usize, x: u32, y: u32, z: u32) -> u32 { | ||
match j / 16 { | ||
0 => (x & y) | (!x & z), | ||
1 => (x & y) | (x & z) | (y & z), | ||
2 => x ^ y ^ z, | ||
_ => unreachable!(), | ||
} | ||
} | ||
|
||
fn ff(j: usize, s: &mut [u32; 4], block: [u32; 16], k: u32) { | ||
s[0] = (s[0] | ||
.wrapping_add(f(j, s[1], s[2], s[3])) | ||
.wrapping_add(block[PERM[j]].wrapping_add(k))) | ||
.rotate_left(ROL[j]); | ||
s.rotate_right(1); | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct RipeMd0 { | ||
pub input_format: ByteFormat, | ||
pub output_format: ByteFormat, | ||
} | ||
|
||
impl Default for RipeMd0 { | ||
fn default() -> Self { | ||
Self { | ||
input_format: ByteFormat::Utf8, | ||
output_format: ByteFormat::Hex, | ||
} | ||
} | ||
} | ||
|
||
impl RipeMd0 { | ||
pub const K: [u32; 4] = [0x50a28be6, 0x5a827999, 0x6ed9eba1, 0x5c4dd124]; | ||
|
||
pub fn input(mut self, input: ByteFormat) -> Self { | ||
self.input_format = input; | ||
self | ||
} | ||
|
||
pub fn output(mut self, output: ByteFormat) -> Self { | ||
self.output_format = output; | ||
self | ||
} | ||
|
||
pub fn compress(state: &mut [u32; 4], block: [u32; 16]) { | ||
let mut l = state.clone(); | ||
let mut r = state.clone(); | ||
|
||
for j in 0..16 { | ||
ff(j, &mut l, block, 0); | ||
ff(j, &mut r, block, Self::K[0]); | ||
} | ||
for j in 0..16 { | ||
ff(j + 16, &mut l, block, Self::K[1]); | ||
ff(j + 16, &mut r, block, 0); | ||
} | ||
for j in 0..16 { | ||
ff(j + 32, &mut l, block, Self::K[2]); | ||
ff(j + 32, &mut r, block, Self::K[3]); | ||
} | ||
|
||
let t = state[1].wrapping_add(l[2]).wrapping_add(r[3]); | ||
state[1] = state[2].wrapping_add(l[3]).wrapping_add(r[0]); | ||
state[2] = state[3].wrapping_add(l[0]).wrapping_add(r[1]); | ||
state[3] = state[0].wrapping_add(l[1]).wrapping_add(r[2]); | ||
state[0] = t; | ||
} | ||
} | ||
|
||
impl ClassicHasher for RipeMd0 { | ||
fn hash(&self, bytes: &[u8]) -> Vec<u8> { | ||
let mut input = bytes.to_vec(); | ||
|
||
md_strengthening_64_le(&mut input, 64); | ||
|
||
let mut state: [u32; 4] = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476]; | ||
|
||
for chunk in input.chunks_exact(64) { | ||
let mut block = [0u32; 16]; | ||
for (elem, b) in block.iter_mut().zip(chunk.chunks_exact(4)) { | ||
*elem = u32::from_le_bytes(b.try_into().unwrap()); | ||
} | ||
Self::compress(&mut state, block) | ||
} | ||
|
||
let mut out = Vec::with_capacity(16); | ||
for word in state { | ||
out.extend(word.to_le_bytes()) | ||
} | ||
out | ||
} | ||
|
||
crate::hash_bytes_from_string! {} | ||
} | ||
|
||
crate::basic_hash_tests!( | ||
RipeMd0::default(), test_0_1, "", | ||
"9f73aa9b372a9dacfb86a6108852e2d9"; | ||
RipeMd0::default(), test_0_2, "a", | ||
"486f74f790bc95ef7963cd2382b4bbc9"; | ||
RipeMd0::default(), test_0_3, "abc", | ||
"3f14bad4c2f9b0ea805e5485d3d6882d"; | ||
RipeMd0::default(), test_0_4, "12345678901234567890123456789012345678901234567890123456789012345678901234567890", | ||
"dfd6b45f60fe79bbbde87c6bfc6580a5"; | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.