Update for latest master (0.9-pre 90d06ec)

This commit is contained in:
Kevin Ballard 2013-11-28 18:35:04 -08:00
parent 9ea9c195e2
commit 7c05f58ac7
7 changed files with 48 additions and 48 deletions

View File

@ -72,11 +72,11 @@ impl Hasher {
/// Update this hasher with more input bytes /// Update this hasher with more input bytes
pub fn update(&self, data: &[u8]) { pub fn update(&self, data: &[u8]) {
do data.as_imm_buf |pdata, len| { data.as_imm_buf(|pdata, len| {
unsafe { unsafe {
libcrypto::EVP_DigestUpdate(self.ctx, pdata, len as c_uint) libcrypto::EVP_DigestUpdate(self.ctx, pdata, len as c_uint)
} }
} });
} }
/** /**
@ -85,11 +85,11 @@ impl Hasher {
*/ */
pub fn final(&self) -> ~[u8] { pub fn final(&self) -> ~[u8] {
let mut res = vec::from_elem(self.len, 0u8); let mut res = vec::from_elem(self.len, 0u8);
do res.as_mut_buf |pres, _len| { res.as_mut_buf(|pres, _len| {
unsafe { unsafe {
libcrypto::EVP_DigestFinal(self.ctx, pres, ptr::null()); libcrypto::EVP_DigestFinal(self.ctx, pres, ptr::null());
} }
} });
res res
} }
} }

4
hex.rs
View File

@ -23,7 +23,7 @@ pub trait ToHex {
impl<'self> ToHex for &'self [u8] { impl<'self> ToHex for &'self [u8] {
fn to_hex(&self) -> ~str { fn to_hex(&self) -> ~str {
let chars = "0123456789ABCDEF".iter().collect::<~[char]>(); let chars = "0123456789ABCDEF".chars().collect::<~[char]>();
let mut s = ~""; let mut s = ~"";
@ -50,7 +50,7 @@ impl<'self> FromHex for &'self str {
fn from_hex(&self) -> ~[u8] { fn from_hex(&self) -> ~[u8] {
let mut vec = vec::with_capacity(self.len() / 2); let mut vec = vec::with_capacity(self.len() / 2);
for (i,c) in self.iter().enumerate() { for (i,c) in self.chars().enumerate() {
let nibble = let nibble =
if c >= '0' && c <= '9' { (c as u8) - 0x30 } if c >= '0' && c <= '9' { (c as u8) - 0x30 }
else if c >= 'a' && c <= 'f' { (c as u8) - (0x61 - 10) } else if c >= 'a' && c <= 'f' { (c as u8) - (0x61 - 10) }

View File

@ -67,9 +67,9 @@ pub fn HMAC(ht: HashType, key: ~[u8]) -> HMAC {
impl HMAC { impl HMAC {
pub fn update(&mut self, data: &[u8]) { pub fn update(&mut self, data: &[u8]) {
unsafe { unsafe {
do data.as_imm_buf |pdata, len| { data.as_imm_buf(|pdata, len| {
HMAC_Update(&mut self.ctx, pdata, len as libc::c_uint) HMAC_Update(&mut self.ctx, pdata, len as libc::c_uint)
} });
} }
} }
@ -77,10 +77,10 @@ impl HMAC {
unsafe { unsafe {
let mut res = vec::from_elem(self.len, 0u8); let mut res = vec::from_elem(self.len, 0u8);
let mut outlen: libc::c_uint = 0; let mut outlen: libc::c_uint = 0;
do res.as_mut_buf |pres, _len| { res.as_mut_buf(|pres, _len| {
HMAC_Final(&mut self.ctx, pres, &mut outlen); HMAC_Final(&mut self.ctx, pres, &mut outlen);
assert!(self.len == outlen as uint) assert!(self.len == outlen as uint)
} });
res res
} }
} }

View File

@ -19,11 +19,11 @@ pub fn pbkdf2_hmac_sha1(pass: &str, salt: &[u8], iter: uint,
assert!(iter >= 1u); assert!(iter >= 1u);
assert!(keylen >= 1u); assert!(keylen >= 1u);
do pass.as_imm_buf |pass_buf, pass_len| { pass.as_imm_buf(|pass_buf, pass_len| {
do salt.as_imm_buf |salt_buf, salt_len| { salt.as_imm_buf(|salt_buf, salt_len| {
let mut out = vec::with_capacity(keylen); let mut out = vec::with_capacity(keylen);
do out.as_mut_buf |out_buf, _out_len| { out.as_mut_buf(|out_buf, _out_len| {
let r = unsafe { let r = unsafe {
libcrypto::PKCS5_PBKDF2_HMAC_SHA1( libcrypto::PKCS5_PBKDF2_HMAC_SHA1(
pass_buf, pass_len as c_int, pass_buf, pass_len as c_int,
@ -33,13 +33,13 @@ pub fn pbkdf2_hmac_sha1(pass: &str, salt: &[u8], iter: uint,
}; };
if r != 1 as c_int { fail!(); } if r != 1 as c_int { fail!(); }
} });
unsafe { vec::raw::set_len(&mut out, keylen); } unsafe { vec::raw::set_len(&mut out, keylen); }
out out
} })
} })
} }
#[cfg(test)] #[cfg(test)]

40
pkey.rs
View File

@ -99,9 +99,9 @@ impl PKey {
if len < 0 as c_int { return ~[]; } if len < 0 as c_int { return ~[]; }
let mut s = vec::from_elem(len as uint, 0u8); let mut s = vec::from_elem(len as uint, 0u8);
let r = do s.as_mut_buf |buf, _| { let r = s.as_mut_buf(|buf, _| {
f(self.evp, &buf) f(self.evp, &buf)
}; });
s.truncate(r as uint); s.truncate(r as uint);
s s
@ -109,13 +109,13 @@ impl PKey {
} }
fn _fromstr(&mut self, s: &[u8], f: extern "C" unsafe fn(c_int, **EVP_PKEY, **u8, c_uint) -> *EVP_PKEY) { fn _fromstr(&mut self, s: &[u8], f: extern "C" unsafe fn(c_int, **EVP_PKEY, **u8, c_uint) -> *EVP_PKEY) {
do s.as_imm_buf |ps, len| { s.as_imm_buf(|ps, len| {
let evp = ptr::null(); let evp = ptr::null();
unsafe { unsafe {
f(6 as c_int, &evp, &ps, len as c_uint); f(6 as c_int, &evp, &ps, len as c_uint);
} }
self.evp = evp; self.evp = evp;
} });
} }
pub fn gen(&mut self, keysz: uint) { pub fn gen(&mut self, keysz: uint) {
@ -228,8 +228,8 @@ impl PKey {
let mut r = vec::from_elem(len as uint + 1u, 0u8); let mut r = vec::from_elem(len as uint + 1u, 0u8);
let rv = do r.as_mut_buf |pr, _len| { let rv = r.as_mut_buf(|pr, _len| {
do s.as_imm_buf |ps, s_len| { s.as_imm_buf(|ps, s_len| {
libcrypto::RSA_public_encrypt( libcrypto::RSA_public_encrypt(
s_len as c_uint, s_len as c_uint,
ps, ps,
@ -237,8 +237,8 @@ impl PKey {
rsa, rsa,
openssl_padding_code(padding) openssl_padding_code(padding)
) )
} })
}; });
if rv < 0 as c_int { if rv < 0 as c_int {
~[] ~[]
} else { } else {
@ -257,8 +257,8 @@ impl PKey {
let mut r = vec::from_elem(len as uint + 1u, 0u8); let mut r = vec::from_elem(len as uint + 1u, 0u8);
let rv = do r.as_mut_buf |pr, _len| { let rv = r.as_mut_buf(|pr, _len| {
do s.as_imm_buf |ps, s_len| { s.as_imm_buf(|ps, s_len| {
libcrypto::RSA_private_decrypt( libcrypto::RSA_private_decrypt(
s_len as c_uint, s_len as c_uint,
ps, ps,
@ -266,8 +266,8 @@ impl PKey {
rsa, rsa,
openssl_padding_code(padding) openssl_padding_code(padding)
) )
} })
}; });
if rv < 0 as c_int { if rv < 0 as c_int {
~[] ~[]
@ -307,8 +307,8 @@ impl PKey {
let mut len = libcrypto::RSA_size(rsa); let mut len = libcrypto::RSA_size(rsa);
let mut r = vec::from_elem(len as uint + 1u, 0u8); let mut r = vec::from_elem(len as uint + 1u, 0u8);
let rv = do r.as_mut_buf |pr, _len| { let rv = r.as_mut_buf(|pr, _len| {
do s.as_imm_buf |ps, s_len| { s.as_imm_buf(|ps, s_len| {
libcrypto::RSA_sign( libcrypto::RSA_sign(
openssl_hash_nid(hash), openssl_hash_nid(hash),
ps, ps,
@ -316,8 +316,8 @@ impl PKey {
pr, pr,
&mut len, &mut len,
rsa) rsa)
} })
}; });
if rv < 0 as c_int { if rv < 0 as c_int {
~[] ~[]
@ -332,8 +332,8 @@ impl PKey {
unsafe { unsafe {
let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp); let rsa = libcrypto::EVP_PKEY_get1_RSA(self.evp);
do m.as_imm_buf |pm, m_len| { m.as_imm_buf(|pm, m_len| {
do s.as_imm_buf |ps, s_len| { s.as_imm_buf(|ps, s_len| {
let rv = libcrypto::RSA_verify( let rv = libcrypto::RSA_verify(
openssl_hash_nid(hash), openssl_hash_nid(hash),
pm, pm,
@ -344,8 +344,8 @@ impl PKey {
); );
rv == 1 as c_int rv == 1 as c_int
} })
} })
} }
} }
} }

View File

@ -13,10 +13,10 @@ mod libcrypto {
pub fn rand_bytes(len: uint) -> ~[u8] { pub fn rand_bytes(len: uint) -> ~[u8] {
let mut out = vec::with_capacity(len); let mut out = vec::with_capacity(len);
do out.as_mut_buf |out_buf, len| { out.as_mut_buf(|out_buf, len| {
let r = unsafe { libcrypto::RAND_bytes(out_buf, len as c_int) }; let r = unsafe { libcrypto::RAND_bytes(out_buf, len as c_int) };
if r != 1 as c_int { fail!() } if r != 1 as c_int { fail!() }
} });
unsafe { vec::raw::set_len(&mut out, len); } unsafe { vec::raw::set_len(&mut out, len); }

20
symm.rs
View File

@ -115,8 +115,8 @@ impl Crypter {
}; };
assert_eq!(key.len(), self.keylen); assert_eq!(key.len(), self.keylen);
do key.as_imm_buf |pkey, _len| { key.as_imm_buf(|pkey, _len| {
do iv.as_imm_buf |piv, _len| { iv.as_imm_buf(|piv, _len| {
libcrypto::EVP_CipherInit( libcrypto::EVP_CipherInit(
self.ctx, self.ctx,
self.evp, self.evp,
@ -124,8 +124,8 @@ impl Crypter {
piv, piv,
mode mode
) )
} });
} });
} }
} }
@ -135,10 +135,10 @@ impl Crypter {
*/ */
pub fn update(&self, data: &[u8]) -> ~[u8] { pub fn update(&self, data: &[u8]) -> ~[u8] {
unsafe { unsafe {
do data.as_imm_buf |pdata, len| { data.as_imm_buf(|pdata, len| {
let mut res = vec::from_elem(len + self.blocksize, 0u8); let mut res = vec::from_elem(len + self.blocksize, 0u8);
let reslen = do res.as_mut_buf |pres, _len| { let reslen = res.as_mut_buf(|pres, _len| {
let mut reslen = (len + self.blocksize) as u32; let mut reslen = (len + self.blocksize) as u32;
libcrypto::EVP_CipherUpdate( libcrypto::EVP_CipherUpdate(
@ -150,11 +150,11 @@ impl Crypter {
); );
reslen reslen
}; });
res.truncate(reslen as uint); res.truncate(reslen as uint);
res res
} })
} }
} }
@ -165,11 +165,11 @@ impl Crypter {
unsafe { unsafe {
let mut res = vec::from_elem(self.blocksize, 0u8); let mut res = vec::from_elem(self.blocksize, 0u8);
let reslen = do res.as_mut_buf |pres, _len| { let reslen = res.as_mut_buf(|pres, _len| {
let mut reslen = self.blocksize as c_int; let mut reslen = self.blocksize as c_int;
libcrypto::EVP_CipherFinal(self.ctx, pres, &mut reslen); libcrypto::EVP_CipherFinal(self.ctx, pres, &mut reslen);
reslen reslen
}; });
res.truncate(reslen as uint); res.truncate(reslen as uint);
res res