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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use crypto::sha2::Sha256;
use super::{Result, HashSetup, IntoHashSetup, consteq};
use random;
use sha2_crypt::{sha2_crypt, parse_sha2_hash, sha2_hash_with};
pub use sha2_crypt::MIN_ROUNDS;
pub use sha2_crypt::MAX_ROUNDS;
pub use sha2_crypt::DEFAULT_ROUNDS;
pub use sha2_crypt::MAX_SALT_LEN;
const SHA256_MAGIC: &'static str = "$5$";
const SHA256_TRANSPOSE: &'static [u8] = b"\x14\x0a\x00\x0b\x01\x15\x02\x16\x0c\x17\x0d\x03\x0e\x04\x18\x05\
\x19\x0f\x1a\x10\x06\x11\x07\x1b\x08\x1c\x12\x1d\x13\x09\x1e\x1f";
fn do_sha256_crypt(pass: &[u8], salt: &str, rounds: Option<u32>) -> Result<String> {
sha2_crypt(pass, salt, rounds, Sha256::new, SHA256_TRANSPOSE, SHA256_MAGIC)
}
#[deprecated(since="0.2.0", note="don't use this algorithm for new passwords")]
pub fn hash<B: AsRef<[u8]>>(pass: B) -> Result<String> {
let saltstr = random::gen_salt_str(MAX_SALT_LEN)?;
do_sha256_crypt(pass.as_ref(), &saltstr, None)
}
fn parse_sha256_hash(hash: &str) -> Result<HashSetup> {
parse_sha2_hash(hash, SHA256_MAGIC)
}
#[deprecated(since="0.2.0", note="don't use this algorithm for new passwords")]
pub fn hash_with<'a, IHS, B>(param: IHS, pass: B) -> Result<String>
where IHS: IntoHashSetup<'a>, B: AsRef<[u8]>
{
sha2_hash_with(IHS::into_hash_setup(param, parse_sha256_hash)?, pass.as_ref(), do_sha256_crypt)
}
pub fn verify<B: AsRef<[u8]>>(pass: B, hash: &str) -> bool {
#[allow(deprecated)]
consteq(hash, hash_with(hash, pass))
}
#[cfg(test)]
mod tests {
use ::HashSetup;
#[test]
#[allow(deprecated)]
fn custom() {
assert_eq!(super::hash_with(
"$5$rounds=11858$WH1ABM5sKhxbkgCK$aTQsjPkz0rBsH3lQlJxw9HDTDXPKBxC0LlVeV69P.t1", "test").unwrap(),
"$5$rounds=11858$WH1ABM5sKhxbkgCK$aTQsjPkz0rBsH3lQlJxw9HDTDXPKBxC0LlVeV69P.t1");
assert_eq!(super::hash_with(HashSetup { salt: Some("WH1ABM5sKhxbkgCK"), rounds: Some(11858) }, "test").unwrap(),
"$5$rounds=11858$WH1ABM5sKhxbkgCK$aTQsjPkz0rBsH3lQlJxw9HDTDXPKBxC0LlVeV69P.t1");
}
#[test]
#[allow(deprecated)]
fn implicit_dflt_rounds() {
assert_eq!(super::hash_with(
"$5$WH1ABM5sKhxbkgCK$sOnTVjQn1Y3EWibd8gWqqJqjH.KaFrxJE5rijqxcPp7", "test").unwrap(),
"$5$WH1ABM5sKhxbkgCK$sOnTVjQn1Y3EWibd8gWqqJqjH.KaFrxJE5rijqxcPp7");
}
}