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 a simple benchmark for secure hash functions.
  • Loading branch information
tatsuya6502 committed May 23, 2017
1 parent 297d3b5 commit 525d560
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/hashes/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
**/*.rs.bk
13 changes: 13 additions & 0 deletions examples/hashes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "hashes"
version = "0.1.0"
authors = ["Tatsuya Kawano <[email protected]>"]

[dependencies]
blake2 = "0.5.2"
data-encoding = "2.0.0-rc.1"
# generic-array = "0.7.2"
rand = "0.3.15"
# sha2 = { version = "0.5.2", features = ["asm"] }
sha2 = "0.5.2"
sha3 = "0.5.1"
34 changes: 34 additions & 0 deletions examples/hashes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# A Simple Benchmark for Secure Hash Implementation in Rust

This is a very simple benchmark program to compare performance (speed)
between the following secure hash functions implemented in Rust:

- **SHA-256**
* crates: [sha2](https://crates.io/crates/sha2) 0.5.2,
[sha2-asm](https://crates.io/crates/sha2-asm) 0.2.1
- **SHA-3-256**
* crate: [sha3](https://crates.io/crates/sha3) 0.5.1
- **Blake2s**
* crate: [blake2](https://crates.io/crates/blake2) 0.5.2

It creates a `Vec<u8>` holding 8GB of random bytes, then calculate
secure hash using these hash functions, and compare time to complete.


## Running the Benchmark

```
$ cargo run --release
```

For example,

```
$ cargo run --release
...
Input data length: 8.00 GB
SHA-256 - 38.16 seconds (38162777286 nano-seconds), digest: "69de2109a91cd2dccf6d1ec447fa3975c0c44ab32459e178e31569bff09ce9d1"
SHA-3-256 - 74.51 seconds (74507281950 nano-seconds), digest: "8e41cb790bc5c80b3fe339f17d1b1c8871c0afe888a0e2692efbdc7e55656203"
Blake2s - 18.57 seconds (18571153963 nano-seconds), digest: "ccae52d11f9e49ba565635be5e88533444392f869f9d0fc1b66d76bc85b33042"
SHA-256: 1.00x, SHA-3-256: 0.51x, Blake2s: 2.05x
```
66 changes: 66 additions & 0 deletions examples/hashes/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// ----------------------------------------------------------------------
// Copyright (c) 2017 Hibari developers. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------

extern crate blake2;
extern crate data_encoding;
extern crate rand;
extern crate sha2;
extern crate sha3;

use std::time::Instant;

// use digest::Digest;
use blake2::{Blake2s, Digest};
use sha2::Sha256;
use sha3::Sha3_256;

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

fn main() {
let input_len = 8 * 1024 * 1024 * 1024;

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

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 ");

println!("SHA-256: 1.00x, SHA-3-256: {:.2}x, Blake2s: {:.2}x",
elapse_sha256 / elapse_sha3_256,
elapse_sha256 / elapse_blake2);
}

fn hash<D: Digest + Default>(input_len: usize, label: &str) -> f64 {
let input = generate_input(input_len);
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: {:?}",
label,
nano_secs / 1e9_f64,
nano_secs,
HEXLOWER.encode(&digest[..]));
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()
}

0 comments on commit 525d560

Please sign in to comment.