Skip to content

Commit 68fd28c

Browse files
committed
Cleanup formatting in std::sha1
This file has suffered a lot of reformats and was looking pretty ragged
1 parent 7b12924 commit 68fd28c

File tree

1 file changed

+15
-35
lines changed

1 file changed

+15
-35
lines changed

src/lib/sha1.rs

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
/*
42
* A SHA-1 implementation derived from Paul E. Jones's reference
53
* implementation, which is written for clarity, not speed. At some
@@ -8,43 +6,29 @@
86
export sha1;
97
export mk_sha1;
108

11-
type sha1 =
9+
type sha1 = obj {
1210
// Provide message input as bytes
13-
14-
11+
fn input(&[u8]);
1512
// Provide message input as string
16-
17-
// Read the digest as a vector of 20 bytes. After
18-
// calling this no further input may provided
19-
// until reset is called
20-
21-
13+
fn input_str(&str);
14+
// Read the digest as a vector of 20 bytes. After calling this no further
15+
// input may provided until reset is called
16+
fn result() -> [u8];
2217
// Same as above, just a hex-string version.
23-
18+
fn result_str() -> str;
2419
// Reset the sha1 state for reuse. This is called
2520
// automatically during construction
26-
obj {
27-
fn input(&[u8]);
28-
fn input_str(&str);
29-
fn result() -> [u8];
30-
fn result_str() -> str;
31-
fn reset();
32-
};
21+
fn reset();
22+
};
3323

3424

3525
// Some unexported constants
3626
const digest_buf_len: uint = 5u;
37-
3827
const msg_block_len: uint = 64u;
39-
4028
const work_buf_len: uint = 80u;
41-
4229
const k0: u32 = 0x5A827999u32;
43-
4430
const k1: u32 = 0x6ED9EBA1u32;
45-
4631
const k2: u32 = 0x8F1BBCDCu32;
47-
4832
const k3: u32 = 0xCA62C1D6u32;
4933

5034

@@ -61,7 +45,6 @@ fn mk_sha1() -> sha1 {
6145

6246
fn add_input(st: &sha1state, msg: &[u8]) {
6347
// FIXME: Should be typestate precondition
64-
6548
assert (!st.computed);
6649
for element: u8 in msg {
6750
st.msg_block[st.msg_block_idx] = element;
@@ -80,14 +63,12 @@ fn mk_sha1() -> sha1 {
8063
}
8164
fn process_msg_block(st: &sha1state) {
8265
// FIXME: Make precondition
83-
8466
assert (vec::len(st.h) == digest_buf_len);
8567
assert (vec::len(st.work_buf) == work_buf_len);
8668
let t: int; // Loop counter
87-
8869
let w = st.work_buf;
89-
// Initialize the first 16 words of the vector w
9070

71+
// Initialize the first 16 words of the vector w
9172
t = 0;
9273
while t < 16 {
9374
let tmp;
@@ -98,8 +79,8 @@ fn mk_sha1() -> sha1 {
9879
w[t] = tmp;
9980
t += 1;
10081
}
101-
// Initialize the rest of vector w
10282

83+
// Initialize the rest of vector w
10384
while t < 80 {
10485
let val = w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16];
10586
w[t] = circular_shift(1u32, val);
@@ -172,6 +153,7 @@ fn mk_sha1() -> sha1 {
172153
}
173154
ret rs;
174155
}
156+
175157
/*
176158
* According to the standard, the message must be padded to an even
177159
* 512 bits. The first padding bit must be a '1'. The last 64 bits
@@ -181,17 +163,15 @@ fn mk_sha1() -> sha1 {
181163
* call process_msg_block() appropriately. When it returns, it
182164
* can be assumed that the message digest has been computed.
183165
*/
184-
185166
fn pad_msg(st: &sha1state) {
186167
// FIXME: Should be a precondition
187-
188168
assert (vec::len(st.msg_block) == msg_block_len);
169+
189170
/*
190171
* Check to see if the current message block is too small to hold
191172
* the initial padding bits and length. If so, we will pad the
192173
* block, process it, and then continue padding into a second block.
193174
*/
194-
195175
if st.msg_block_idx > 55u {
196176
st.msg_block[st.msg_block_idx] = 0x80u8;
197177
st.msg_block_idx += 1u;
@@ -208,8 +188,8 @@ fn mk_sha1() -> sha1 {
208188
st.msg_block[st.msg_block_idx] = 0u8;
209189
st.msg_block_idx += 1u;
210190
}
211-
// Store the message length as the last 8 octets
212191

192+
// Store the message length as the last 8 octets
213193
st.msg_block[56] = st.len_high >> 24u32 & 0xFFu32 as u8;
214194
st.msg_block[57] = st.len_high >> 16u32 & 0xFFu32 as u8;
215195
st.msg_block[58] = st.len_high >> 8u32 & 0xFFu32 as u8;
@@ -223,7 +203,6 @@ fn mk_sha1() -> sha1 {
223203
obj sha1(st: sha1state) {
224204
fn reset() {
225205
// FIXME: Should be typestate precondition
226-
227206
assert (vec::len(st.h) == digest_buf_len);
228207
st.len_low = 0u32;
229208
st.len_high = 0u32;
@@ -257,6 +236,7 @@ fn mk_sha1() -> sha1 {
257236
sh.reset();
258237
ret sh;
259238
}
239+
260240
// Local Variables:
261241
// mode: rust;
262242
// fill-column: 78;

0 commit comments

Comments
 (0)