mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2025-02-16 12:18:27 +00:00
fix PBKDF2 example (#538)
This commit is contained in:
parent
2a57fa3904
commit
ff903b0b66
1 changed files with 8 additions and 7 deletions
|
@ -17,10 +17,11 @@ use data_encoding::HEXUPPER;
|
||||||
use ring::error::Unspecified;
|
use ring::error::Unspecified;
|
||||||
use ring::rand::SecureRandom;
|
use ring::rand::SecureRandom;
|
||||||
use ring::{digest, pbkdf2, rand};
|
use ring::{digest, pbkdf2, rand};
|
||||||
|
use std::num::NonZeroU32;
|
||||||
|
|
||||||
fn main() -> Result<(), Unspecified> {
|
fn main() -> Result<(), Unspecified> {
|
||||||
const CREDENTIAL_LEN: usize = digest::SHA512_OUTPUT_LEN;
|
const CREDENTIAL_LEN: usize = digest::SHA512_OUTPUT_LEN;
|
||||||
const N_ITER: u32 = 100_000;
|
let n_iter = NonZeroU32::new(100_000).unwrap();
|
||||||
let rng = rand::SystemRandom::new();
|
let rng = rand::SystemRandom::new();
|
||||||
|
|
||||||
let mut salt = [0u8; CREDENTIAL_LEN];
|
let mut salt = [0u8; CREDENTIAL_LEN];
|
||||||
|
@ -29,8 +30,8 @@ fn main() -> Result<(), Unspecified> {
|
||||||
let password = "Guess Me If You Can!";
|
let password = "Guess Me If You Can!";
|
||||||
let mut pbkdf2_hash = [0u8; CREDENTIAL_LEN];
|
let mut pbkdf2_hash = [0u8; CREDENTIAL_LEN];
|
||||||
pbkdf2::derive(
|
pbkdf2::derive(
|
||||||
&digest::SHA512,
|
pbkdf2::PBKDF2_HMAC_SHA512,
|
||||||
N_ITER,
|
n_iter,
|
||||||
&salt,
|
&salt,
|
||||||
password.as_bytes(),
|
password.as_bytes(),
|
||||||
&mut pbkdf2_hash,
|
&mut pbkdf2_hash,
|
||||||
|
@ -39,16 +40,16 @@ fn main() -> Result<(), Unspecified> {
|
||||||
println!("PBKDF2 hash: {}", HEXUPPER.encode(&pbkdf2_hash));
|
println!("PBKDF2 hash: {}", HEXUPPER.encode(&pbkdf2_hash));
|
||||||
|
|
||||||
let should_succeed = pbkdf2::verify(
|
let should_succeed = pbkdf2::verify(
|
||||||
&digest::SHA512,
|
pbkdf2::PBKDF2_HMAC_SHA512,
|
||||||
N_ITER,
|
n_iter,
|
||||||
&salt,
|
&salt,
|
||||||
password.as_bytes(),
|
password.as_bytes(),
|
||||||
&pbkdf2_hash,
|
&pbkdf2_hash,
|
||||||
);
|
);
|
||||||
let wrong_password = "Definitely not the correct password";
|
let wrong_password = "Definitely not the correct password";
|
||||||
let should_fail = pbkdf2::verify(
|
let should_fail = pbkdf2::verify(
|
||||||
&digest::SHA512,
|
pbkdf2::PBKDF2_HMAC_SHA512,
|
||||||
N_ITER,
|
n_iter,
|
||||||
&salt,
|
&salt,
|
||||||
wrong_password.as_bytes(),
|
wrong_password.as_bytes(),
|
||||||
&pbkdf2_hash,
|
&pbkdf2_hash,
|
||||||
|
|
Loading…
Add table
Reference in a new issue