Rename crypto:#️⃣:HashType -> Type

s/HashType/Type/ to follow the current Rust style. Import Type as HashType in modules where the name might be ambiguous.
[breaking change]
This commit is contained in:
Gleb Kozyrev 2015-01-22 18:03:19 +02:00
parent eb7b7bf847
commit 71f8420205
6 changed files with 49 additions and 47 deletions

View File

@ -6,7 +6,7 @@ use ffi;
/// Message digest (hash) type. /// Message digest (hash) type.
#[derive(Copy)] #[derive(Copy)]
pub enum HashType { pub enum Type {
MD5, MD5,
SHA1, SHA1,
SHA224, SHA224,
@ -16,11 +16,11 @@ pub enum HashType {
RIPEMD160 RIPEMD160
} }
impl HashType { impl Type {
/// Returns the length of the message digest. /// Returns the length of the message digest.
#[inline] #[inline]
pub fn md_len(&self) -> usize { pub fn md_len(&self) -> usize {
use self::HashType::*; use self::Type::*;
match *self { match *self {
MD5 => 16, MD5 => 16,
SHA1 => 20, SHA1 => 20,
@ -36,7 +36,7 @@ impl HashType {
#[inline] #[inline]
pub fn evp_md(&self) -> *const ffi::EVP_MD { pub fn evp_md(&self) -> *const ffi::EVP_MD {
unsafe { unsafe {
use self::HashType::*; use self::Type::*;
match *self { match *self {
MD5 => ffi::EVP_md5(), MD5 => ffi::EVP_md5(),
SHA1 => ffi::EVP_sha1(), SHA1 => ffi::EVP_sha1(),
@ -66,10 +66,10 @@ use self::State::*;
/// Calculate a hash in one go. /// Calculate a hash in one go.
/// ///
/// ``` /// ```
/// use openssl::crypto::hash::{hash, HashType}; /// use openssl::crypto::hash::{hash, Type};
/// let data = b"\x42\xF4\x97\xE0"; /// let data = b"\x42\xF4\x97\xE0";
/// let spec = b"\x7c\x43\x0f\x17\x8a\xef\xdf\x14\x87\xfe\xe7\x14\x4e\x96\x41\xe2"; /// let spec = b"\x7c\x43\x0f\x17\x8a\xef\xdf\x14\x87\xfe\xe7\x14\x4e\x96\x41\xe2";
/// let res = hash(HashType::MD5, data); /// let res = hash(Type::MD5, data);
/// assert_eq!(res, spec); /// assert_eq!(res, spec);
/// ``` /// ```
/// ///
@ -77,10 +77,10 @@ use self::State::*;
/// ///
/// ``` /// ```
/// use std::old_io::Writer; /// use std::old_io::Writer;
/// use openssl::crypto::hash::{Hasher, HashType}; /// use openssl::crypto::hash::{Hasher, Type};
/// let data = [b"\x42\xF4", b"\x97\xE0"]; /// let data = [b"\x42\xF4", b"\x97\xE0"];
/// let spec = b"\x7c\x43\x0f\x17\x8a\xef\xdf\x14\x87\xfe\xe7\x14\x4e\x96\x41\xe2"; /// let spec = b"\x7c\x43\x0f\x17\x8a\xef\xdf\x14\x87\xfe\xe7\x14\x4e\x96\x41\xe2";
/// let mut h = Hasher::new(HashType::MD5); /// let mut h = Hasher::new(Type::MD5);
/// h.write_all(data[0]); /// h.write_all(data[0]);
/// h.write_all(data[1]); /// h.write_all(data[1]);
/// let res = h.finish(); /// let res = h.finish();
@ -95,13 +95,13 @@ use self::State::*;
pub struct Hasher { pub struct Hasher {
ctx: *mut ffi::EVP_MD_CTX, ctx: *mut ffi::EVP_MD_CTX,
md: *const ffi::EVP_MD, md: *const ffi::EVP_MD,
type_: HashType, type_: Type,
state: State, state: State,
} }
impl Hasher { impl Hasher {
/// Creates a new `Hasher` with the specified hash type. /// Creates a new `Hasher` with the specified hash type.
pub fn new(ty: HashType) -> Hasher { pub fn new(ty: Type) -> Hasher {
ffi::init(); ffi::init();
let ctx = unsafe { let ctx = unsafe {
@ -203,7 +203,7 @@ impl Drop for Hasher {
} }
/// Computes the hash of the `data` with the hash `t`. /// Computes the hash of the `data` with the hash `t`.
pub fn hash(t: HashType, data: &[u8]) -> Vec<u8> { pub fn hash(t: Type, data: &[u8]) -> Vec<u8> {
let mut h = Hasher::new(t); let mut h = Hasher::new(t);
let _ = h.write_all(data); let _ = h.write_all(data);
h.finish() h.finish()
@ -212,10 +212,10 @@ pub fn hash(t: HashType, data: &[u8]) -> Vec<u8> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use serialize::hex::{FromHex, ToHex}; use serialize::hex::{FromHex, ToHex};
use super::{hash, Hasher, HashType}; use super::{hash, Hasher, Type};
use std::old_io::Writer; use std::old_io::Writer;
fn hash_test(hashtype: HashType, hashtest: &(&str, &str)) { fn hash_test(hashtype: Type, hashtest: &(&str, &str)) {
let res = hash(hashtype, &*hashtest.0.from_hex().unwrap()); let res = hash(hashtype, &*hashtest.0.from_hex().unwrap());
assert_eq!(res.to_hex(), hashtest.1); assert_eq!(res.to_hex(), hashtest.1);
} }
@ -247,13 +247,13 @@ mod tests {
#[test] #[test]
fn test_md5() { fn test_md5() {
for test in md5_tests.iter() { for test in md5_tests.iter() {
hash_test(HashType::MD5, test); hash_test(Type::MD5, test);
} }
} }
#[test] #[test]
fn test_md5_recycle() { fn test_md5_recycle() {
let mut h = Hasher::new(HashType::MD5); let mut h = Hasher::new(Type::MD5);
for test in md5_tests.iter() { for test in md5_tests.iter() {
hash_recycle_test(&mut h, test); hash_recycle_test(&mut h, test);
} }
@ -261,11 +261,11 @@ mod tests {
#[test] #[test]
fn test_finish_twice() { fn test_finish_twice() {
let mut h = Hasher::new(HashType::MD5); let mut h = Hasher::new(Type::MD5);
let _ = h.write_all(&*md5_tests[6].0.from_hex().unwrap()); let _ = h.write_all(&*md5_tests[6].0.from_hex().unwrap());
let _ = h.finish(); let _ = h.finish();
let res = h.finish(); let res = h.finish();
let null = hash(HashType::MD5, &[]); let null = hash(Type::MD5, &[]);
assert_eq!(res, null); assert_eq!(res, null);
} }
@ -275,7 +275,7 @@ mod tests {
let inp = md5_tests[i].0.from_hex().unwrap(); let inp = md5_tests[i].0.from_hex().unwrap();
assert!(inp.len() > 2); assert!(inp.len() > 2);
let p = inp.len() / 2; let p = inp.len() / 2;
let h0 = Hasher::new(HashType::MD5); let h0 = Hasher::new(Type::MD5);
println!("Clone a new hasher"); println!("Clone a new hasher");
let mut h1 = h0.clone(); let mut h1 = h0.clone();
@ -305,7 +305,7 @@ mod tests {
]; ];
for test in tests.iter() { for test in tests.iter() {
hash_test(HashType::SHA1, test); hash_test(Type::SHA1, test);
} }
} }
@ -316,7 +316,7 @@ mod tests {
]; ];
for test in tests.iter() { for test in tests.iter() {
hash_test(HashType::SHA256, test); hash_test(Type::SHA256, test);
} }
} }
@ -327,7 +327,7 @@ mod tests {
]; ];
for test in tests.iter() { for test in tests.iter() {
hash_test(HashType::RIPEMD160, test); hash_test(Type::RIPEMD160, test);
} }
} }
} }

View File

@ -18,7 +18,7 @@ use libc::{c_int, c_uint};
use std::iter::repeat; use std::iter::repeat;
use std::old_io::{IoError, Writer}; use std::old_io::{IoError, Writer};
use crypto::hash::HashType; use crypto::hash::Type;
use ffi; use ffi;
#[derive(PartialEq, Copy)] #[derive(PartialEq, Copy)]
@ -37,25 +37,25 @@ use self::State::*;
/// Calculate a HMAC in one go. /// Calculate a HMAC in one go.
/// ///
/// ``` /// ```
/// use openssl::crypto::hash::HashType; /// use openssl::crypto::hash::Type;
/// use openssl::crypto::hmac::hmac; /// use openssl::crypto::hmac::hmac;
/// let key = b"Jefe"; /// let key = b"Jefe";
/// let data = b"what do ya want for nothing?"; /// let data = b"what do ya want for nothing?";
/// let spec = b"\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38"; /// let spec = b"\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38";
/// let res = hmac(HashType::MD5, key, data); /// let res = hmac(Type::MD5, key, data);
/// assert_eq!(spec, res); /// assert_eq!(spec, res);
/// ``` /// ```
/// ///
/// Use the `Writer` trait to supply the input in chunks. /// Use the `Writer` trait to supply the input in chunks.
/// ///
/// ``` /// ```
/// use openssl::crypto::hash::HashType;
/// use std::old_io::Writer; /// use std::old_io::Writer;
/// use openssl::crypto::hash::Type;
/// use openssl::crypto::hmac::HMAC; /// use openssl::crypto::hmac::HMAC;
/// let key = b"Jefe"; /// let key = b"Jefe";
/// let data = [b"what do ya ", b"want for nothing?"]; /// let data = [b"what do ya ", b"want for nothing?"];
/// let spec = b"\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38"; /// let spec = b"\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38";
/// let mut h = HMAC::new(HashType::MD5, &*key); /// let mut h = HMAC::new(Type::MD5, &*key);
/// h.write_all(data[0]); /// h.write_all(data[0]);
/// h.write_all(data[1]); /// h.write_all(data[1]);
/// let res = h.finish(); /// let res = h.finish();
@ -63,13 +63,13 @@ use self::State::*;
/// ``` /// ```
pub struct HMAC { pub struct HMAC {
ctx: ffi::HMAC_CTX, ctx: ffi::HMAC_CTX,
type_: HashType, type_: Type,
state: State, state: State,
} }
impl HMAC { impl HMAC {
/// Creates a new `HMAC` with the specified hash type using the `key`. /// Creates a new `HMAC` with the specified hash type using the `key`.
pub fn new(ty: HashType, key: &[u8]) -> HMAC { pub fn new(ty: Type, key: &[u8]) -> HMAC {
ffi::init(); ffi::init();
let ctx = unsafe { let ctx = unsafe {
@ -185,7 +185,7 @@ impl Drop for HMAC {
} }
/// Computes the HMAC of the `data` with the hash `t` and `key`. /// Computes the HMAC of the `data` with the hash `t` and `key`.
pub fn hmac(t: HashType, key: &[u8], data: &[u8]) -> Vec<u8> { pub fn hmac(t: Type, key: &[u8], data: &[u8]) -> Vec<u8> {
let mut h = HMAC::new(t, key); let mut h = HMAC::new(t, key);
let _ = h.write_all(data); let _ = h.write_all(data);
h.finish() h.finish()
@ -195,12 +195,12 @@ pub fn hmac(t: HashType, key: &[u8], data: &[u8]) -> Vec<u8> {
mod tests { mod tests {
use std::iter::repeat; use std::iter::repeat;
use serialize::hex::FromHex; use serialize::hex::FromHex;
use crypto::hash::HashType; use crypto::hash::Type;
use crypto::hash::HashType::*; use crypto::hash::Type::*;
use super::{hmac, HMAC}; use super::{hmac, HMAC};
use std::old_io::Writer; use std::old_io::Writer;
fn test_hmac(ty: HashType, tests: &[(Vec<u8>, Vec<u8>, Vec<u8>)]) { fn test_hmac(ty: Type, tests: &[(Vec<u8>, Vec<u8>, Vec<u8>)]) {
for &(ref key, ref data, ref res) in tests.iter() { for &(ref key, ref data, ref res) in tests.iter() {
assert_eq!(hmac(ty, &**key, &**data), *res); assert_eq!(hmac(ty, &**key, &**data), *res);
} }
@ -267,11 +267,11 @@ mod tests {
b"Test Using Larger Than Block-Size Key - Hash Key First".to_vec(), b"Test Using Larger Than Block-Size Key - Hash Key First".to_vec(),
"6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd".from_hex().unwrap()); "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd".from_hex().unwrap());
let mut h = HMAC::new(HashType::MD5, &*test.0); let mut h = HMAC::new(Type::MD5, &*test.0);
let _ = h.write_all(&*test.1); let _ = h.write_all(&*test.1);
let _ = h.finish(); let _ = h.finish();
let res = h.finish(); let res = h.finish();
let null = hmac(HashType::MD5, &*test.0, &[]); let null = hmac(Type::MD5, &*test.0, &[]);
assert_eq!(res, null); assert_eq!(res, null);
} }
@ -287,7 +287,7 @@ mod tests {
"6f630fad67cda0ee1fb1f562db3aa53e".from_hex().unwrap()), "6f630fad67cda0ee1fb1f562db3aa53e".from_hex().unwrap()),
]; ];
let p = tests[0].0.len() / 2; let p = tests[0].0.len() / 2;
let h0 = HMAC::new(HashType::MD5, &*tests[0].0); let h0 = HMAC::new(Type::MD5, &*tests[0].0);
println!("Clone a new hmac"); println!("Clone a new hmac");
let mut h1 = h0.clone(); let mut h1 = h0.clone();
@ -360,7 +360,7 @@ mod tests {
fn test_sha2(ty: HashType, results: &[Vec<u8>]) { fn test_sha2(ty: Type, results: &[Vec<u8>]) {
// test vectors from RFC 4231 // test vectors from RFC 4231
let tests: [(Vec<u8>, Vec<u8>); 6] = [ let tests: [(Vec<u8>, Vec<u8>); 6] = [
(repeat(0xb_u8).take(20).collect(), b"Hi There".to_vec()), (repeat(0xb_u8).take(20).collect(), b"Hi There".to_vec()),

View File

@ -3,7 +3,8 @@ use std::iter::repeat;
use std::mem; use std::mem;
use std::ptr; use std::ptr;
use bio::{MemBio}; use bio::{MemBio};
use crypto::hash::HashType; use crypto::hash;
use crypto::hash::Type as HashType;
use ffi; use ffi;
use ssl::error::{SslError, StreamError}; use ssl::error::{SslError, StreamError};
@ -276,7 +277,7 @@ impl PKey {
*/ */
pub fn verify(&self, m: &[u8], s: &[u8]) -> bool { self.verify_with_hash(m, s, HashType::SHA256) } pub fn verify(&self, m: &[u8], s: &[u8]) -> bool { self.verify_with_hash(m, s, HashType::SHA256) }
pub fn sign_with_hash(&self, s: &[u8], hash: HashType) -> Vec<u8> { pub fn sign_with_hash(&self, s: &[u8], hash: hash::Type) -> Vec<u8> {
unsafe { unsafe {
let rsa = ffi::EVP_PKEY_get1_RSA(self.evp); let rsa = ffi::EVP_PKEY_get1_RSA(self.evp);
let len = ffi::RSA_size(rsa); let len = ffi::RSA_size(rsa);
@ -300,7 +301,7 @@ impl PKey {
} }
} }
pub fn verify_with_hash(&self, m: &[u8], s: &[u8], hash: HashType) -> bool { pub fn verify_with_hash(&self, m: &[u8], s: &[u8], hash: hash::Type) -> bool {
unsafe { unsafe {
let rsa = ffi::EVP_PKEY_get1_RSA(self.evp); let rsa = ffi::EVP_PKEY_get1_RSA(self.evp);
@ -332,7 +333,7 @@ impl Drop for PKey {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crypto::hash::HashType::{MD5, SHA1}; use crypto::hash::Type::{MD5, SHA1};
#[test] #[test]
fn test_gen_pub() { fn test_gen_pub() {

View File

@ -3,7 +3,7 @@ use std::old_io::net::tcp::TcpStream;
use std::old_io::{Writer}; use std::old_io::{Writer};
use std::thread::Thread; use std::thread::Thread;
use crypto::hash::HashType::{SHA256}; use crypto::hash::Type::{SHA256};
use ssl::SslMethod::Sslv23; use ssl::SslMethod::Sslv23;
use ssl::{SslContext, SslStream, VerifyCallback}; use ssl::{SslContext, SslStream, VerifyCallback};
use ssl::SslVerifyMode::SslVerifyPeer; use ssl::SslVerifyMode::SslVerifyPeer;

View File

@ -8,7 +8,8 @@ use std::ptr;
use asn1::{Asn1Time}; use asn1::{Asn1Time};
use bio::{MemBio}; use bio::{MemBio};
use crypto::hash::{HashType}; use crypto::hash;
use crypto::hash::Type as HashType;
use crypto::pkey::{PKey}; use crypto::pkey::{PKey};
use crypto::rand::rand_bytes; use crypto::rand::rand_bytes;
use ffi; use ffi;
@ -152,14 +153,14 @@ impl<'a, T: AsStr<'a>> ToStr for Vec<T> {
/// use std::old_io::{File, Open, Write}; /// use std::old_io::{File, Open, Write};
/// # use std::old_io::fs; /// # use std::old_io::fs;
/// ///
/// use openssl::crypto::hash::HashType; /// use openssl::crypto::hash::Type;
/// use openssl::x509::{KeyUsage, X509Generator}; /// use openssl::x509::{KeyUsage, X509Generator};
/// ///
/// let gen = X509Generator::new() /// let gen = X509Generator::new()
/// .set_bitlength(2048) /// .set_bitlength(2048)
/// .set_valid_period(365*2) /// .set_valid_period(365*2)
/// .set_CN("SuperMegaCorp Inc.") /// .set_CN("SuperMegaCorp Inc.")
/// .set_sign_hash(HashType::SHA256) /// .set_sign_hash(Type::SHA256)
/// .set_usage(&[KeyUsage::DigitalSignature]); /// .set_usage(&[KeyUsage::DigitalSignature]);
/// ///
/// let (cert, pkey) = gen.generate().unwrap(); /// let (cert, pkey) = gen.generate().unwrap();
@ -236,7 +237,7 @@ impl X509Generator {
self self
} }
pub fn set_sign_hash(mut self, hash_type: HashType) -> X509Generator { pub fn set_sign_hash(mut self, hash_type: hash::Type) -> X509Generator {
self.hash_type = hash_type; self.hash_type = hash_type;
self self
} }
@ -387,7 +388,7 @@ impl<'ctx> X509<'ctx> {
} }
/// Returns certificate fingerprint calculated using provided hash /// Returns certificate fingerprint calculated using provided hash
pub fn fingerprint(&self, hash_type: HashType) -> Option<Vec<u8>> { pub fn fingerprint(&self, hash_type: hash::Type) -> Option<Vec<u8>> {
let evp = hash_type.evp_md(); let evp = hash_type.evp_md();
let len = hash_type.md_len(); let len = hash_type.md_len();
let v: Vec<u8> = repeat(0).take(len as usize).collect(); let v: Vec<u8> = repeat(0).take(len as usize).collect();

View File

@ -2,7 +2,7 @@ use serialize::hex::FromHex;
use std::old_io::{File, Open, Read}; use std::old_io::{File, Open, Read};
use std::old_io::util::NullWriter; use std::old_io::util::NullWriter;
use crypto::hash::HashType::{SHA256}; use crypto::hash::Type::{SHA256};
use x509::{X509, X509Generator}; use x509::{X509, X509Generator};
use x509::KeyUsage::{DigitalSignature, KeyEncipherment}; use x509::KeyUsage::{DigitalSignature, KeyEncipherment};
use x509::ExtKeyUsage::{ClientAuth, ServerAuth}; use x509::ExtKeyUsage::{ClientAuth, ServerAuth};