@@ -14,6 +14,8 @@ type sha1 =
14
14
// Provide message input as bytes
15
15
fn input ( & vec[ u8 ] ) ;
16
16
17
+ fn input_ivec ( & u8 [ ] ) ;
18
+
17
19
// Provide message input as string
18
20
fn input_str ( & str ) ;
19
21
@@ -22,6 +24,8 @@ type sha1 =
22
24
// until reset is called
23
25
fn result ( ) -> vec[ u8 ] ;
24
26
27
+ fn result_ivec ( ) -> u8 [ ] ;
28
+
25
29
// Same as above, just a hex-string version.
26
30
fn result_str ( ) -> str ;
27
31
@@ -50,15 +54,15 @@ const u32 k3 = 0xCA62C1D6u32;
50
54
// Builds a sha1 object
51
55
fn mk_sha1( ) -> sha1 {
52
56
type sha1state =
53
- rec ( vec [ mutable u32 ] h ,
57
+ rec ( u32 [ mutable] h ,
54
58
mutable u32 len_low,
55
59
mutable u32 len_high ,
56
- vec [ mutable u8 ] msg_block ,
60
+ u8 [ mutable] msg_block ,
57
61
mutable uint msg_block_idx ,
58
62
mutable bool computed ,
59
- vec [ mutable u32 ] work_buf ) ;
63
+ u32 [ mutable] work_buf ) ;
60
64
61
- fn add_input ( & sha1state st, & vec [ u8 ] msg ) {
65
+ fn add_input( & sha1state st, & u8 [ ] msg) {
62
66
// FIXME: Should be typestate precondition
63
67
64
68
assert ( !st. computed ) ;
@@ -80,8 +84,8 @@ fn mk_sha1() -> sha1 {
80
84
fn process_msg_block( & sha1state st) {
81
85
// FIXME: Make precondition
82
86
83
- assert ( vec :: len ( st. h ) == digest_buf_len) ;
84
- assert ( vec :: len ( st. work_buf ) == work_buf_len) ;
87
+ assert ( ivec :: len ( st. h ) == digest_buf_len) ;
88
+ assert ( ivec :: len ( st. work_buf ) == work_buf_len) ;
85
89
let int t; // Loop counter
86
90
87
91
auto w = st. work_buf ;
@@ -160,15 +164,15 @@ fn mk_sha1() -> sha1 {
160
164
fn circular_shift ( u32 bits , u32 word ) -> u32 {
161
165
ret word << bits | word >> 32u32 - bits;
162
166
}
163
- fn mk_result ( & sha1state st) -> vec [ u8 ] {
167
+ fn mk_result ( & sha1state st) -> u8 [ ] {
164
168
if ( !st. computed ) { pad_msg ( st) ; st. computed = true ; }
165
- let vec [ u8 ] rs = [ ] ;
169
+ let u8 [ ] rs = ~ [ ] ;
166
170
for ( u32 hpart in st. h) {
167
171
auto a = hpart >> 24u32 & 0xFFu32 as u8 ;
168
172
auto b = hpart >> 16u32 & 0xFFu32 as u8;
169
173
auto c = hpart >> 8u32 & 0xFFu32 as u8 ;
170
174
auto d = hpart & 0xFFu32 as u8;
171
- rs += [ a, b, c, d] ;
175
+ rs += ~ [ a, b, c, d] ;
172
176
}
173
177
ret rs;
174
178
}
@@ -185,7 +189,7 @@ fn mk_sha1() -> sha1 {
185
189
fn pad_msg ( & sha1state st) {
186
190
// FIXME: Should be a precondition
187
191
188
- assert ( vec :: len ( st. msg_block ) == msg_block_len) ;
192
+ assert ( ivec :: len ( st. msg_block ) == msg_block_len) ;
189
193
/*
190
194
* Check to see if the current message block is too small to hold
191
195
* the initial padding bits and length. If so, we will pad the
@@ -224,7 +228,7 @@ fn mk_sha1() -> sha1 {
224
228
fn reset ( ) {
225
229
// FIXME: Should be typestate precondition
226
230
227
- assert ( vec :: len ( st. h ) == digest_buf_len) ;
231
+ assert ( ivec :: len ( st. h ) == digest_buf_len) ;
228
232
st. len_low = 0u32 ;
229
233
st. len_high = 0u32 ;
230
234
st. msg_block_idx = 0 u;
@@ -235,9 +239,20 @@ fn mk_sha1() -> sha1 {
235
239
st. h . ( 4 ) = 0xC3D2E1F0u32 ;
236
240
st. computed = false ;
237
241
}
238
- fn input ( & vec[ u8] msg ) { add_input ( st, msg) ; }
239
- fn input_str ( & str msg) { add_input ( st, str:: bytes ( msg) ) ; }
240
- fn result ( ) -> vec[ u8 ] { ret mk_result ( st) ; }
242
+ fn input ( & vec[ u8] msg ) {
243
+ auto m = ~[ ] ;
244
+ for ( u8 b in msg) { m += ~[ b] ; }
245
+ add_input ( st, m) ;
246
+ }
247
+ fn input_ivec( & u8 [ ] msg) { add_input ( st , msg ) ; }
248
+ fn input_str( & str msg) { add_input ( st , str:: bytes_ivec ( msg ) ) ; }
249
+ fn result ( ) -> vec[ u8 ] {
250
+ auto rivec = mk_result ( st ) ;
251
+ auto rvec = [ ] ;
252
+ for ( u8 b in rivec) { rvec += [ b] ; }
253
+ ret rvec;
254
+ }
255
+ fn result_ivec ( ) -> u8 [ ] { ret mk_result ( st) ; }
241
256
fn result_str ( ) -> str {
242
257
auto r = mk_result ( st) ;
243
258
auto s = "" ;
@@ -246,13 +261,13 @@ fn mk_sha1() -> sha1 {
246
261
}
247
262
}
248
263
auto st =
249
- rec ( h=vec :: init_elt_mut[ u32] ( 0u32 , digest_buf_len) ,
264
+ rec ( h=ivec :: init_elt_mut[ u32] ( 0u32 , digest_buf_len) ,
250
265
mutable len_low=0u32 ,
251
266
mutable len_high=0u32 ,
252
- msg_block=vec :: init_elt_mut[ u8] ( 0u8 , msg_block_len) ,
267
+ msg_block=ivec :: init_elt_mut[ u8] ( 0u8 , msg_block_len) ,
253
268
mutable msg_block_idx=0 u,
254
269
mutable computed=false ,
255
- work_buf=vec :: init_elt_mut[ u32] ( 0u32 , work_buf_len) ) ;
270
+ work_buf=ivec :: init_elt_mut[ u32] ( 0u32 , work_buf_len) ) ;
256
271
auto sh = sha1 ( st) ;
257
272
sh. reset ( ) ;
258
273
ret sh;
0 commit comments