2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::str;
|
|
|
|
use core::uint;
|
|
|
|
use core::vec;
|
|
|
|
|
2013-02-04 18:48:52 -06:00
|
|
|
struct Quad {
|
|
|
|
a: u32,
|
|
|
|
b: u32,
|
|
|
|
c: u32,
|
|
|
|
d: u32
|
|
|
|
}
|
|
|
|
|
2013-05-28 22:11:41 -05:00
|
|
|
/// Calculates the md4 hash of the given slice of bytes, returning the 128-bit
|
|
|
|
/// result as a quad of u32's
|
2013-03-21 23:34:30 -05:00
|
|
|
pub fn md4(msg: &[u8]) -> Quad {
|
2012-01-20 09:43:39 -06:00
|
|
|
// subtle: if orig_len is merely uint, then the code below
|
|
|
|
// which performs shifts by 32 bits or more has undefined
|
|
|
|
// results.
|
2013-05-14 04:52:12 -05:00
|
|
|
let orig_len: u64 = (msg.len() * 8u) as u64;
|
2012-01-20 09:43:39 -06:00
|
|
|
|
2012-01-17 04:50:31 -06:00
|
|
|
// pad message
|
2013-05-19 00:07:44 -05:00
|
|
|
let mut msg = vec::append(vec::to_owned(msg), [0x80u8]);
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut bitlen = orig_len + 8u64;
|
2012-01-20 11:05:21 -06:00
|
|
|
while (bitlen + 64u64) % 512u64 > 0u64 {
|
2013-04-08 15:50:34 -05:00
|
|
|
msg.push(0u8);
|
2012-01-20 11:05:21 -06:00
|
|
|
bitlen += 8u64;
|
2012-01-17 04:50:31 -06:00
|
|
|
}
|
2012-01-20 09:43:39 -06:00
|
|
|
|
2012-01-17 04:50:31 -06:00
|
|
|
// append length
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut i = 0u64;
|
2012-01-20 11:05:21 -06:00
|
|
|
while i < 8u64 {
|
2013-04-08 15:50:34 -05:00
|
|
|
msg.push((orig_len >> (i * 8u64)) as u8);
|
2012-01-20 11:05:21 -06:00
|
|
|
i += 1u64;
|
2012-01-17 04:50:31 -06:00
|
|
|
}
|
|
|
|
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut a = 0x67452301u32;
|
|
|
|
let mut b = 0xefcdab89u32;
|
|
|
|
let mut c = 0x98badcfeu32;
|
|
|
|
let mut d = 0x10325476u32;
|
2012-01-17 04:50:31 -06:00
|
|
|
|
2013-03-21 23:34:30 -05:00
|
|
|
fn rot(r: int, x: u32) -> u32 {
|
2012-01-17 04:50:31 -06:00
|
|
|
let r = r as u32;
|
|
|
|
(x << r) | (x >> (32u32 - r))
|
|
|
|
}
|
|
|
|
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut i = 0u;
|
2013-05-14 04:52:12 -05:00
|
|
|
let e = msg.len();
|
2013-02-12 17:34:48 -06:00
|
|
|
let mut x = vec::from_elem(16u, 0u32);
|
2012-01-17 04:50:31 -06:00
|
|
|
while i < e {
|
2013-06-04 23:43:41 -05:00
|
|
|
let (aa, bb, cc, dd) = (a, b, c, d);
|
2012-01-17 04:50:31 -06:00
|
|
|
|
2013-06-04 23:43:41 -05:00
|
|
|
let mut (j, base) = (0u, i);
|
2012-01-17 04:50:31 -06:00
|
|
|
while j < 16u {
|
|
|
|
x[j] = (msg[base] as u32) + (msg[base + 1u] as u32 << 8u32) +
|
|
|
|
(msg[base + 2u] as u32 << 16u32) +
|
|
|
|
(msg[base + 3u] as u32 << 24u32);
|
|
|
|
j += 1u; base += 4u;
|
|
|
|
}
|
|
|
|
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut j = 0u;
|
2012-01-17 04:50:31 -06:00
|
|
|
while j < 16u {
|
|
|
|
a = rot(3, a + ((b & c) | (!b & d)) + x[j]);
|
|
|
|
j += 1u;
|
|
|
|
d = rot(7, d + ((a & b) | (!a & c)) + x[j]);
|
|
|
|
j += 1u;
|
|
|
|
c = rot(11, c + ((d & a) | (!d & b)) + x[j]);
|
|
|
|
j += 1u;
|
|
|
|
b = rot(19, b + ((c & d) | (!c & a)) + x[j]);
|
|
|
|
j += 1u;
|
|
|
|
}
|
|
|
|
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut j = 0u;
|
|
|
|
let q = 0x5a827999u32;
|
2012-01-17 04:50:31 -06:00
|
|
|
while j < 4u {
|
|
|
|
a = rot(3, a + ((b & c) | ((b & d) | (c & d))) + x[j] + q);
|
|
|
|
d = rot(5, d + ((a & b) | ((a & c) | (b & c))) + x[j + 4u] + q);
|
|
|
|
c = rot(9, c + ((d & a) | ((d & b) | (a & b))) + x[j + 8u] + q);
|
|
|
|
b = rot(13, b + ((c & d) | ((c & a) | (d & a))) + x[j + 12u] + q);
|
|
|
|
j += 1u;
|
|
|
|
}
|
|
|
|
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut j = 0u;
|
|
|
|
let q = 0x6ed9eba1u32;
|
2012-01-17 04:50:31 -06:00
|
|
|
while j < 8u {
|
2012-01-29 20:33:08 -06:00
|
|
|
let jj = if j > 2u { j - 3u } else { j };
|
2012-01-17 04:50:31 -06:00
|
|
|
a = rot(3, a + (b ^ c ^ d) + x[jj] + q);
|
|
|
|
d = rot(9, d + (a ^ b ^ c) + x[jj + 8u] + q);
|
|
|
|
c = rot(11, c + (d ^ a ^ b) + x[jj + 4u] + q);
|
|
|
|
b = rot(15, b + (c ^ d ^ a) + x[jj + 12u] + q);
|
|
|
|
j += 2u;
|
|
|
|
}
|
|
|
|
|
|
|
|
a += aa; b += bb; c += cc; d += dd;
|
|
|
|
i += 64u;
|
|
|
|
}
|
2013-02-04 18:48:52 -06:00
|
|
|
return Quad {a: a, b: b, c: c, d: d};
|
2012-01-17 04:50:31 -06:00
|
|
|
}
|
|
|
|
|
2013-05-28 22:11:41 -05:00
|
|
|
/// Calculates the md4 hash of a slice of bytes, returning the hex-encoded
|
|
|
|
/// version of the hash
|
2013-03-21 23:34:30 -05:00
|
|
|
pub fn md4_str(msg: &[u8]) -> ~str {
|
2013-02-04 18:48:52 -06:00
|
|
|
let Quad {a, b, c, d} = md4(msg);
|
2013-03-21 23:34:30 -05:00
|
|
|
fn app(a: u32, b: u32, c: u32, d: u32, f: &fn(u32)) {
|
2012-01-17 04:50:31 -06:00
|
|
|
f(a); f(b); f(c); f(d);
|
|
|
|
}
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut result = ~"";
|
2012-06-30 18:19:07 -05:00
|
|
|
do app(a, b, c, d) |u| {
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut i = 0u32;
|
2012-01-17 04:50:31 -06:00
|
|
|
while i < 4u32 {
|
|
|
|
let byte = (u >> (i * 8u32)) as u8;
|
2013-05-23 11:09:11 -05:00
|
|
|
if byte <= 16u8 { result += "0"; }
|
2013-01-24 14:47:57 -06:00
|
|
|
result += uint::to_str_radix(byte as uint, 16u);
|
2012-01-17 04:50:31 -06:00
|
|
|
i += 1u32;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2013-05-28 22:11:41 -05:00
|
|
|
/// Calculates the md4 hash of a string, returning the hex-encoded version of
|
|
|
|
/// the hash
|
2013-03-21 23:34:30 -05:00
|
|
|
pub fn md4_text(msg: &str) -> ~str { md4_str(str::to_bytes(msg)) }
|
2012-01-17 04:50:31 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_md4() {
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(md4_text(""), ~"31d6cfe0d16ae931b73c59d7e0c089c0");
|
|
|
|
assert_eq!(md4_text("a"), ~"bde52cb31de33e46245e05fbdbd6fb24");
|
|
|
|
assert_eq!(md4_text("abc"), ~"a448017aaf21d8525fc10ae87aa6729d");
|
|
|
|
assert!(md4_text("message digest") ==
|
2013-03-06 21:09:17 -06:00
|
|
|
~"d9130a8164549fe818874806e1c7014b");
|
2013-05-23 11:39:00 -05:00
|
|
|
assert!(md4_text("abcdefghijklmnopqrstuvwxyz") ==
|
2013-03-06 15:58:02 -06:00
|
|
|
~"d79e1c308aa5bbcdeea8ed63df412da9");
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(md4_text(
|
2013-05-23 11:39:00 -05:00
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\
|
2013-03-06 15:58:02 -06:00
|
|
|
0123456789") == ~"043f8582f241db351ce627e153e7f0e4");
|
2013-05-23 11:39:00 -05:00
|
|
|
assert!(md4_text("1234567890123456789012345678901234567890123456789\
|
2012-07-14 00:57:48 -05:00
|
|
|
0123456789012345678901234567890") ==
|
2013-03-06 15:58:02 -06:00
|
|
|
~"e33b4ddc9c38f2199c3e7b164fcc0536");
|
2012-01-17 04:50:31 -06:00
|
|
|
}
|