Created
December 1, 2016 11:52
-
-
Save gquintard/37da4f99979117b23fd751149bd4f187 to your computer and use it in GitHub Desktop.
sha256 test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate ring; | |
extern crate time; | |
use ring::digest::digest; | |
use ring::digest::SHA256; | |
fn hash(s: &[u8]) -> Vec<u8> { | |
let hasher = digest(&SHA256, s); | |
hasher.as_ref().to_vec() | |
} | |
fn tohex(input: Vec<u8>) -> String { | |
let output: Vec<String> = input.iter().map(|b| format!("{:02X}", b)).collect(); | |
output.join("") | |
} | |
fn main() { | |
let s0 = "abc".as_bytes(); | |
let s1 = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".as_bytes(); | |
let vs2 = vec![b'a'; 1000000]; | |
let s2 = vs2.as_slice(); | |
let ss3 = String::new() + | |
"Above the broad circular chews his shaped symptom. " + | |
"The spectrum treasures a bush finger around the salary. " + | |
"The daily rope breezes into a companion. " + | |
"The wolf suffers the law after the legal cyclist. " + | |
"The craft volunteers the attitude. " + | |
"The amazing freedom weds throughout the cross gesture."; | |
let s3 = ss3.as_bytes(); | |
println!("s0 hash: {}", tohex(hash(s0))); | |
println!("s1 hash: {}", tohex(hash(s1))); | |
println!("s2 hash: {}", tohex(hash(s2))); | |
println!("s3 hash: {}", tohex(hash(s3))); | |
let start = time::now(); | |
for _ in 0..1000 { | |
hash(s0); | |
hash(s1); | |
hash(s2); | |
hash(s3); | |
} | |
println!("time: {}", time::now() - start); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment