use std::io::Write;
use super::md5;
use crate::compat;
use crate::{encoding::hex, hash::Hash};
struct MD5Test {
output: &'static str,
input: &'static [u8],
}
const GOLDEN: &[MD5Test] = &[
MD5Test {
output: "d41d8cd98f00b204e9800998ecf8427e",
input: b"",
},
MD5Test{
output: "0cc175b9c0f1b6a831c399e269772661",
input: b"a",
},
MD5Test{
output: "187ef4436122d1cc2f40dc2b92f0eba0",
input: b"ab",
},
MD5Test{
output: "900150983cd24fb0d6963f7d28e17f72",
input: b"abc",
},
MD5Test{
output: "e2fc714c4727ee9395f324cd2e7f331f",
input: b"abcd",
},
MD5Test{
output: "ab56b4d92b40713acc5af89985d4b786",
input: b"abcde",
},
MD5Test{
output: "e80b5017098950fc58aad83c8c14978e",
input: b"abcdef",
},
MD5Test{
output: "7ac66c0f148de9519b8bd264312c4d64",
input: b"abcdefg",
},
MD5Test{
output: "e8dc4081b13434b45189a720b77b6818",
input: b"abcdefgh",
},
MD5Test{
output: "8aa99b1f439ff71293e95357bac6fd94",
input: b"abcdefghi",
},
MD5Test{
output: "a925576942e94b2ef57a066101b48876",
input: b"abcdefghij",
},
MD5Test{
output: "d747fc1719c7eacb84058196cfe56d57",
input: b"Discard medicine more than two years old.",
},
MD5Test{
output: "bff2dcb37ef3a44ba43ab144768ca837",
input: b"He who has a shady past knows that nice guys finish last.",
},
MD5Test{
output: "0441015ecb54a7342d017ed1bcfdbea5",
input: b"I wouldn't marry him with a ten foot pole.",
},
MD5Test{
output: "9e3cac8e9e9757a60c3ea391130d3689",
input: b"Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave",
},
MD5Test{
output: "a0f04459b031f916a59a35cc482dc039",
input: b"The days of the digital watch are numbered. -Tom Stoppard",
},
MD5Test{
output: "e7a48e0fe884faf31475d2a04b1362cc",
input: b"Nepal premier won't resign.",
},
MD5Test{
output: "637d2fe925c07c113800509964fb0e06",
input: b"For every action there is an equal and opposite government program.",
},
MD5Test{
output: "834a8d18d5c6562119cf4c7f5086cb71",
input: b"His money is twice tainted: 'taint yours and 'taint mine.",
},
MD5Test{
output: "de3a4d2fd6c73ec2db2abad23b444281",
input: b"There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977",
},
MD5Test{
output: "acf203f997e2cf74ea3aff86985aefaf",
input: b"It's a tiny change to the code and not completely disgusting. - Bob Manchek",
},
MD5Test{
output: "e1c1384cb4d2221dfdd7c795a4222c9a",
input: b"size: a.out: bad magic",
},
MD5Test{
output: "c90f3ddecc54f34228c063d7525bf644",
input: b"The major problem is with sendmail. -Mark Horton",
},
MD5Test{
output: "cdf7ab6c1fd49bd9933c43f3ea5af185",
input: b"Give me a rock, paper and scissors and I will move the world. CCFestoon",
},
MD5Test{
output: "83bc85234942fc883c063cbd7f0ad5d0",
input: b"If the enemy is within range, then so are you.",
},
MD5Test{
output: "277cbe255686b48dd7e8f389394d9299",
input: b"It's well we cannot hear the screams/That we create in others' dreams.",
},
MD5Test{
output: "fd3fb0a7ffb8af16603f3d3af98f8e1f",
input: b"You remind me of a TV show, but that's all right: I watch it anyway.",
},
MD5Test{
output: "469b13a78ebf297ecda64d4723655154",
input: b"C is as portable as Stonehedge!!",
},
MD5Test{
output: "63eb3a2f466410104731c4b037600110",
input: b"Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley",
},
MD5Test{
output: "72c2ed7592debca1c90fc0100f931a2f",
input: b"The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule",
},
MD5Test{
output: "132f7619d33b523b1d9e5bd8e0928355",
input: b"How can you write a big system without C++? -Paul Glick",
},
];
#[test]
fn test_golden() {
for g in GOLDEN {
let s = hex::encode_to_string(&md5::sum(g.input));
assert_eq!(g.output, s);
let mut c = md5::Digest::new();
let mut buf = vec![0_u8; g.input.len() + 4];
for j in 0..3 + 4 {
#[allow(clippy::comparison_chain)]
if j < 2 {
c.write_all(g.input).unwrap();
} else if j == 2 {
let half = g.input.len() / 2;
c.write_all(&g.input[0..half]).unwrap();
_ = c.sum(&[]);
c.write_all(&g.input[half..]).unwrap();
} else if j > 2 {
let start = j - 2;
compat::copy(&mut buf[start..], g.input);
c.write_all(&buf[start..start + g.input.len()]).unwrap();
}
let s = hex::encode_to_string(&c.sum(&[]));
assert_eq!(g.output, s, "test #{}", j);
c.reset();
}
}
}