Skip to content

Commit

Permalink
GH #9 - Replace MD5 checksum on blob with a safe hasher
Browse files Browse the repository at this point in the history
Add Blake2b and SHA-512 to the simple secure hash benchmark.
  • Loading branch information
tatsuya6502 committed May 24, 2017
1 parent fc65336 commit 7094351
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 20 deletions.
5 changes: 3 additions & 2 deletions examples/hashes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ authors = ["Tatsuya Kawano <[email protected]>"]

[dependencies]
blake2 = "0.5.2"
blake2-rfc = "0.2.17"
data-encoding = "2.0.0-rc.1"
# generic-array = "0.7.2"
md-5 = { version = "0.4.3", features = ["asm"] }
# md-5 = "0.4.3"
rand = "0.3.15"
# sha2 = { version = "0.5.2", features = ["asm"] }
sha2 = "0.5.2"
sha2 = { version = "0.5.2", features = ["asm"] }
# sha2 = "0.5.2"
sha3 = "0.5.1"
83 changes: 65 additions & 18 deletions examples/hashes/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// ----------------------------------------------------------------------

extern crate blake2;
extern crate blake2_rfc;
extern crate md_5 as md5;
extern crate sha2;
extern crate sha3;
Expand All @@ -25,36 +26,73 @@ extern crate data_encoding;
use std::time::Instant;

// use digest::Digest;
use blake2::{Blake2s, Digest};
use blake2::{Blake2b, Blake2s, Digest};
use blake2_rfc::blake2b::blake2b;
use md5::Md5;
use sha2::Sha256;
use sha3::Sha3_256;
use sha2::{Sha256, Sha512};
use sha3::{Sha3_256, Sha3_512};

use data_encoding::HEXLOWER;
use rand::{Rng, SeedableRng, XorShiftRng};

const GB: usize = 1024 * 1024 * 1024;

fn main() {
let input_len = 8 * 1024 * 1024 * 1024;
let input_len = 8 * GB;
// let input_len = 1024 * 1024;
let input = generate_input(input_len);

let dur_md5 = hash::<Md5>(&input[..], "MD5 (asm) ");
let dur_sha256 = hash::<Sha256>(&input[..], "SHA-256 (asm) ");
let dur_sha3_256 = hash::<Sha3_256>(&input[..], "SHA-3-256 ");
let dur_blake2s = hash::<Blake2s>(&input[..], "Blake2s ");
let dur_blake2b_rfc = hash_blake2b_rfc(&input[..]);
let dur_sha512 = hash::<Sha512>(&input[..], "SHA-512 (asm) ");
let dur_sha3_512 = hash::<Sha3_512>(&input[..], "SHA-3-512 ");
let dur_blake2b = hash::<Blake2b>(&input[..], "Blake2b ");

println!(r"
Speed ups:
MD5 (asm): {:.2}x
SHA-256 (asm): 1.00x
SHA-3-256: {:.2}x
Blake2s (256) {:.2}x
Blake2b (256): {:.2}x
SHA-512 (asm): {:.2}x
SHA-3-512: {:.2}x
Blake2b (512): {:.2}x",
dur_sha256 / dur_md5,
dur_sha256 / dur_sha3_256,
dur_sha256 / dur_blake2s,
dur_sha256 / dur_blake2b_rfc,
dur_sha256 / dur_sha512,
dur_sha256 / dur_sha3_512,
dur_sha256 / dur_blake2b);
}

fn generate_input(len: usize) -> Vec<u8> {
println!("Generating a random input data (length: {:.2} GB)", len as f64 / GB as f64);

println!("Input data length: {:.2} GB", input_len as f64 / 1024.0 / 1024.0 / 1024.0);
let start = Instant::now();

let elapse_md5 = hash::<Md5>(input_len, "MD5 ");
let elapse_sha256 = hash::<Sha256>(input_len, "SHA-256 ");
let elapse_sha3_256 = hash::<Sha3_256>(input_len, "SHA-3-256");
let elapse_blake2 = hash::<Blake2s>(input_len, "Blake2s ");
let mut rng = XorShiftRng::from_seed([0, 1, 2, 3]);
let input = rng.gen_iter().take(len).collect();

println!("MD5: {:.2}x, SHA-256: 1.00x, SHA-3-256: {:.2}x, Blake2s: {:.2}x",
elapse_sha256 / elapse_md5,
elapse_sha256 / elapse_sha3_256,
elapse_sha256 / elapse_blake2);
let dur = Instant::now() - start;
let nano_secs = dur.subsec_nanos() as f64 + dur.as_secs() as f64 * 1e9_f64;
println!("Generated the random input data in {:.2} seconds ({:.0} nano-seconds)\n",
nano_secs / 1e9_f64,
nano_secs);
input
}

fn hash<D: Digest + Default>(input_len: usize, label: &str) -> f64 {
let input = generate_input(input_len);
fn hash<D: Digest + Default>(input: &[u8], label: &str) -> f64 {
let start = Instant::now();

let mut hasher = D::default();
hasher.input(&input[..]);
let digest = hasher.result();

let dur = Instant::now() - start;
let nano_secs = dur.subsec_nanos() as f64 + dur.as_secs() as f64 * 1e9_f64;
println!("{} - {:.2} seconds ({:.0} nano-seconds), digest: {:?}",
Expand All @@ -65,7 +103,16 @@ fn hash<D: Digest + Default>(input_len: usize, label: &str) -> f64 {
nano_secs
}

fn generate_input(len: usize) -> Vec<u8> {
let mut rng = XorShiftRng::from_seed([0, 1, 2, 3]);
rng.gen_iter().take(len).collect()
fn hash_blake2b_rfc(input: &[u8]) -> f64 {
let start = Instant::now();

let digest = blake2b(32, &[], input);

let dur = Instant::now() - start;
let nano_secs = dur.subsec_nanos() as f64 + dur.as_secs() as f64 * 1e9_f64;
println!("Blake2b (256): - {:.2} seconds ({:.0} nano-seconds), digest: {:?}",
nano_secs / 1e9_f64,
nano_secs,
HEXLOWER.encode(digest.as_bytes()));
nano_secs
}

0 comments on commit 7094351

Please sign in to comment.