Commit 08cdf5fd authored by Kevin Ballard's avatar Kevin Ballard
Browse files

Update for current incoming

parent 4e79896c
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -20,8 +20,6 @@
       uuid = "38297409-b4c2-4499-8131-a99a7e44dad3")];
#[crate_type = "lib"];

extern mod std; // FIXME https://github.com/mozilla/rust/issues/1127

pub mod hex;
pub mod hash;
pub mod hmac;
+13 −13
Original line number Diff line number Diff line
use core::libc::c_uint;
use std::libc::c_uint;

pub enum HashType {
    MD5,
@@ -15,9 +15,9 @@ pub type EVP_MD_CTX = *libc::c_void;
#[allow(non_camel_case_types)]
pub type EVP_MD = *libc::c_void;

#[link_name = "crypto"]
#[abi = "cdecl"]
extern mod libcrypto {
#[link_args = "-lcrypto"]
extern {
    fn EVP_MD_CTX_create() -> EVP_MD_CTX;

    fn EVP_md5() -> EVP_MD;
@@ -35,12 +35,12 @@ extern mod libcrypto {
pub fn evpmd(t: HashType) -> (EVP_MD, uint) {
    unsafe {
        match t {
            MD5 => (libcrypto::EVP_md5(), 16u),
            SHA1 => (libcrypto::EVP_sha1(), 20u),
            SHA224 => (libcrypto::EVP_sha224(), 28u),
            SHA256 => (libcrypto::EVP_sha256(), 32u),
            SHA384 => (libcrypto::EVP_sha384(), 48u),
            SHA512 => (libcrypto::EVP_sha512(), 64u),
            MD5 => (EVP_md5(), 16u),
            SHA1 => (EVP_sha1(), 20u),
            SHA224 => (EVP_sha224(), 28u),
            SHA256 => (EVP_sha256(), 32u),
            SHA384 => (EVP_sha384(), 48u),
            SHA512 => (EVP_sha512(), 64u),
        }
    }
}
@@ -53,7 +53,7 @@ pub struct Hasher {

pub fn Hasher(ht: HashType) -> Hasher {
    unsafe {
        let ctx = libcrypto::EVP_MD_CTX_create();
        let ctx = EVP_MD_CTX_create();
        let (evp, mdlen) = evpmd(ht);
        let h = Hasher { evp: evp, ctx: ctx, len: mdlen };
        h.init();
@@ -65,7 +65,7 @@ pub impl Hasher {
    /// Initializes this hasher
    fn init(&self) {
        unsafe {
            libcrypto::EVP_DigestInit(self.ctx, self.evp);
            EVP_DigestInit(self.ctx, self.evp);
        }
    }

@@ -73,7 +73,7 @@ pub impl Hasher {
    fn update(&self, data: &[u8]) {
        unsafe {
            do vec::as_imm_buf(data) |pdata, len| {
                libcrypto::EVP_DigestUpdate(self.ctx, pdata, len as c_uint)
                EVP_DigestUpdate(self.ctx, pdata, len as c_uint)
            }
        }
    }
@@ -86,7 +86,7 @@ pub impl Hasher {
        unsafe {
            let mut res = vec::from_elem(self.len, 0u8);
            do vec::as_mut_buf(res) |pres, _len| {
                libcrypto::EVP_DigestFinal(self.ctx, pres, ptr::null());
                EVP_DigestFinal(self.ctx, pres, ptr::null());
            }
            res
        }
+1 −1
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ pub trait ToHex {
impl<'self> ToHex for &'self [u8] {
    fn to_hex(&self) -> ~str {

        let chars = str::to_chars(~"0123456789ABCDEF");
        let chars = str::to_chars("0123456789ABCDEF");

        let mut s = ~"";

+6 −7
Original line number Diff line number Diff line
@@ -26,10 +26,9 @@ pub struct HMAC_CTX {
    key: [libc::c_uchar, ..128]
}

#[link_name = "crypto"]
#[link_args = "-lcrypto"]
#[abi = "cdecl"]
extern mod libcrypto {

extern {
    fn HMAC_CTX_init(ctx: *mut HMAC_CTX, key: *u8, keylen: libc::c_int, md: EVP_MD);

    fn HMAC_Update(ctx: *mut HMAC_CTX, input: *u8, len: libc::c_uint);
@@ -56,7 +55,7 @@ pub fn HMAC(ht: HashType, key: ~[u8]) -> HMAC {
            mut key: [0u8, .. 128]
        };

        libcrypto::HMAC_CTX_init(&mut ctx,
        HMAC_CTX_init(&mut ctx,
                                 vec::raw::to_ptr(key),
                                 key.len() as libc::c_int,
                                 evp);
@@ -69,7 +68,7 @@ pub impl HMAC {
    fn update(&mut self, data: &[u8]) {
        unsafe {
            do vec::as_imm_buf(data) |pdata, len| {
                libcrypto::HMAC_Update(&mut self.ctx, pdata, len as libc::c_uint)
                HMAC_Update(&mut self.ctx, pdata, len as libc::c_uint)
            }
        }
    }
@@ -79,7 +78,7 @@ pub impl HMAC {
            let mut res = vec::from_elem(self.len, 0u8);
            let mut outlen: libc::c_uint = 0;
            do vec::as_mut_buf(res) |pres, _len| {
                libcrypto::HMAC_Final(&mut self.ctx, pres, &mut outlen);
                HMAC_Final(&mut self.ctx, pres, &mut outlen);
                assert!(self.len == outlen as uint)
            }
            res
@@ -90,7 +89,7 @@ pub impl HMAC {
fn main() {
    let mut h = HMAC(SHA512, ~[00u8]);

    h.update(~[00u8]);
    h.update([00u8]);

    io::println(fmt!("%?", h.final()))
}
+7 −7
Original line number Diff line number Diff line
use core::libc::c_int;
use std::libc::c_int;

#[link_name = "crypto"]
#[link_args = "-lcrypto"]
#[abi = "cdecl"]
extern mod libcrypto {
extern {
    fn PKCS5_PBKDF2_HMAC_SHA1(pass: *u8, passlen: c_int,
                            salt: *u8, saltlen: c_int,
                            iter: c_int, keylen: c_int,
@@ -23,7 +23,7 @@ pub fn pbkdf2_hmac_sha1(pass: &str, salt: &[u8], iter: uint,

            do vec::as_mut_buf(out) |out_buf, _out_len| {
                unsafe {
                    let r = libcrypto::PKCS5_PBKDF2_HMAC_SHA1(
                    let r = PKCS5_PBKDF2_HMAC_SHA1(
                        pass_buf, pass_len as c_int,
                        salt_buf, salt_len as c_int,
                        iter as c_int, keylen as c_int,
Loading