Commit 4e79896c authored by Kevin Ballard's avatar Kevin Ballard
Browse files

Remove unnecessary `unsafe` blocks

parent f824a241
Loading
Loading
Loading
Loading
+3 −5
Original line number Diff line number Diff line
@@ -98,12 +98,10 @@ pub impl Hasher {
 * value
 */
pub fn hash(t: HashType, data: &[u8]) -> ~[u8] {
    unsafe {
    let h = Hasher(t);
    h.update(data);
    h.final()
}
}

#[cfg(test)]
mod tests {
+4 −8
Original line number Diff line number Diff line
@@ -34,11 +34,9 @@ impl<'self> ToHex for &'self [u8] {
            let xhi = (x >> 4) & 0x0F;
            let xlo = (x     ) & 0x0F;

            unsafe {
            str::push_char(&mut s, chars[xhi]);
            str::push_char(&mut s, chars[xlo]);
        }
        }

        s
    }
@@ -61,10 +59,8 @@ impl<'self> FromHex for &'self str {
                else { fail!(~"bad hex character"); };

            if i % 2 == 0 {
                unsafe {
                vec::push(&mut vec, nibble << 4);
            }
            }
            else {
                vec[i/2] |= nibble;
            }
+16 −20
Original line number Diff line number Diff line
@@ -100,7 +100,6 @@ pub fn PKey() -> PKey {
///Represents a public key, optionally with a private key attached.
priv impl PKey {
    priv fn _tostr(&self, f: @fn(*EVP_PKEY, &*mut u8) -> c_int) -> ~[u8] {
        unsafe {
        let buf = ptr::mut_null();
        let len = f(self.evp, &buf);
        if len < 0 as c_int { return ~[]; }
@@ -112,14 +111,12 @@ priv impl PKey {

        vec::slice(s, 0u, r as uint).to_owned()
    }
    }

    priv fn _fromstr(
        &mut self,
        s: &[u8],
        f: @fn(c_int, &*EVP_PKEY, &*u8, c_uint) -> *EVP_PKEY
    ) {
        unsafe {
        do vec::as_imm_buf(s) |ps, len| {
            let evp = ptr::null();
            f(6 as c_int, &evp, &ps, len as c_uint);
@@ -127,7 +124,6 @@ priv impl PKey {
        }
    }
}
}

pub impl PKey {
    fn gen(&mut self, keysz: uint) {
@@ -298,24 +294,24 @@ pub impl PKey {
     * Encrypts data using OAEP padding, returning the encrypted data. The
     * supplied data must not be larger than max_data().
     */
    fn encrypt(&self, s: &[u8]) -> ~[u8] { unsafe { self.encrypt_with_padding(s, OAEP) } }
    fn encrypt(&self, s: &[u8]) -> ~[u8] { self.encrypt_with_padding(s, OAEP) }

    /**
     * Decrypts data, expecting OAEP padding, returning the decrypted data.
     */
    fn decrypt(&self, s: &[u8]) -> ~[u8] { unsafe { self.decrypt_with_padding(s, OAEP) } }
    fn decrypt(&self, s: &[u8]) -> ~[u8] { self.decrypt_with_padding(s, OAEP) }

    /**
     * Signs data, using OpenSSL's default scheme and sha256. Unlike encrypt(),
     * can process an arbitrary amount of data; returns the signature.
     */
    fn sign(&self, s: &[u8]) -> ~[u8] { unsafe { self.sign_with_hash(s, SHA256) } }
    fn sign(&self, s: &[u8]) -> ~[u8] { self.sign_with_hash(s, SHA256) }

    /**
     * Verifies a signature s (using OpenSSL's default scheme and sha256) on a
     * message m. Returns true if the signature is valid, and false otherwise.
     */
    fn verify(&self, m: &[u8], s: &[u8]) -> bool { unsafe { self.verify_with_hash(m, s, SHA256) } }
    fn verify(&self, m: &[u8], s: &[u8]) -> bool { self.verify_with_hash(m, s, SHA256) }

    fn sign_with_hash(&self, s: &[u8], hash: HashType) -> ~[u8] {
        unsafe {