summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2015-05-13 15:28:09 -0400
committerJesse Luehrs <doy@tozt.net>2015-05-13 15:43:23 -0400
commit5a0d3acd041f229e0b9d48c35682af4b94b9affb (patch)
tree37b5e271778cec6dad4aad9defe0e508b16193a4
parenta6385b210242b32c071e874a9662165468710b87 (diff)
downloadmatasano-5a0d3acd041f229e0b9d48c35682af4b94b9affb.tar.gz
matasano-5a0d3acd041f229e0b9d48c35682af4b94b9affb.zip
also allow padding sha1 with a user-specified length
-rw-r--r--src/lib.rs1
-rw-r--r--src/sha1.rs16
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<u8> {
- 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<u8> {
+ 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();
}