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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use sha1::{digest::FixedOutputDirty, Digest, Sha1};
use std::ops;
const SHA1_BYTE_LENGTH: usize = 20;
const DYNAMIC_PADDING_LENGTH: usize = 48;
#[derive(Debug, PartialEq, Clone)]
pub struct HashPrefix {
pub data: Vec<u8>,
pub half_byte: Option<u8>,
}
pub struct SearchParams {
pub current_commit: Vec<u8>,
pub desired_prefix: HashPrefix,
pub counter_range: ops::Range<u64>,
}
#[derive(Debug, PartialEq)]
pub struct HashMatch {
pub commit: Vec<u8>,
pub hash: [u8; SHA1_BYTE_LENGTH],
}
#[derive(Debug, PartialEq)]
struct ProcessedCommit {
header: Vec<u8>,
commit: Vec<u8>,
dynamic_padding_start_index: usize,
}
static PADDINGS: [[u8; 8]; 256] = {
let mut paddings = [[0; 8]; 256];
let mut i = 0;
while i < 256 {
let mut j = 0;
while j < 8 {
paddings[i][j] = if i & (0x80 >> j) == 0 { b' ' } else { b'\t' };
j += 1;
}
i += 1;
}
paddings
};
pub fn iterate_for_match(params: &SearchParams) -> Option<HashMatch> {
let ProcessedCommit {
header,
mut commit,
dynamic_padding_start_index,
} = process_commit(¶ms.current_commit);
let cached_sha1_state = Sha1::new()
.chain(&header)
.chain(&commit[0..dynamic_padding_start_index]);
let remaining_commit_data = &mut commit[dynamic_padding_start_index..];
let mut hash_result = Default::default();
for counter in params.counter_range.clone() {
let dynamic_padding_data = &mut remaining_commit_data[0..DYNAMIC_PADDING_LENGTH];
for (padding_chunk, counter_byte) in dynamic_padding_data
.chunks_exact_mut(8)
.zip(counter.to_le_bytes().iter())
{
padding_chunk.copy_from_slice(&PADDINGS[*counter_byte as usize]);
}
let mut sha1_hash = cached_sha1_state.clone();
sha1_hash.update(&remaining_commit_data);
sha1_hash.finalize_into_dirty(&mut hash_result);
if matches_desired_prefix(hash_result.as_ref(), ¶ms.desired_prefix) {
return Some(HashMatch {
commit,
hash: hash_result.into(),
});
}
}
None
}
fn process_commit(original_commit: &[u8]) -> ProcessedCommit {
const DYNAMIC_PADDING_ALIGNMENT: usize = 64;
let commit_split_index = get_commit_split_index(original_commit);
let replaceable_padding_size = original_commit[commit_split_index..]
.iter()
.take_while(|byte| **byte == b' ' || **byte == b'\t')
.count();
let approximate_length_before_static_padding =
format!("commit {}\x00", original_commit.len()).len() + commit_split_index;
let static_padding_length = (DYNAMIC_PADDING_ALIGNMENT
- (approximate_length_before_static_padding % DYNAMIC_PADDING_ALIGNMENT))
% DYNAMIC_PADDING_ALIGNMENT;
let commit_length = original_commit.len() - replaceable_padding_size
+ static_padding_length
+ DYNAMIC_PADDING_LENGTH;
let header = format!("commit {}\x00", commit_length).into_bytes();
let mut commit = Vec::with_capacity(commit_length);
commit.extend(&original_commit[..commit_split_index]);
commit.resize(commit.len() + static_padding_length, b' ');
let dynamic_padding_start_index = commit.len();
assert!((dynamic_padding_start_index + header.len()) % DYNAMIC_PADDING_ALIGNMENT <= 1);
commit.resize(commit.len() + DYNAMIC_PADDING_LENGTH, b'\t');
commit.extend(&original_commit[commit_split_index + replaceable_padding_size..]);
ProcessedCommit {
header,
commit,
dynamic_padding_start_index,
}
}
fn get_commit_split_index(commit: &[u8]) -> usize {
let mut found_gpgsig_line = false;
const SIGNATURE_MARKER: &[u8] = b"-----END PGP SIGNATURE-----";
for index in 0..commit.len() {
if commit[index..].starts_with(b"\ngpgsig ") {
found_gpgsig_line = true;
} else if !found_gpgsig_line && commit[index..].starts_with(b"\n\n") {
break;
} else if found_gpgsig_line && commit[index..].starts_with(SIGNATURE_MARKER) {
return index + SIGNATURE_MARKER.len();
}
}
commit.len()
- commit
.iter()
.rev()
.take_while(|byte| **byte == b' ' || **byte == b'\t' || **byte == b'\n')
.count()
}
fn matches_desired_prefix(hash: &[u8; SHA1_BYTE_LENGTH], prefix: &HashPrefix) -> bool {
prefix.data == hash[..prefix.data.len()]
&& match prefix.half_byte {
Some(half_byte) => (hash[prefix.data.len()] & 0xf0) == half_byte,
None => true,
}
}
pub fn parse_prefix(prefix: &str) -> Option<HashPrefix> {
if prefix.len() > SHA1_BYTE_LENGTH * 2 {
return None;
}
let mut data = Vec::new();
for index in 0..(prefix.len() / 2) {
match u8::from_str_radix(&prefix[2 * index..2 * index + 2], 16) {
Ok(value) => data.push(value),
Err(_) => return None,
}
}
Some(HashPrefix {
data,
half_byte: if prefix.len() % 2 == 1 {
match u8::from_str_radix(&prefix[prefix.len() - 1..], 16) {
Ok(value) => Some(value << 4),
Err(_) => return None,
}
} else {
None
},
})
}
#[cfg(test)]
mod tests;