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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
extern crate base64;
extern crate crypto;
extern crate hex;

mod cipher;
pub mod config;
pub mod convert;
pub mod error;
mod hasher;

#[cfg(test)]
mod tests {
  #[test]
  fn encode_hex() {
    let hex_string = hex::encode("Hello world!");
    assert_eq!("48656c6c6f20776f726c6421", hex_string);
  }

  #[test]
  fn decode_hex() {
    let string = String::from_utf8(hex::decode("48656c6c6f20776f726c6421").unwrap()).unwrap();
    assert_eq!("Hello world!", string);
  }

  #[test]
  fn encode_base64() {
    let base64_string = base64::encode("Hello world");
    assert_eq!("SGVsbG8gd29ybGQ=", base64_string);
  }

  #[test]
  fn decode_base64() {
    let string = String::from_utf8(base64::decode("SGVsbG8gd29ybGQ=").unwrap()).unwrap();
    assert_eq!("Hello world", string);
  }

  #[test]
  fn crypto_bcrypt_encrypt() {
    let password = "very secured password";
    let salt = [0u8; 16];
    let rounds = 10;
    let mut output = vec![0u8; 32];

    crypto::bcrypt_pbkdf::bcrypt_pbkdf(password.as_bytes(), &salt, rounds, &mut output);

    assert_eq!("7Dx4QJ551DqRoPToSNyMGn4yEGwSB1/kbC9MLygEZks=", base64::encode(&output));
  }

  #[test]
  fn crypto_bcrypt_verify() {
    let hash = "7Dx4QJ551DqRoPToSNyMGn4yEGwSB1/kbC9MLygEZks=";
    let password = "very secured password";
    let salt = [0u8; 16];
    let rounds = 10;
    let mut output = vec![0u8; 32];

    crypto::bcrypt_pbkdf::bcrypt_pbkdf(password.as_bytes(), &salt, rounds, &mut output);

    assert_eq!(hash, base64::encode(&output));
  }

  #[test]
  fn crypto_pbkdf2_encrypt() {
    let password = "very secured password";
    let salt = [0u8; 16];
    let rounds = 10;
    let mut output = vec![0u8; 32];

    let mut mac = crypto::hmac::Hmac::new(crypto::sha2::Sha256::new(), password.as_bytes());
    crypto::pbkdf2::pbkdf2(&mut mac, &salt, rounds, &mut output);

    assert_eq!("VcZDcja5mTzJ02mP5YBLDx88n7hVcFIuOCqj/qXBMkk=", base64::encode(&output));
  }
}