|
| 1 | +// Copyright 2015 Brian Smith. |
| 2 | +// |
| 3 | +// Permission to use, copy, modify, and/or distribute this software for any |
| 4 | +// purpose with or without fee is hereby granted, provided that the above |
| 5 | +// copyright notice and this permission notice appear in all copies. |
| 6 | +// |
| 7 | +// THE SOFTWARE IS PROVIDED "AS IS" AND AND THE AUTHORS DISCLAIM ALL WARRANTIES |
| 8 | +// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | +// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
| 10 | +// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | +// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | +// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | +// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 | + |
| 15 | +// Derived from the CC0-licensed implementation by Joseph Birr-Pixton at |
| 16 | +// https://github.com/ctz/rust-fastpbkdf2/blob/master/pbkdf2-bench/src/main.rs |
| 17 | +// commit b000c72c7b3beb0bee761bebcf07ac3f0875d1ad. This version only measures |
| 18 | +// *ring*. |
| 19 | + |
| 20 | +extern crate ring; |
| 21 | +extern crate time; |
| 22 | + |
| 23 | +use ring::pbkdf2; |
| 24 | +use time::SteadyTime; |
| 25 | + |
| 26 | +const ITERATIONS: usize = 1 << 20; |
| 27 | +const PASSWORD: &'static [u8] = b"password"; |
| 28 | +const SALT: &'static [u8] = b"salt"; |
| 29 | + |
| 30 | +fn bench<F>(name: &'static str, f: F) where F: FnOnce() { |
| 31 | + let start = SteadyTime::now(); |
| 32 | + f(); |
| 33 | + let duration = SteadyTime::now() - start; |
| 34 | + println!("{} = {}ms", name, duration.num_milliseconds()); |
| 35 | +} |
| 36 | + |
| 37 | +fn ring_sha1() { |
| 38 | + let mut out = [0u8; 20]; |
| 39 | + pbkdf2::derive(&pbkdf2::HMAC_SHA1, ITERATIONS, PASSWORD, SALT, &mut out); |
| 40 | +} |
| 41 | + |
| 42 | +fn ring_sha256() { |
| 43 | + let mut out = [0u8; 32]; |
| 44 | + pbkdf2::derive(&pbkdf2::HMAC_SHA256, ITERATIONS, PASSWORD, SALT, &mut out); |
| 45 | +} |
| 46 | + |
| 47 | +fn ring_sha512() { |
| 48 | + let mut out = [0u8; 64]; |
| 49 | + pbkdf2::derive(&pbkdf2::HMAC_SHA512, ITERATIONS, PASSWORD, SALT, &mut out); |
| 50 | +} |
| 51 | + |
| 52 | +fn main() { |
| 53 | + bench("ring-sha1", ring_sha1); |
| 54 | + bench("ring-sha256", ring_sha256); |
| 55 | + bench("ring-sha512", ring_sha512); |
| 56 | +} |
0 commit comments