summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2015-05-13 12:25:11 -0400
committerJesse Luehrs <doy@tozt.net>2015-05-13 12:25:11 -0400
commita6385b210242b32c071e874a9662165468710b87 (patch)
tree94491ba0919a73c205344698fae22395c051125e
parentccfafce47eadb72cfebe18c8c9f3d627ef51aab2 (diff)
downloadmatasano-a6385b210242b32c071e874a9662165468710b87.tar.gz
matasano-a6385b210242b32c071e874a9662165468710b87.zip
refactor sha1 to allow passing in an initial state
-rw-r--r--src/lib.rs2
-rw-r--r--src/sha1.rs26
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<u8> {
let ml: u64 = bytes.len() as u64 * 8;
let ml_bytes: [u8; 8] = unsafe {
::std::mem::transmute(ml.to_be())
};
- let message: Vec<u8> = 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())
};