From 5a0d3acd041f229e0b9d48c35682af4b94b9affb Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 13 May 2015 15:28:09 -0400 Subject: also allow padding sha1 with a user-specified length --- src/lib.rs | 1 + src/sha1.rs | 16 +++++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 112f968..2252129 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,6 +29,7 @@ pub use random::MersenneTwister; pub use random::mt19937_stream_cipher; pub use sha1::sha1; pub use sha1::pad_sha1; +pub use sha1::sha1_padding; pub use sha1::sha1_with_state; pub use sha1::sha1_mac; pub use crack::BlockCipherMode; diff --git a/src/sha1.rs b/src/sha1.rs index bb6d82c..bcd9295 100644 --- a/src/sha1.rs +++ b/src/sha1.rs @@ -14,16 +14,22 @@ pub fn sha1 (bytes: &[u8]) -> [u8; 20] { } pub fn pad_sha1 (bytes: &[u8]) -> Vec { - let ml: u64 = bytes.len() as u64 * 8; + return bytes + .iter() + .map(|x| *x) + .chain(sha1_padding(bytes.len() as u64)) + .collect(); +} + +pub fn sha1_padding (len: u64) -> Vec { + let ml: u64 = len * 8; let ml_bytes: [u8; 8] = unsafe { ::std::mem::transmute(ml.to_be()) }; - - return bytes + return [0x80u8] .iter() .map(|x| *x) - .chain(::std::iter::repeat(0x80).take(1)) - .chain(::std::iter::repeat(0x00).take(55 - (bytes.len() % 64))) + .chain(::std::iter::repeat(0x00).take(55 - (len % 64) as usize)) .chain(ml_bytes.iter().map(|x| *x)) .collect(); } -- cgit v1.2.3-54-g00ecf