From ff903b0b663937e0959d434e06aff73e110a7b17 Mon Sep 17 00:00:00 2001 From: Pietro Danzi Date: Tue, 17 Dec 2019 04:15:00 +0100 Subject: [PATCH] fix PBKDF2 example (#538) --- src/cryptography/encryption/pbkdf2.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/cryptography/encryption/pbkdf2.md b/src/cryptography/encryption/pbkdf2.md index f53e52f..3654e0a 100644 --- a/src/cryptography/encryption/pbkdf2.md +++ b/src/cryptography/encryption/pbkdf2.md @@ -17,10 +17,11 @@ use data_encoding::HEXUPPER; use ring::error::Unspecified; use ring::rand::SecureRandom; use ring::{digest, pbkdf2, rand}; +use std::num::NonZeroU32; fn main() -> Result<(), Unspecified> { 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 mut salt = [0u8; CREDENTIAL_LEN]; @@ -29,8 +30,8 @@ fn main() -> Result<(), Unspecified> { let password = "Guess Me If You Can!"; let mut pbkdf2_hash = [0u8; CREDENTIAL_LEN]; pbkdf2::derive( - &digest::SHA512, - N_ITER, + pbkdf2::PBKDF2_HMAC_SHA512, + n_iter, &salt, password.as_bytes(), &mut pbkdf2_hash, @@ -39,16 +40,16 @@ fn main() -> Result<(), Unspecified> { println!("PBKDF2 hash: {}", HEXUPPER.encode(&pbkdf2_hash)); let should_succeed = pbkdf2::verify( - &digest::SHA512, - N_ITER, + pbkdf2::PBKDF2_HMAC_SHA512, + n_iter, &salt, password.as_bytes(), &pbkdf2_hash, ); let wrong_password = "Definitely not the correct password"; let should_fail = pbkdf2::verify( - &digest::SHA512, - N_ITER, + pbkdf2::PBKDF2_HMAC_SHA512, + n_iter, &salt, wrong_password.as_bytes(), &pbkdf2_hash,