aboutsummaryrefslogtreecommitdiff
path: root/src/crypto.rs
diff options
context:
space:
mode:
authorDaniel García <[email protected]>2018-02-10 01:00:55 +0100
committerDaniel García <[email protected]>2018-02-10 01:00:55 +0100
commit5cd40c63ed230bee6dd54b79a9a7e768ad87978f (patch)
treeae576af977bc6aa425412a49c4b73e0b7a7f30d7 /src/crypto.rs
downloadvaultwarden-5cd40c63ed230bee6dd54b79a9a7e768ad87978f.tar.gz
vaultwarden-5cd40c63ed230bee6dd54b79a9a7e768ad87978f.zip
First working version
Diffstat (limited to 'src/crypto.rs')
-rw-r--r--src/crypto.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/crypto.rs b/src/crypto.rs
new file mode 100644
index 00000000..645ee903
--- /dev/null
+++ b/src/crypto.rs
@@ -0,0 +1,36 @@
+///
+/// PBKDF2 derivation
+///
+
+use ring::{digest, pbkdf2};
+
+static DIGEST_ALG: &digest::Algorithm = &digest::SHA256;
+const OUTPUT_LEN: usize = digest::SHA256_OUTPUT_LEN;
+
+pub fn hash_password(secret: &[u8], salt: &[u8], iterations: u32) -> Vec<u8> {
+ let mut out = vec![0u8; OUTPUT_LEN]; // Initialize array with zeros
+
+ pbkdf2::derive(DIGEST_ALG, iterations, salt, secret, &mut out);
+
+ out
+}
+
+pub fn verify_password_hash(secret: &[u8], salt: &[u8], previous: &[u8], iterations: u32) -> bool {
+ pbkdf2::verify(DIGEST_ALG, iterations, salt, secret, previous).is_ok()
+}
+
+///
+/// Random values
+///
+
+pub fn get_random_64() -> Vec<u8> {
+ get_random(vec![0u8; 64])
+}
+
+pub fn get_random(mut array: Vec<u8>) -> Vec<u8> {
+ use ring::rand::{SecureRandom, SystemRandom};
+
+ SystemRandom::new().fill(&mut array);
+
+ array
+}