From a6385b210242b32c071e874a9662165468710b87 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 13 May 2015 12:25:11 -0400 Subject: refactor sha1 to allow passing in an initial state --- src/lib.rs | 2 ++ src/sha1.rs | 26 ++++++++++++++++---------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8452240..112f968 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,6 +28,8 @@ pub use primitives::repeating_key_xor; pub use random::MersenneTwister; pub use random::mt19937_stream_cipher; pub use sha1::sha1; +pub use sha1::pad_sha1; +pub use sha1::sha1_with_state; pub use sha1::sha1_mac; pub use crack::BlockCipherMode; pub use crack::find_aes_128_ecb_encrypted_string; diff --git a/src/sha1.rs b/src/sha1.rs index 3e97478..bb6d82c 100644 --- a/src/sha1.rs +++ b/src/sha1.rs @@ -1,29 +1,35 @@ #[cfg(test)] use serialize::hex::ToHex; pub fn sha1 (bytes: &[u8]) -> [u8; 20] { - let mut h: [u32; 5] = [ - 0x67452301, - 0xEFCDAB89, - 0x98BADCFE, - 0x10325476, - 0xC3D2E1F0, - ]; + sha1_with_state( + bytes, + [ + 0x67452301, + 0xEFCDAB89, + 0x98BADCFE, + 0x10325476, + 0xC3D2E1F0, + ] + ) +} +pub fn pad_sha1 (bytes: &[u8]) -> Vec { let ml: u64 = bytes.len() as u64 * 8; let ml_bytes: [u8; 8] = unsafe { ::std::mem::transmute(ml.to_be()) }; - let message: Vec = bytes + return bytes .iter() .map(|x| *x) .chain(::std::iter::repeat(0x80).take(1)) .chain(::std::iter::repeat(0x00).take(55 - (bytes.len() % 64))) .chain(ml_bytes.iter().map(|x| *x)) .collect(); - assert!(message.len() % 64 == 0); +} - for chunk in message.chunks(64) { +pub fn sha1_with_state (bytes: &[u8], mut h: [u32; 5]) -> [u8; 20] { + for chunk in pad_sha1(bytes).chunks(64) { let chunk_words: &[u32; 16] = unsafe { ::std::mem::transmute(chunk.as_ptr()) }; -- cgit v1.2.3-54-g00ecf