1extern crate base64;
2extern crate crypto;
3extern crate hex;
4
5mod cipher;
6pub mod config;
7pub mod convert;
8pub mod error;
9mod hasher;
10
11#[cfg(test)]
12mod tests {
13 #[test]
14 fn encode_hex() {
15 let hex_string = hex::encode("Hello world!");
16 assert_eq!("48656c6c6f20776f726c6421", hex_string);
17 }
18
19 #[test]
20 fn decode_hex() {
21 let string = String::from_utf8(hex::decode("48656c6c6f20776f726c6421").unwrap()).unwrap();
22 assert_eq!("Hello world!", string);
23 }
24
25 #[test]
26 fn encode_base64() {
27 let base64_string = base64::encode("Hello world");
28 assert_eq!("SGVsbG8gd29ybGQ=", base64_string);
29 }
30
31 #[test]
32 fn decode_base64() {
33 let string = String::from_utf8(base64::decode("SGVsbG8gd29ybGQ=").unwrap()).unwrap();
34 assert_eq!("Hello world", string);
35 }
36
37 #[test]
38 fn crypto_bcrypt_encrypt() {
39 let password = "very secured password";
40 let salt = [0u8; 16];
41 let rounds = 10;
42 let mut output = vec![0u8; 32];
43
44 crypto::bcrypt_pbkdf::bcrypt_pbkdf(password.as_bytes(), &salt, rounds, &mut output);
45
46 assert_eq!("7Dx4QJ551DqRoPToSNyMGn4yEGwSB1/kbC9MLygEZks=", base64::encode(&output));
47 }
48
49 #[test]
50 fn crypto_bcrypt_verify() {
51 let hash = "7Dx4QJ551DqRoPToSNyMGn4yEGwSB1/kbC9MLygEZks=";
52 let password = "very secured password";
53 let salt = [0u8; 16];
54 let rounds = 10;
55 let mut output = vec![0u8; 32];
56
57 crypto::bcrypt_pbkdf::bcrypt_pbkdf(password.as_bytes(), &salt, rounds, &mut output);
58
59 assert_eq!(hash, base64::encode(&output));
60 }
61
62 #[test]
63 fn crypto_pbkdf2_encrypt() {
64 let password = "very secured password";
65 let salt = [0u8; 16];
66 let rounds = 10;
67 let mut output = vec![0u8; 32];
68
69 let mut mac = crypto::hmac::Hmac::new(crypto::sha2::Sha256::new(), password.as_bytes());
70 crypto::pbkdf2::pbkdf2(&mut mac, &salt, rounds, &mut output);
71
72 assert_eq!("VcZDcja5mTzJ02mP5YBLDx88n7hVcFIuOCqj/qXBMkk=", base64::encode(&output));
73 }
74}