summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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();
}