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.
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
pub pure 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.
|
|
|
|
let orig_len: u64 = (vec::len(msg) * 8u) as u64;
|
|
|
|
|
2012-01-17 04:50:31 -06:00
|
|
|
// pad message
|
2012-08-25 17:10:25 -05:00
|
|
|
let mut msg = vec::append(vec::from_slice(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 {
|
2012-11-17 12:13:11 -06:00
|
|
|
unsafe {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 {
|
2012-11-17 12:13:11 -06:00
|
|
|
unsafe {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
|
|
|
|
2012-11-17 12:13:11 -06:00
|
|
|
pure 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;
|
|
|
|
let e = vec::len(msg);
|
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 {
|
|
|
|
let aa = a, bb = b, cc = c, dd = d;
|
|
|
|
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut j = 0u, base = 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
|
|
|
}
|
|
|
|
|
2012-11-17 12:13:11 -06:00
|
|
|
pub pure fn md4_str(msg: &[u8]) -> ~str {
|
2013-02-04 18:48:52 -06:00
|
|
|
let Quad {a, b, c, d} = md4(msg);
|
2013-03-07 16:38:38 -06:00
|
|
|
pure 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;
|
2012-07-14 00:57:48 -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
|
|
|
|
}
|
|
|
|
|
2012-11-17 12:13:11 -06:00
|
|
|
pub pure 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-03-06 15:58:02 -06:00
|
|
|
fail_unless!(md4_text(~"") == ~"31d6cfe0d16ae931b73c59d7e0c089c0");
|
|
|
|
fail_unless!(md4_text(~"a") == ~"bde52cb31de33e46245e05fbdbd6fb24");
|
|
|
|
fail_unless!(md4_text(~"abc") == ~"a448017aaf21d8525fc10ae87aa6729d");
|
2013-03-06 21:09:17 -06:00
|
|
|
fail_unless!(md4_text(~"message digest") ==
|
|
|
|
~"d9130a8164549fe818874806e1c7014b");
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(md4_text(~"abcdefghijklmnopqrstuvwxyz") ==
|
|
|
|
~"d79e1c308aa5bbcdeea8ed63df412da9");
|
|
|
|
fail_unless!(md4_text(
|
|
|
|
~"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\
|
|
|
|
0123456789") == ~"043f8582f241db351ce627e153e7f0e4");
|
|
|
|
fail_unless!(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
|
|
|
}
|