1use md5::{Digest, Md5};
10
11pub const ITOA64: &[u8; 64] = b"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
13
14const BLOWFISH_ITOA64: &[u8; 64] = b"./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
16
17fn ord(input: &[u8], i: usize, warn: &mut dyn FnMut(usize)) -> u32 {
19 input.get(i).map_or_else(
20 || {
21 warn(i);
22 0
23 },
24 |b| u32::from(*b),
25 )
26}
27
28fn push(output: &mut Vec<u8>, alphabet: &[u8], i: u32, warn: &mut dyn FnMut(usize)) {
30 let i = i as usize;
31 match alphabet.get(i) {
32 Some(c) => output.push(*c),
33 None => warn(i),
34 }
35}
36
37pub fn encode64(
41 itoa64: &[u8],
42 input: &[u8],
43 count: usize,
44 warn_input: &mut dyn FnMut(usize),
45 warn_itoa64: &mut dyn FnMut(usize),
46) -> Vec<u8> {
47 let mut output = Vec::with_capacity(count.div_ceil(3) * 4);
48 let mut i = 0;
49 loop {
50 let mut value = ord(input, i, warn_input);
51 i += 1;
52 push(&mut output, itoa64, value & 0x3F, warn_itoa64);
53 if i < count {
54 value |= ord(input, i, warn_input) << 8;
55 }
56 push(&mut output, itoa64, (value >> 6) & 0x3F, warn_itoa64);
57 let at = i;
58 i += 1;
59 if at >= count {
60 break;
61 }
62 if i < count {
63 value |= ord(input, i, warn_input) << 16;
64 }
65 push(&mut output, itoa64, (value >> 12) & 0x3F, warn_itoa64);
66 let at = i;
67 i += 1;
68 if at >= count {
69 break;
70 }
71 push(&mut output, itoa64, (value >> 18) & 0x3F, warn_itoa64);
72 if i >= count {
73 break;
74 }
75 }
76 output
77}
78
79pub fn blowfish_salt(input: &[u8], warn: &mut dyn FnMut(usize)) -> Vec<u8> {
82 let mut output = Vec::with_capacity(22);
83 let mut i = 0;
84 loop {
85 let mut c1 = ord(input, i, warn);
86 i += 1;
87 output.push(BLOWFISH_ITOA64[(c1 >> 2) as usize]);
88 c1 = (c1 & 0x03) << 4;
89 if i >= 16 {
90 output.push(BLOWFISH_ITOA64[c1 as usize]);
91 break;
92 }
93 let mut c2 = ord(input, i, warn);
94 i += 1;
95 c1 |= c2 >> 4;
96 output.push(BLOWFISH_ITOA64[c1 as usize]);
97 c1 = (c2 & 0x0F) << 2;
98 c2 = ord(input, i, warn);
99 i += 1;
100 c1 |= c2 >> 6;
101 output.push(BLOWFISH_ITOA64[c1 as usize]);
102 output.push(BLOWFISH_ITOA64[(c2 & 0x3F) as usize]);
103 }
104 output
105}
106
107#[must_use]
109pub fn md5_raw(data: &[u8]) -> [u8; 16] {
110 Md5::digest(data).into()
111}
112
113#[must_use]
116pub fn md5_rounds(salt: &[u8], password: &[u8], count: u64) -> [u8; 16] {
117 let mut hash: [u8; 16] = Md5::new().chain_update(salt).chain_update(password).finalize().into();
118 for _ in 0..count {
119 hash = Md5::new().chain_update(hash).chain_update(password).finalize().into();
120 }
121 hash
122}
123
124#[cfg(test)]
125mod tests {
126 use super::{ITOA64, blowfish_salt, encode64, md5_rounds};
127
128 fn none(_: usize) {
129 panic!("no offset past the end");
130 }
131
132 #[test]
135 fn wordpress_hash() {
136 let setting = b"$P$BkZSVUxvhvrYOqtXfIkMOz2sNVHU8M1";
137 let count_log2 = ITOA64.iter().position(|c| *c == setting[3]).unwrap();
138 let hash = md5_rounds(&setting[4..12], b"password", 1 << count_log2);
139 let mut out = setting[..12].to_vec();
140 out.extend(encode64(ITOA64, &hash, 16, &mut none, &mut none));
141 assert_eq!(out.len(), 34);
142 }
143
144 #[test]
145 fn short_input_reports_offsets() {
146 let mut offsets = Vec::new();
147 let out = encode64(ITOA64, b"ab", 6, &mut |i| offsets.push(i), &mut none);
148 assert_eq!(out.len(), 8);
149 assert_eq!(offsets, vec![2, 3, 4, 5]);
150 let mut offsets = Vec::new();
151 assert_eq!(blowfish_salt(b"", &mut |i| offsets.push(i)).len(), 22);
152 assert_eq!(offsets, (0..16).collect::<Vec<_>>());
153 }
154}