Skip to content
64 changes: 64 additions & 0 deletions balloon-hash/benches/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#![feature(test)]

extern crate test;
use test::Bencher;

use balloon_hash::{Algorithm, Balloon, Params};
use digest::array::Array;

#[path = "../data/balloon_m_test_vectors.rs"]
mod test_vectors;
use test_vectors::{BALLOON_M_TEST_VECTORS, BalloonMTestVector};

fn bench_test_vector(b: &mut Bencher, test_vector: &BalloonMTestVector) {
let params = Params::new(test_vector.s_cost, test_vector.t_cost, test_vector.p_cost).unwrap();
let balloon = Balloon::<sha2::Sha256>::new(Algorithm::BalloonM, params, None);
let mut memory = vec![Array::default(); balloon.params.s_cost.get() as usize];
b.bytes = test_vector.password.len() as u64;
b.iter(|| {
let result = balloon
.hash_password_with_memory(test_vector.password, test_vector.salt, &mut memory)
.unwrap();
assert_eq!(result.as_slice(), test_vector.output);
});
}

#[bench]
fn bench_balloon_m_vector_0(b: &mut Bencher) {
bench_test_vector(b, &BALLOON_M_TEST_VECTORS[0]);
}

#[bench]
fn bench_balloon_m_vector_1(b: &mut Bencher) {
bench_test_vector(b, &BALLOON_M_TEST_VECTORS[1]);
}

#[bench]
fn bench_balloon_m_vector_2(b: &mut Bencher) {
bench_test_vector(b, &BALLOON_M_TEST_VECTORS[2]);
}

#[bench]
fn bench_balloon_m_vector_3(b: &mut Bencher) {
bench_test_vector(b, &BALLOON_M_TEST_VECTORS[3]);
}

#[bench]
fn bench_balloon_m_vector_4(b: &mut Bencher) {
bench_test_vector(b, &BALLOON_M_TEST_VECTORS[4]);
}

#[bench]
fn bench_balloon_m_vector_5(b: &mut Bencher) {
bench_test_vector(b, &BALLOON_M_TEST_VECTORS[5]);
}

#[bench]
fn bench_balloon_m_vector_6(b: &mut Bencher) {
bench_test_vector(b, &BALLOON_M_TEST_VECTORS[6]);
}

#[bench]
fn bench_balloon_m_vector_7(b: &mut Bencher) {
bench_test_vector(b, &BALLOON_M_TEST_VECTORS[7]);
}
80 changes: 80 additions & 0 deletions balloon-hash/data/balloon_m_test_vectors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use hex_literal::hex;

pub struct BalloonMTestVector {
pub password: &'static [u8],
pub salt: &'static [u8],
pub s_cost: u32,
pub t_cost: u32,
pub p_cost: u32,
pub output: [u8; 32],
}

/// Tested with the following implementations:
/// - <https://github.com/nachonavarro/balloon-hashing>
/// - <https://github.com/nogoegst/balloon>
pub const BALLOON_M_TEST_VECTORS: &[BalloonMTestVector] = &[
BalloonMTestVector {
password: b"hunter42",
salt: b"examplesalt",
s_cost: 1024,
t_cost: 3,
p_cost: 4,
output: hex!("1832bd8e5cbeba1cb174a13838095e7e66508e9bf04c40178990adbc8ba9eb6f"),
},
BalloonMTestVector {
password: b"",
salt: b"salt",
s_cost: 3,
t_cost: 3,
p_cost: 2,
output: hex!("f8767fe04059cef67b4427cda99bf8bcdd983959dbd399a5e63ea04523716c23"),
},
BalloonMTestVector {
password: b"password",
salt: b"",
s_cost: 3,
t_cost: 3,
p_cost: 3,
output: hex!("bcad257eff3d1090b50276514857e60db5d0ec484129013ef3c88f7d36e438d6"),
},
BalloonMTestVector {
password: b"password",
salt: b"",
s_cost: 3,
t_cost: 3,
p_cost: 1,
output: hex!("498344ee9d31baf82cc93ebb3874fe0b76e164302c1cefa1b63a90a69afb9b4d"),
},
BalloonMTestVector {
password: b"\0",
salt: b"\0",
s_cost: 3,
t_cost: 3,
p_cost: 4,
output: hex!("8a665611e40710ba1fd78c181549c750f17c12e423c11930ce997f04c7153e0c"),
},
BalloonMTestVector {
password: b"\0",
salt: b"\0",
s_cost: 3,
t_cost: 3,
p_cost: 1,
output: hex!("d9e33c683451b21fb3720afbd78bf12518c1d4401fa39f054b052a145c968bb1"),
},
BalloonMTestVector {
password: b"password",
salt: b"salt",
s_cost: 1,
t_cost: 1,
p_cost: 16,
output: hex!("a67b383bb88a282aef595d98697f90820adf64582a4b3627c76b7da3d8bae915"),
},
BalloonMTestVector {
password: b"password",
salt: b"salt",
s_cost: 1,
t_cost: 1,
p_cost: 1,
output: hex!("97a11df9382a788c781929831d409d3599e0b67ab452ef834718114efdcd1c6d"),
},
];
50 changes: 50 additions & 0 deletions balloon-hash/data/balloon_test_vectors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use hex_literal::hex;

pub struct BalloonTestVector {
pub password: &'static [u8],
pub salt: &'static [u8],
pub s_cost: u32,
pub t_cost: u32,
pub output: [u8; 32],
}

/// Tested with the following implementations:
/// - <https://github.com/nachonavarro/balloon-hashing>
/// - <https://github.com/nogoegst/balloon>
pub const BALLOON_TEST_VECTORS: &[BalloonTestVector] = &[
BalloonTestVector {
password: b"hunter42",
salt: b"examplesalt",
s_cost: 1024,
t_cost: 3,
output: hex!("716043dff777b44aa7b88dcbab12c078abecfac9d289c5b5195967aa63440dfb"),
},
BalloonTestVector {
password: b"",
salt: b"salt",
s_cost: 3,
t_cost: 3,
output: hex!("5f02f8206f9cd212485c6bdf85527b698956701ad0852106f94b94ee94577378"),
},
BalloonTestVector {
password: b"password",
salt: b"",
s_cost: 3,
t_cost: 3,
output: hex!("20aa99d7fe3f4df4bd98c655c5480ec98b143107a331fd491deda885c4d6a6cc"),
},
BalloonTestVector {
password: b"\0",
salt: b"\0",
s_cost: 3,
t_cost: 3,
output: hex!("4fc7e302ffa29ae0eac31166cee7a552d1d71135f4e0da66486fb68a749b73a4"),
},
BalloonTestVector {
password: b"password",
salt: b"salt",
s_cost: 1,
t_cost: 1,
output: hex!("eefda4a8a75b461fa389c1dcfaf3e9dfacbc26f81f22e6f280d15cc18c417545"),
},
];
54 changes: 4 additions & 50 deletions balloon-hash/tests/balloon.rs
Original file line number Diff line number Diff line change
@@ -1,59 +1,13 @@
use balloon_hash::{Algorithm, Balloon, Params};
use digest::array::Array;
use hex_literal::hex;

struct TestVector {
password: &'static [u8],
salt: &'static [u8],
s_cost: u32,
t_cost: u32,
output: [u8; 32],
}

/// Tested with the following implementations:
/// - <https://github.com/nachonavarro/balloon-hashing>
/// - <https://github.com/nogoegst/balloon>
const TEST_VECTORS: &[TestVector] = &[
TestVector {
password: b"hunter42",
salt: b"examplesalt",
s_cost: 1024,
t_cost: 3,
output: hex!("716043dff777b44aa7b88dcbab12c078abecfac9d289c5b5195967aa63440dfb"),
},
TestVector {
password: b"",
salt: b"salt",
s_cost: 3,
t_cost: 3,
output: hex!("5f02f8206f9cd212485c6bdf85527b698956701ad0852106f94b94ee94577378"),
},
TestVector {
password: b"password",
salt: b"",
s_cost: 3,
t_cost: 3,
output: hex!("20aa99d7fe3f4df4bd98c655c5480ec98b143107a331fd491deda885c4d6a6cc"),
},
TestVector {
password: b"\0",
salt: b"\0",
s_cost: 3,
t_cost: 3,
output: hex!("4fc7e302ffa29ae0eac31166cee7a552d1d71135f4e0da66486fb68a749b73a4"),
},
TestVector {
password: b"password",
salt: b"salt",
s_cost: 1,
t_cost: 1,
output: hex!("eefda4a8a75b461fa389c1dcfaf3e9dfacbc26f81f22e6f280d15cc18c417545"),
},
];
#[path = "../data/balloon_test_vectors.rs"]
mod balloon_test_vectors;
use balloon_test_vectors::BALLOON_TEST_VECTORS;

#[test]
fn test_vectors() {
for test_vector in TEST_VECTORS {
for test_vector in BALLOON_TEST_VECTORS {
let balloon = Balloon::<sha2::Sha256>::new(
Algorithm::Balloon,
Params::new(test_vector.s_cost, test_vector.t_cost, 1).unwrap(),
Expand Down
84 changes: 4 additions & 80 deletions balloon-hash/tests/balloon_m.rs
Original file line number Diff line number Diff line change
@@ -1,89 +1,13 @@
use balloon_hash::{Algorithm, Balloon, Params};
use digest::array::Array;
use hex_literal::hex;

struct TestVector {
password: &'static [u8],
salt: &'static [u8],
s_cost: u32,
t_cost: u32,
p_cost: u32,
output: [u8; 32],
}

/// Tested with the following implementations:
/// - <https://github.com/nachonavarro/balloon-hashing>
/// - <https://github.com/nogoegst/balloon>
const TEST_VECTORS: &[TestVector] = &[
TestVector {
password: b"hunter42",
salt: b"examplesalt",
s_cost: 1024,
t_cost: 3,
p_cost: 4,
output: hex!("1832bd8e5cbeba1cb174a13838095e7e66508e9bf04c40178990adbc8ba9eb6f"),
},
TestVector {
password: b"",
salt: b"salt",
s_cost: 3,
t_cost: 3,
p_cost: 2,
output: hex!("f8767fe04059cef67b4427cda99bf8bcdd983959dbd399a5e63ea04523716c23"),
},
TestVector {
password: b"password",
salt: b"",
s_cost: 3,
t_cost: 3,
p_cost: 3,
output: hex!("bcad257eff3d1090b50276514857e60db5d0ec484129013ef3c88f7d36e438d6"),
},
TestVector {
password: b"password",
salt: b"",
s_cost: 3,
t_cost: 3,
p_cost: 1,
output: hex!("498344ee9d31baf82cc93ebb3874fe0b76e164302c1cefa1b63a90a69afb9b4d"),
},
TestVector {
password: b"\0",
salt: b"\0",
s_cost: 3,
t_cost: 3,
p_cost: 4,
output: hex!("8a665611e40710ba1fd78c181549c750f17c12e423c11930ce997f04c7153e0c"),
},
TestVector {
password: b"\0",
salt: b"\0",
s_cost: 3,
t_cost: 3,
p_cost: 1,
output: hex!("d9e33c683451b21fb3720afbd78bf12518c1d4401fa39f054b052a145c968bb1"),
},
TestVector {
password: b"password",
salt: b"salt",
s_cost: 1,
t_cost: 1,
p_cost: 16,
output: hex!("a67b383bb88a282aef595d98697f90820adf64582a4b3627c76b7da3d8bae915"),
},
TestVector {
password: b"password",
salt: b"salt",
s_cost: 1,
t_cost: 1,
p_cost: 1,
output: hex!("97a11df9382a788c781929831d409d3599e0b67ab452ef834718114efdcd1c6d"),
},
];
#[path = "../data/balloon_m_test_vectors.rs"]
mod balloon_m_test_vectors;
use balloon_m_test_vectors::BALLOON_M_TEST_VECTORS;

#[test]
fn test_vectors() {
for test_vector in TEST_VECTORS {
for test_vector in BALLOON_M_TEST_VECTORS {
let balloon = Balloon::<sha2::Sha256>::new(
Algorithm::BalloonM,
Params::new(test_vector.s_cost, test_vector.t_cost, test_vector.p_cost).unwrap(),
Expand Down
Loading
Loading