Skip to content

Commit 3dabbcb

Browse files
author
Palmer Cox
committed
Crypto: Add large input tests for all Digests
Create a helper function in cryptoutil.rs which feeds 1,000,000 'a's into a Digest with varying input sizes and then checks the result. This is essentially the same as one of Sha1's existing tests, so, that test was re-implemented using this method. New tests were added using this method for Sha512 and Sha256.
1 parent 3714e3a commit 3dabbcb

File tree

3 files changed

+63
-21
lines changed

3 files changed

+63
-21
lines changed

src/libextra/crypto/cryptoutil.rs

+33
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,36 @@ impl <T: FixedBuffer> StandardPadding for T {
241241
self.zero_until(size - rem);
242242
}
243243
}
244+
245+
246+
#[cfg(test)]
247+
mod test {
248+
use std::rand::IsaacRng;
249+
use std::rand::RngUtil;
250+
use std::vec;
251+
252+
use digest::Digest;
253+
254+
/// Feed 1,000,000 'a's into the digest with varying input sizes and check that the result is
255+
/// correct.
256+
pub fn test_digest_1million_random<D: Digest>(digest: &mut D, blocksize: uint, expected: &str) {
257+
let total_size = 1000000;
258+
let buffer = vec::from_elem(blocksize * 2, 'a' as u8);
259+
let mut rng = IsaacRng::new_unseeded();
260+
let mut count = 0;
261+
262+
digest.reset();
263+
264+
while count < total_size {
265+
let next: uint = rng.gen_uint_range(0, 2 * blocksize + 1);
266+
let remaining = total_size - count;
267+
let size = if next > remaining { remaining } else { next };
268+
digest.input(buffer.slice_to(size));
269+
count += size;
270+
}
271+
272+
let result_str = digest.result_str();
273+
274+
assert!(expected == result_str);
275+
}
276+
}

src/libextra/crypto/sha1.rs

+10-21
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ impl Digest for Sha1 {
240240

241241
#[cfg(test)]
242242
mod tests {
243-
243+
use cryptoutil::test::test_digest_1million_random;
244244
use digest::Digest;
245245
use sha1::Sha1;
246246

@@ -253,15 +253,6 @@ mod tests {
253253

254254
#[test]
255255
fn test() {
256-
fn a_million_letter_a() -> ~str {
257-
let mut i = 0;
258-
let mut rs = ~"";
259-
while i < 100000 {
260-
rs.push_str("aaaaaaaaaa");
261-
i += 1;
262-
}
263-
return rs;
264-
}
265256
// Test messages from FIPS 180-1
266257

267258
let fips_180_1_tests = ~[
@@ -289,17 +280,6 @@ mod tests {
289280
],
290281
output_str: ~"84983e441c3bd26ebaae4aa1f95129e5e54670f1"
291282
},
292-
Test {
293-
input: a_million_letter_a(),
294-
output: ~[
295-
0x34u8, 0xAAu8, 0x97u8, 0x3Cu8,
296-
0xD4u8, 0xC4u8, 0xDAu8, 0xA4u8,
297-
0xF6u8, 0x1Eu8, 0xEBu8, 0x2Bu8,
298-
0xDBu8, 0xADu8, 0x27u8, 0x31u8,
299-
0x65u8, 0x34u8, 0x01u8, 0x6Fu8,
300-
],
301-
output_str: ~"34aa973cd4c4daa4f61eeb2bdbad27316534016f"
302-
},
303283
];
304284
// Examples from wikipedia
305285

@@ -366,6 +346,15 @@ mod tests {
366346
sh.reset();
367347
}
368348
}
349+
350+
#[test]
351+
fn test_1million_random_sha1() {
352+
let mut sh = Sha1::new();
353+
test_digest_1million_random(
354+
&mut sh,
355+
64,
356+
"34aa973cd4c4daa4f61eeb2bdbad27316534016f");
357+
}
369358
}
370359

371360
#[cfg(test)]

src/libextra/crypto/sha2.rs

+20
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,7 @@ static H224: [u32, ..8] = [
756756

757757
#[cfg(test)]
758758
mod tests {
759+
use cryptoutil::test::test_digest_1million_random;
759760
use digest::Digest;
760761
use sha2::{Sha512, Sha384, Sha512Trunc256, Sha512Trunc224, Sha256, Sha224};
761762

@@ -947,6 +948,25 @@ mod tests {
947948
948949
test_hash(sh, tests);
949950
}
951+
952+
#[test]
953+
fn test_1million_random_sha512() {
954+
let mut sh = Sha512::new();
955+
test_digest_1million_random(
956+
&mut sh,
957+
128,
958+
"e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb" +
959+
"de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b");
960+
}
961+
962+
#[test]
963+
fn test_1million_random_sha256() {
964+
let mut sh = Sha256::new();
965+
test_digest_1million_random(
966+
&mut sh,
967+
64,
968+
"cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0");
969+
}
950970
}
951971

952972

0 commit comments

Comments
 (0)