Created
January 9, 2019 02:44
-
-
Save petejodo/ee9596c0afb3c78972c0a1cb53e4525d to your computer and use it in GitHub Desktop.
Rust Salts and Password Hashing
This file contains hidden or 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
| use lazy_static::lazy_static; | |
| use ring::rand::{SecureRandom, SystemRandom}; | |
| lazy_static! { | |
| static ref RANDOM: SystemRandom = { | |
| let rng = SystemRandom::new(); | |
| let mut warming_salt: Vec<u8> = vec![0; 8]; | |
| rng.fill(&mut warming_salt) | |
| .expect("Failure to initialize with warming salt"); | |
| rng | |
| }; | |
| } | |
| pub fn generate_salt(salt_length: usize) -> Result<Vec<u8>, ()> { | |
| let mut salt: Vec<u8> = vec![0; salt_length]; | |
| RANDOM.fill(&mut salt).map_err(|_| ())?; | |
| Ok(salt) | |
| } | |
| pub fn hash_password_digest(password: &str, salt: &[u8]) -> Vec<u8> { | |
| let bytes = password.as_bytes(); | |
| let mut output = vec![0; argon2rs::defaults::LENGTH]; | |
| let argon2 = argon2rs::Argon2::default(argon2rs::Variant::Argon2i); | |
| argon2.hash(&mut output, bytes, salt, &[], &[]); | |
| output | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment