Skip to content

Commit 069f992

Browse files
committed
Refactor Blake2b tests and add a new test with a large input
1 parent 75b5442 commit 069f992

File tree

1 file changed

+74
-28
lines changed

1 file changed

+74
-28
lines changed

src/blake2b.rs

Lines changed: 74 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -392,62 +392,108 @@ mod digest_tests {
392392
//use cryptoutil::test::test_digest_1million_random;
393393
use blake2b::Blake2b;
394394
use digest::Digest;
395+
use serialize::hex::FromHex;
395396

396397

397398
struct Test {
398-
input: &'static str,
399-
output_str: &'static str,
399+
input: Vec<u8>,
400+
output: Vec<u8>,
401+
key: Option<Vec<u8>>,
400402
}
401403

402-
fn test_hash<D: Digest>(sh: &mut D, tests: &[Test]) {
403-
// Test that it works when accepting the message all at once
404-
for t in tests.iter() {
405-
sh.input_str(t.input);
404+
fn test_hash(tests: &[Test]) {
405+
for t in tests {
406+
let mut sh = match t.key {
407+
Some(ref key) => Blake2b::new_keyed(64, &key),
408+
None => Blake2b::new(64)
409+
};
406410

407-
let out_str = sh.result_str();
408-
assert!(&out_str[..] == t.output_str);
411+
// Test that it works when accepting the message all at once
412+
sh.input(&t.input[..]);
413+
414+
let mut out = [0u8; 64];
415+
sh.result(&mut out);
416+
assert!(&out[..] == &t.output[..]);
409417

410418
sh.reset();
411-
}
412419

413-
// Test that it works when accepting the message in pieces
414-
for t in tests.iter() {
420+
// Test that it works when accepting the message in pieces
415421
let len = t.input.len();
416422
let mut left = len;
417423
while left > 0 {
418424
let take = (left + 1) / 2;
419-
sh.input_str(&t.input[len - left..take + len - left]);
420-
left = left - take;
425+
sh.input(&t.input[len - left..take + len - left]);
426+
left -= take;
421427
}
422428

423-
let out_str = sh.result_str();
424-
assert!(&out_str[..] == t.output_str);
429+
let mut out = [0u8; 64];
430+
sh.result(&mut out);
431+
assert!(&out[..] == &t.output[..]);
425432

426433
sh.reset();
427434
}
428435
}
429436

430437
#[test]
431438
fn test_blake2b_digest() {
432-
// Examples from wikipedia
433-
let wikipedia_tests = vec![
439+
let tests = vec![
440+
// Examples from wikipedia
441+
Test {
442+
input: vec![],
443+
output: "786a02f742015903c6c6fd852552d272\
444+
912f4740e15847618a86e217f71f5419\
445+
d25e1031afee585313896444934eb04b\
446+
903a685b1448b755d56f701afe9be2ce".from_hex().unwrap(),
447+
key: None
448+
},
434449
Test {
435-
input: "",
436-
output_str: "786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419\
437-
d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce"
450+
input: "The quick brown fox jumps over the lazy dog".as_bytes().to_vec(),
451+
output: "a8add4bdddfd93e4877d2746e62817b1\
452+
16364a1fa7bc148d95090bc7333b3673\
453+
f82401cf7aa2e4cb1ecd90296e3f14cb\
454+
5413f8ed77be73045b13914cdcd6a918".from_hex().unwrap(),
455+
key: None
438456
},
457+
// from: https://github.com/BLAKE2/BLAKE2/blob/master/testvectors/blake2b-test.txt
439458
Test {
440-
input: "The quick brown fox jumps over the lazy dog",
441-
output_str: "a8add4bdddfd93e4877d2746e62817b116364a1fa7bc148d95090bc7333b3673\
442-
f82401cf7aa2e4cb1ecd90296e3f14cb5413f8ed77be73045b13914cdcd6a918"
459+
input: vec![0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
460+
0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
461+
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
462+
0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
463+
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
464+
0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
465+
0x48, 0x49, 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f8, 0xf9, 0xfa, 0xfb,
480+
0xfc, 0xfd, 0xfe],
481+
output: vec![0x14, 0x27, 0x09, 0xd6, 0x2e, 0x28, 0xfc, 0xcc, 0xd0, 0xaf, 0x97,
482+
0xfa, 0xd0, 0xf8, 0x46, 0x5b, 0x97, 0x1e, 0x82, 0x20, 0x1d, 0xc5,
483+
0x10, 0x70, 0xfa, 0xa0, 0x37, 0x2a, 0xa4, 0x3e, 0x92, 0x48, 0x4b,
484+
0xe1, 0xc1, 0xe7, 0x3b, 0xa1, 0x09, 0x06, 0xd5, 0xd1, 0x85, 0x3d,
485+
0xb6, 0xa4, 0x10, 0x6e, 0x0a, 0x7b, 0xf9, 0x80, 0x0d, 0x37, 0x3d,
486+
0x6d, 0xee, 0x2d, 0x46, 0xd6, 0x2e, 0xf2, 0xa4, 0x61],
487+
key: Some(vec![0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
488+
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
489+
0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
490+
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
491+
0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
492+
0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f])
443493
},
444494
];
445495

446-
let tests = wikipedia_tests;
447-
448-
let mut sh = Blake2b::new(64);
449-
450-
test_hash(&mut sh, &tests[..]);
496+
test_hash(&tests[..]);
451497
}
452498
}
453499

0 commit comments

Comments
 (0)