summaryrefslogtreecommitdiff
path: root/src/auth/mod.rs
blob: f3db525252e163914f3b2974d9f273c6f630c3e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
extern crate base64;

use crypto::bcrypt_pbkdf::bcrypt_pbkdf;

// TODO: Replace salt with a random string.
// TODO: Configurable number of iterations.
pub fn encode(pw: &str) -> String {
    let salt = "hello";
    let mut enc = vec!(0; 32);
    let encrypted = bcrypt_pbkdf(pw.as_bytes(), salt.as_bytes(),
                                 10, &mut enc);
    format!("${}${}${}", "sdv1",
            base64::encode(salt.as_bytes()), base64::encode(&enc))
}

pub fn validate(pw: &str, enc: &str) -> bool {
    // let cs = enc.split('$');
    // println("{:?}", cs.len());
    // let enc_pw = cs[3];
    encode(pw) == enc
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn it_validates() {
        assert_eq!(false, validate("123", "123"));
        assert_eq!(true, validate("123", &encode("123")));
    }
}