Skip to content

Commit 4f326dd

Browse files
committed
Remove deprecated modes from SHA1 and MD4 in libstd
1 parent b260844 commit 4f326dd

File tree

2 files changed

+46
-37
lines changed

2 files changed

+46
-37
lines changed

src/libstd/md4.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
fn md4(msg: ~[u8]) -> {a: u32, b: u32, c: u32, d: u32} {
1+
#[forbid(deprecated_mode)];
2+
#[forbid(deprecated_pattern)];
3+
4+
fn md4(msg: &[u8]) -> {a: u32, b: u32, c: u32, d: u32} {
25
// subtle: if orig_len is merely uint, then the code below
36
// which performs shifts by 32 bits or more has undefined
47
// results.
58
let orig_len: u64 = (vec::len(msg) * 8u) as u64;
69

710
// pad message
8-
let mut msg = vec::append(msg, ~[0x80u8]);
11+
let mut msg = vec::append(vec::from_slice(msg), ~[0x80u8]);
912
let mut bitlen = orig_len + 8u64;
1013
while (bitlen + 64u64) % 512u64 > 0u64 {
1114
vec::push(msg, 0u8);
@@ -82,7 +85,7 @@ fn md4(msg: ~[u8]) -> {a: u32, b: u32, c: u32, d: u32} {
8285
return {a: a, b: b, c: c, d: d};
8386
}
8487

85-
fn md4_str(msg: ~[u8]) -> ~str {
88+
fn md4_str(msg: &[u8]) -> ~str {
8689
let {a, b, c, d} = md4(msg);
8790
fn app(a: u32, b: u32, c: u32, d: u32, f: fn(u32)) {
8891
f(a); f(b); f(c); f(d);
@@ -100,7 +103,7 @@ fn md4_str(msg: ~[u8]) -> ~str {
100103
result
101104
}
102105

103-
fn md4_text(msg: ~str) -> ~str { md4_str(str::to_bytes(msg)) }
106+
fn md4_text(msg: &str) -> ~str { md4_str(str::to_bytes(msg)) }
104107

105108
#[test]
106109
fn test_md4() {

src/libstd/sha1.rs

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
* the `reset` method.
1313
*/
1414

15+
#[forbid(deprecated_mode)];
16+
#[forbid(deprecated_pattern)];
17+
1518
/*
1619
* A SHA-1 implementation derived from Paul E. Jones's reference
1720
* implementation, which is written for clarity, not speed. At some
@@ -22,9 +25,9 @@ export sha1;
2225
/// The SHA-1 interface
2326
trait sha1 {
2427
/// Provide message input as bytes
25-
fn input(~[u8]);
28+
fn input((&[u8]));
2629
/// Provide message input as string
27-
fn input_str(~str);
30+
fn input_str((&str));
2831
/**
2932
* Read the digest as a vector of 20 bytes. After calling this no further
3033
* input may be provided until reset is called.
@@ -60,7 +63,7 @@ fn sha1() -> sha1 {
6063
mut computed: bool,
6164
work_buf: @~[mut u32]};
6265

63-
fn add_input(st: sha1state, msg: ~[u8]) {
66+
fn add_input(st: &sha1state, msg: &[u8]) {
6467
assert (!st.computed);
6568
for vec::each(msg) |element| {
6669
st.msg_block[st.msg_block_idx] = element;
@@ -76,7 +79,7 @@ fn sha1() -> sha1 {
7679
if st.msg_block_idx == msg_block_len { process_msg_block(st); }
7780
}
7881
}
79-
fn process_msg_block(st: sha1state) {
82+
fn process_msg_block(st: &sha1state) {
8083
assert (vec::len(st.h) == digest_buf_len);
8184
assert (vec::len(*st.work_buf) == work_buf_len);
8285
let mut t: int; // Loop counter
@@ -155,10 +158,10 @@ fn sha1() -> sha1 {
155158
fn circular_shift(bits: u32, word: u32) -> u32 {
156159
return word << bits | word >> 32u32 - bits;
157160
}
158-
fn mk_result(st: sha1state) -> ~[u8] {
159-
if !st.computed { pad_msg(st); st.computed = true; }
161+
fn mk_result(st: &sha1state) -> ~[u8] {
162+
if !(*st).computed { pad_msg(st); (*st).computed = true; }
160163
let mut rs: ~[u8] = ~[];
161-
for vec::each_mut(st.h) |ptr_hpart| {
164+
for vec::each_mut((*st).h) |ptr_hpart| {
162165
let hpart = *ptr_hpart;
163166
let a = (hpart >> 24u32 & 0xFFu32) as u8;
164167
let b = (hpart >> 16u32 & 0xFFu32) as u8;
@@ -178,40 +181,40 @@ fn sha1() -> sha1 {
178181
* call process_msg_block() appropriately. When it returns, it
179182
* can be assumed that the message digest has been computed.
180183
*/
181-
fn pad_msg(st: sha1state) {
182-
assert (vec::len(st.msg_block) == msg_block_len);
184+
fn pad_msg(st: &sha1state) {
185+
assert (vec::len((*st).msg_block) == msg_block_len);
183186

184187
/*
185188
* Check to see if the current message block is too small to hold
186189
* the initial padding bits and length. If so, we will pad the
187190
* block, process it, and then continue padding into a second block.
188191
*/
189-
if st.msg_block_idx > 55u {
190-
st.msg_block[st.msg_block_idx] = 0x80u8;
191-
st.msg_block_idx += 1u;
192-
while st.msg_block_idx < msg_block_len {
193-
st.msg_block[st.msg_block_idx] = 0u8;
194-
st.msg_block_idx += 1u;
192+
if (*st).msg_block_idx > 55u {
193+
(*st).msg_block[(*st).msg_block_idx] = 0x80u8;
194+
(*st).msg_block_idx += 1u;
195+
while (*st).msg_block_idx < msg_block_len {
196+
(*st).msg_block[(*st).msg_block_idx] = 0u8;
197+
(*st).msg_block_idx += 1u;
195198
}
196199
process_msg_block(st);
197200
} else {
198-
st.msg_block[st.msg_block_idx] = 0x80u8;
199-
st.msg_block_idx += 1u;
201+
(*st).msg_block[(*st).msg_block_idx] = 0x80u8;
202+
(*st).msg_block_idx += 1u;
200203
}
201-
while st.msg_block_idx < 56u {
202-
st.msg_block[st.msg_block_idx] = 0u8;
203-
st.msg_block_idx += 1u;
204+
while (*st).msg_block_idx < 56u {
205+
(*st).msg_block[(*st).msg_block_idx] = 0u8;
206+
(*st).msg_block_idx += 1u;
204207
}
205208

206209
// Store the message length as the last 8 octets
207-
st.msg_block[56] = (st.len_high >> 24u32 & 0xFFu32) as u8;
208-
st.msg_block[57] = (st.len_high >> 16u32 & 0xFFu32) as u8;
209-
st.msg_block[58] = (st.len_high >> 8u32 & 0xFFu32) as u8;
210-
st.msg_block[59] = (st.len_high & 0xFFu32) as u8;
211-
st.msg_block[60] = (st.len_low >> 24u32 & 0xFFu32) as u8;
212-
st.msg_block[61] = (st.len_low >> 16u32 & 0xFFu32) as u8;
213-
st.msg_block[62] = (st.len_low >> 8u32 & 0xFFu32) as u8;
214-
st.msg_block[63] = (st.len_low & 0xFFu32) as u8;
210+
(*st).msg_block[56] = ((*st).len_high >> 24u32 & 0xFFu32) as u8;
211+
(*st).msg_block[57] = ((*st).len_high >> 16u32 & 0xFFu32) as u8;
212+
(*st).msg_block[58] = ((*st).len_high >> 8u32 & 0xFFu32) as u8;
213+
(*st).msg_block[59] = ((*st).len_high & 0xFFu32) as u8;
214+
(*st).msg_block[60] = ((*st).len_low >> 24u32 & 0xFFu32) as u8;
215+
(*st).msg_block[61] = ((*st).len_low >> 16u32 & 0xFFu32) as u8;
216+
(*st).msg_block[62] = ((*st).len_low >> 8u32 & 0xFFu32) as u8;
217+
(*st).msg_block[63] = ((*st).len_low & 0xFFu32) as u8;
215218
process_msg_block(st);
216219
}
217220

@@ -228,13 +231,16 @@ fn sha1() -> sha1 {
228231
self.h[4] = 0xC3D2E1F0u32;
229232
self.computed = false;
230233
}
231-
fn input(msg: ~[u8]) { add_input(self, msg); }
232-
fn input_str(msg: ~str) { add_input(self, str::to_bytes(msg)); }
233-
fn result() -> ~[u8] { return mk_result(self); }
234+
fn input(msg: &[u8]) { add_input(&self, msg); }
235+
fn input_str(msg: &str) {
236+
let bs = str::to_bytes(msg);
237+
add_input(&self, bs);
238+
}
239+
fn result() -> ~[u8] { return mk_result(&self); }
234240
fn result_str() -> ~str {
235-
let r = mk_result(self);
241+
let rr = mk_result(&self);
236242
let mut s = ~"";
237-
for vec::each(r) |b| { s += uint::to_str(b as uint, 16u); }
243+
for vec::each(rr) |b| { s += uint::to_str(b as uint, 16u); }
238244
return s;
239245
}
240246
}

0 commit comments

Comments
 (0)