Skip to content

Instantly share code, notes, and snippets.

@petejodo
Created January 9, 2019 02:44
Show Gist options
  • Select an option

  • Save petejodo/ee9596c0afb3c78972c0a1cb53e4525d to your computer and use it in GitHub Desktop.

Select an option

Save petejodo/ee9596c0afb3c78972c0a1cb53e4525d to your computer and use it in GitHub Desktop.
Rust Salts and Password Hashing
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