summaryrefslogtreecommitdiff
path: root/src/auth/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/auth/mod.rs')
-rw-r--r--src/auth/mod.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/auth/mod.rs b/src/auth/mod.rs
new file mode 100644
index 0000000..9ef9960
--- /dev/null
+++ b/src/auth/mod.rs
@@ -0,0 +1,32 @@
+extern crate crypto;
+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")));
+ }
+}