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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
extern crate data_encoding;
#[macro_use]
extern crate error_chain;
#[macro_use]
extern crate serde;
extern crate serde_bytes;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
pub mod de;
pub use de::{from_str, McfDeserializer};
mod encoding;
pub use encoding::base64;
pub use encoding::base64bcrypt;
pub mod ser;
pub use ser::{to_string, McfSerializer};
pub use serde_json::{Map, Value};
#[derive(Debug, Deserialize, Serialize)]
pub struct McfHash {
pub algorithm: Hashes,
pub parameters: Map<String, Value>,
#[serde(with = "base64")]
pub salt: Vec<u8>,
#[serde(with = "base64")]
pub hash: Vec<u8>,
}
pub mod legacy {
use super::*;
#[derive(Debug, Deserialize, Serialize)]
pub struct BcryptHash {
algorithm: Hashes,
cost: u8,
#[serde(with = "base64bcrypt")]
salthash: (Vec<u8>, Vec<u8>),
}
impl Into<McfHash> for BcryptHash {
fn into(self) -> McfHash {
let mut params = Map::<String, Value>::new();
params.insert("cost".to_string(), Value::Number(self.cost.into()));
McfHash {
algorithm: self.algorithm,
parameters: params,
salt: self.salthash.0,
hash: self.salthash.1,
}
}
}
}
macro_rules! enum_hashes {
($($hash:ident = $val:expr,)*) => (
#[derive(Debug, Deserialize, PartialEq, Serialize)]
pub enum Hashes {
$(
#[serde(rename = $val)]
$hash,
)*
}
impl Hashes {
pub fn from_id(id: &str) -> Option<Hashes> {
match id {
$(
$val => Some(Hashes::$hash),
)*
_ => None
}
}
pub fn to_id(&self) -> &'static str {
match *self {
$(
Hashes::$hash => $val,
)*
}
}
}
)
}
enum_hashes!{
Md5Crypt = "1",
Bcrypt = "2",
Bcrypta = "2a",
Bcryptx = "2x",
Bcrypty = "2y",
Bcryptb = "2b",
BcryptMcf = "2y-mcf",
BsdNtHash = "3",
Sha256Crypt = "5",
Sha512Crypt = "6",
SunMd5Crypt = "md5",
Sha1Crypt = "sha1",
AprMd5Crypt = "apr1",
Argon2i = "argon2i",
Argon2d = "argon2d",
BcryptSha256 = "bcrypt-sha256",
Phpassp = "P",
Phpassh = "H",
Pbkdf2Sha1 = "pbkdf2",
Pbkdf2Sha256 = "pbkdf2-sha256",
Pbkdf2Sha512 = "pbkdf2-sha512",
Scram = "scram",
CtaPbkdf2Sha1 = "p5k2",
Scrypt = "scrypt",
Hmac = "hmac",
Custom = "custom",
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_all() {
let argon_hash = "$argon2i$m=262144,p=1,t=2$c29tZXNhbHQ\
$Pmiaqj0op3zyvHKlGsUxZnYXURgvHuKS4/Z3p9pMJGc";
let bcrypt_hash = "$2a$10$ckjEeyTD6estWyoofn4EROM9Ik2PqVcfcrepX.uGp6.aqRdCMN/Oe";
let argon: McfHash = from_str(argon_hash).unwrap();
println!("{:?}", argon);
println!("In JSON: {}", serde_json::to_string_pretty(&argon).unwrap());
assert_eq!(to_string(&argon).unwrap(), argon_hash);
let bcrypt: legacy::BcryptHash = from_str(bcrypt_hash).unwrap();
println!("{:?}", bcrypt);
println!("In JSON: {}",
serde_json::to_string_pretty(&bcrypt).unwrap());
let updated: McfHash = bcrypt.into();
println!("In JSON: {}",
serde_json::to_string_pretty(&updated).unwrap());
}
#[test]
fn test_trial_deserialize() {
#[derive(Deserialize)]
#[serde(untagged)]
enum BcryptOrArgon {
Argon(McfHash),
Bcrypt(legacy::BcryptHash),
}
let argon_hash = "$argon2i$m=262144,p=1,t=2$c29tZXNhbHQ\
$Pmiaqj0op3zyvHKlGsUxZnYXURgvHuKS4/Z3p9pMJGc";
let bcrypt_hash = "$2a$10$ckjEeyTD6estWyoofn4EROM9Ik2PqVcfcrepX.uGp6.aqRdCMN/Oe";
let argon = {
match from_str::<McfHash>(argon_hash) {
Ok(v) => BcryptOrArgon::Argon(v),
Err(_) => {
let v = from_str::<legacy::BcryptHash>(argon_hash).unwrap();
BcryptOrArgon::Bcrypt(v)
}
}
};
assert!(if let BcryptOrArgon::Argon(_) = argon {
true
} else {
false
});
let bcrypt = {
match from_str::<McfHash>(bcrypt_hash) {
Ok(v) => BcryptOrArgon::Argon(v),
Err(_) => {
let v = from_str::<legacy::BcryptHash>(bcrypt_hash).unwrap();
BcryptOrArgon::Bcrypt(v)
}
}
};
assert!(if let BcryptOrArgon::Bcrypt(_) = bcrypt {
true
} else {
false
});
}
}