Convert std::sha1 and std::rand over to ifaces

This commit is contained in:
Marijn Haverbeke 2012-01-11 12:54:39 +01:00
parent 26610dbbc8
commit 4f76db43e6
2 changed files with 40 additions and 38 deletions

View file

@ -18,7 +18,7 @@ Obj: rng
A random number generator
*/
type rng = obj {
iface rng {
/*
Method: next
@ -46,7 +46,7 @@ type rng = obj {
Return a random byte string.
*/
fn gen_bytes(len: uint) -> [u8];
};
}
resource rand_res(c: rustrt::rctx) { rustrt::rand_free(c); }
@ -58,12 +58,12 @@ Function: mk_rng
Create a random number generator
*/
fn mk_rng() -> rng {
obj rt_rng(c: @rand_res) {
fn next() -> u32 { ret rustrt::rand_next(**c); }
impl of rng for @rand_res {
fn next() -> u32 { ret rustrt::rand_next(**self); }
fn next_float() -> float {
let u1 = rustrt::rand_next(**c) as float;
let u2 = rustrt::rand_next(**c) as float;
let u3 = rustrt::rand_next(**c) as float;
let u1 = rustrt::rand_next(**self) as float;
let u2 = rustrt::rand_next(**self) as float;
let u3 = rustrt::rand_next(**self) as float;
let scale = u32::max_value as float;
ret ((u1 / scale + u2) / scale + u3) / scale;
}
@ -74,7 +74,7 @@ fn mk_rng() -> rng {
let s = "";
let i = 0u;
while (i < len) {
let n = rustrt::rand_next(**c) as uint %
let n = rustrt::rand_next(**self) as uint %
str::char_len(charset);
s = s + str::from_char(str::char_at(charset, n));
i += 1u;
@ -85,14 +85,14 @@ fn mk_rng() -> rng {
let v = [];
let i = 0u;
while i < len {
let n = rustrt::rand_next(**c) as uint;
let n = rustrt::rand_next(**self) as uint;
v += [(n % (u8::max_value as uint)) as u8];
i += 1u;
}
v
}
}
ret rt_rng(@rand_res(rustrt::rand_new()));
@rand_res(rustrt::rand_new()) as rng
}
// Local Variables:
// mode: rust;

View file

@ -25,11 +25,11 @@ export mk_sha1;
/* Section: Types */
/*
Obj: sha1
Iface: sha1
The SHA-1 object
The SHA-1 interface
*/
type sha1 = obj {
iface sha1 {
/*
Method: input
@ -62,7 +62,7 @@ type sha1 = obj {
Reset the SHA-1 state for reuse
*/
fn reset();
};
}
/* Section: Operations */
@ -248,39 +248,41 @@ fn mk_sha1() -> sha1 {
st.msg_block[63] = st.len_low & 0xFFu32 as u8;
process_msg_block(st);
}
obj sha1(st: sha1state) {
impl of sha1 for sha1state {
fn reset() {
// FIXME: Should be typestate precondition
assert (vec::len(st.h) == digest_buf_len);
st.len_low = 0u32;
st.len_high = 0u32;
st.msg_block_idx = 0u;
st.h[0] = 0x67452301u32;
st.h[1] = 0xEFCDAB89u32;
st.h[2] = 0x98BADCFEu32;
st.h[3] = 0x10325476u32;
st.h[4] = 0xC3D2E1F0u32;
st.computed = false;
assert (vec::len(self.h) == digest_buf_len);
self.len_low = 0u32;
self.len_high = 0u32;
self.msg_block_idx = 0u;
self.h[0] = 0x67452301u32;
self.h[1] = 0xEFCDAB89u32;
self.h[2] = 0x98BADCFEu32;
self.h[3] = 0x10325476u32;
self.h[4] = 0xC3D2E1F0u32;
self.computed = false;
}
fn input(msg: [u8]) { add_input(st, msg); }
fn input_str(msg: str) { add_input(st, str::bytes(msg)); }
fn result() -> [u8] { ret mk_result(st); }
fn input(msg: [u8]) { add_input(self, msg); }
fn input_str(msg: str) { add_input(self, str::bytes(msg)); }
fn result() -> [u8] { ret mk_result(self); }
fn result_str() -> str {
let r = mk_result(st);
let r = mk_result(self);
let s = "";
for b: u8 in r { s += uint::to_str(b as uint, 16u); }
ret s;
}
}
let st =
{h: vec::init_elt_mut::<u32>(0u32, digest_buf_len),
mutable len_low: 0u32,
mutable len_high: 0u32,
msg_block: vec::init_elt_mut::<u8>(0u8, msg_block_len),
mutable msg_block_idx: 0u,
mutable computed: false,
work_buf: vec::init_elt_mut::<u32>(0u32, work_buf_len)};
let sh = sha1(st);
let st = {
h: vec::init_elt_mut(0u32, digest_buf_len),
mutable len_low: 0u32,
mutable len_high: 0u32,
msg_block: vec::init_elt_mut(0u8, msg_block_len),
mutable msg_block_idx: 0u,
mutable computed: false,
work_buf: vec::init_elt_mut(0u32, work_buf_len)
};
let sh = st as sha1;
sh.reset();
ret sh;
}