summaryrefslogtreecommitdiffstats
path: root/src/sha1.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/sha1.rs')
-rw-r--r--src/sha1.rs26
1 files changed, 16 insertions, 10 deletions
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())
};