Skip to content

Commit

Permalink
sha-crypt: test cleanup (#241)
Browse files Browse the repository at this point in the history
Simplify test vectors
  • Loading branch information
tarcieri authored Sep 17, 2021
1 parent 6ee4f43 commit db88376
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions sha-crypt/tests/mod.rs → sha-crypt/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,40 @@ use sha_crypt::{sha512_crypt_b64, Sha512Params, ROUNDS_MAX, ROUNDS_MIN};
#[cfg(feature = "simple")]
use sha_crypt::{sha512_check, sha512_simple};

struct TestCrypt {
struct TestVector {
input: &'static str,
salt: &'static str,
result: &'static str,
rounds: usize,
}

fn tests_sha512_crypt() -> Vec<TestCrypt> {
vec![
TestCrypt {
input: "Hello world!",
salt: "saltstring",
result:
"svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjnQJuesI68u4OTLiBFdcbYEdFCoEOfaS35inz1",
rounds: 5_000,
},
TestCrypt {
input: "Hello world!",
salt: "saltstringsaltstring",
result:
"OW1/O6BYHV6BcXZu8QVeXbDWra3Oeqh0sbHbbMCVNSnCM/UrjmM0Dp8vOuZeHBy/YTBmSK6H9qs/y3RnOaw5v.",
rounds: 10_000,
},
TestCrypt {
input: "This is just a test",
salt: "toolongsaltstring",
result:
"lQ8jolhgVRVhY4b5pZKaysCLi0QBxGoNeKQzQ3glMhwllF7oGDZxUhx1yxdYcz/e1JSbq3y6JMxxl8audkUEm0",
rounds: 5_000,
},
]
}
const TEST_VECTORS: &[TestVector] = &[
TestVector {
input: "Hello world!",
salt: "saltstring",
result:
"svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjnQJuesI68u4OTLiBFdcbYEdFCoEOfaS35inz1",
rounds: 5_000,
},
TestVector {
input: "Hello world!",
salt: "saltstringsaltstring",
result:
"OW1/O6BYHV6BcXZu8QVeXbDWra3Oeqh0sbHbbMCVNSnCM/UrjmM0Dp8vOuZeHBy/YTBmSK6H9qs/y3RnOaw5v.",
rounds: 10_000,
},
TestVector {
input: "This is just a test",
salt: "toolongsaltstring",
result:
"lQ8jolhgVRVhY4b5pZKaysCLi0QBxGoNeKQzQ3glMhwllF7oGDZxUhx1yxdYcz/e1JSbq3y6JMxxl8audkUEm0",
rounds: 5_000,
},
];

#[test]
fn test_sha512_crypt() {
let tests = tests_sha512_crypt();
for t in tests.iter() {
for t in TEST_VECTORS {
let params = Sha512Params::new(t.rounds).expect("Rounds error");
let result = sha512_crypt_b64(t.input.as_bytes(), t.salt.as_bytes(), &params).unwrap();
assert!(result == t.result);
Expand Down

0 comments on commit db88376

Please sign in to comment.