aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGleb Kozyrev <[email protected]>2015-01-22 18:03:19 +0200
committerGleb Kozyrev <[email protected]>2015-01-28 21:51:12 +0200
commit71f84202053e81581dc619ae9df1c37cfc0482e6 (patch)
treefaf581a7f8739ae16af648c1caf386e0085672c6 /src
parentChange Hasher and HMAC APIs closer to std::hash model (diff)
downloadrust-openssl-71f84202053e81581dc619ae9df1c37cfc0482e6.tar.xz
rust-openssl-71f84202053e81581dc619ae9df1c37cfc0482e6.zip
Rename crypto::hash::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]
Diffstat (limited to 'src')
-rw-r--r--src/crypto/hash.rs42
-rw-r--r--src/crypto/hmac.rs30
-rw-r--r--src/crypto/pkey.rs9
-rw-r--r--src/ssl/tests.rs2
-rw-r--r--src/x509/mod.rs11
-rw-r--r--src/x509/tests.rs2
6 files changed, 49 insertions, 47 deletions
diff --git a/src/crypto/hash.rs b/src/crypto/hash.rs
index 1c2dc149..a9a3d8b9 100644
--- a/src/crypto/hash.rs
+++ b/src/crypto/hash.rs
@@ -6,7 +6,7 @@ use ffi;
/// Message digest (hash) type.
#[derive(Copy)]
-pub enum HashType {
+pub enum Type {
MD5,
SHA1,
SHA224,
@@ -16,11 +16,11 @@ pub enum HashType {
RIPEMD160
}
-impl HashType {
+impl Type {
/// Returns the length of the message digest.
#[inline]
pub fn md_len(&self) -> usize {
- use self::HashType::*;
+ use self::Type::*;
match *self {
MD5 => 16,
SHA1 => 20,
@@ -36,7 +36,7 @@ impl HashType {
#[inline]
pub fn evp_md(&self) -> *const ffi::EVP_MD {
unsafe {
- use self::HashType::*;
+ use self::Type::*;
match *self {
MD5 => ffi::EVP_md5(),
SHA1 => ffi::EVP_sha1(),
@@ -66,10 +66,10 @@ use self::State::*;
/// 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 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);
/// ```
///
@@ -77,10 +77,10 @@ use self::State::*;
///
/// ```
/// 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 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[1]);
/// let res = h.finish();
@@ -95,13 +95,13 @@ use self::State::*;
pub struct Hasher {
ctx: *mut ffi::EVP_MD_CTX,
md: *const ffi::EVP_MD,
- type_: HashType,
+ type_: Type,
state: State,
}
impl Hasher {
/// Creates a new `Hasher` with the specified hash type.
- pub fn new(ty: HashType) -> Hasher {
+ pub fn new(ty: Type) -> Hasher {
ffi::init();
let ctx = unsafe {
@@ -203,7 +203,7 @@ impl Drop for Hasher {
}
/// 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 _ = h.write_all(data);
h.finish()
@@ -212,10 +212,10 @@ pub fn hash(t: HashType, data: &[u8]) -> Vec<u8> {
#[cfg(test)]
mod tests {
use serialize::hex::{FromHex, ToHex};
- use super::{hash, Hasher, HashType};
+ use super::{hash, Hasher, Type};
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());
assert_eq!(res.to_hex(), hashtest.1);
}
@@ -247,13 +247,13 @@ mod tests {
#[test]
fn test_md5() {
for test in md5_tests.iter() {
- hash_test(HashType::MD5, test);
+ hash_test(Type::MD5, test);
}
}
#[test]
fn test_md5_recycle() {
- let mut h = Hasher::new(HashType::MD5);
+ let mut h = Hasher::new(Type::MD5);
for test in md5_tests.iter() {
hash_recycle_test(&mut h, test);
}
@@ -261,11 +261,11 @@ mod tests {
#[test]
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.finish();
let res = h.finish();
- let null = hash(HashType::MD5, &[]);
+ let null = hash(Type::MD5, &[]);
assert_eq!(res, null);
}
@@ -275,7 +275,7 @@ mod tests {
let inp = md5_tests[i].0.from_hex().unwrap();
assert!(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");
let mut h1 = h0.clone();
@@ -305,7 +305,7 @@ mod tests {
];
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() {
- hash_test(HashType::SHA256, test);
+ hash_test(Type::SHA256, test);
}
}
@@ -327,7 +327,7 @@ mod tests {
];
for test in tests.iter() {
- hash_test(HashType::RIPEMD160, test);
+ hash_test(Type::RIPEMD160, test);
}
}
}
diff --git a/src/crypto/hmac.rs b/src/crypto/hmac.rs
index 948f8b61..1b9653c1 100644
--- a/src/crypto/hmac.rs
+++ b/src/crypto/hmac.rs
@@ -18,7 +18,7 @@ use libc::{c_int, c_uint};
use std::iter::repeat;
use std::old_io::{IoError, Writer};
-use crypto::hash::HashType;
+use crypto::hash::Type;
use ffi;
#[derive(PartialEq, Copy)]
@@ -37,25 +37,25 @@ use self::State::*;
/// Calculate a HMAC in one go.
///
/// ```
-/// use openssl::crypto::hash::HashType;
+/// use openssl::crypto::hash::Type;
/// use openssl::crypto::hmac::hmac;
/// let key = b"Jefe";
/// 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 res = hmac(HashType::MD5, key, data);
+/// let res = hmac(Type::MD5, key, data);
/// assert_eq!(spec, res);
/// ```
///
/// Use the `Writer` trait to supply the input in chunks.
///
/// ```
-/// use openssl::crypto::hash::HashType;
/// use std::old_io::Writer;
+/// use openssl::crypto::hash::Type;
/// use openssl::crypto::hmac::HMAC;
/// let key = b"Jefe";
/// 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 mut h = HMAC::new(HashType::MD5, &*key);
+/// let mut h = HMAC::new(Type::MD5, &*key);
/// h.write_all(data[0]);
/// h.write_all(data[1]);
/// let res = h.finish();
@@ -63,13 +63,13 @@ use self::State::*;
/// ```
pub struct HMAC {
ctx: ffi::HMAC_CTX,
- type_: HashType,
+ type_: Type,
state: State,
}
impl HMAC {
/// 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();
let ctx = unsafe {
@@ -185,7 +185,7 @@ impl Drop for HMAC {
}
/// 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 _ = h.write_all(data);
h.finish()
@@ -195,12 +195,12 @@ pub fn hmac(t: HashType, key: &[u8], data: &[u8]) -> Vec<u8> {
mod tests {
use std::iter::repeat;
use serialize::hex::FromHex;
- use crypto::hash::HashType;
- use crypto::hash::HashType::*;
+ use crypto::hash::Type;
+ use crypto::hash::Type::*;
use super::{hmac, HMAC};
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() {
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(),
"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.finish();
let res = h.finish();
- let null = hmac(HashType::MD5, &*test.0, &[]);
+ let null = hmac(Type::MD5, &*test.0, &[]);
assert_eq!(res, null);
}
@@ -287,7 +287,7 @@ mod tests {
"6f630fad67cda0ee1fb1f562db3aa53e".from_hex().unwrap()),
];
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");
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
let tests: [(Vec<u8>, Vec<u8>); 6] = [
(repeat(0xb_u8).take(20).collect(), b"Hi There".to_vec()),
diff --git a/src/crypto/pkey.rs b/src/crypto/pkey.rs
index 1aa5cc0e..c20fae4f 100644
--- a/src/crypto/pkey.rs
+++ b/src/crypto/pkey.rs
@@ -3,7 +3,8 @@ use std::iter::repeat;
use std::mem;
use std::ptr;
use bio::{MemBio};
-use crypto::hash::HashType;
+use crypto::hash;
+use crypto::hash::Type as HashType;
use ffi;
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 sign_with_hash(&self, s: &[u8], hash: HashType) -> Vec<u8> {
+ pub fn sign_with_hash(&self, s: &[u8], hash: hash::Type) -> Vec<u8> {
unsafe {
let rsa = ffi::EVP_PKEY_get1_RSA(self.evp);
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 {
let rsa = ffi::EVP_PKEY_get1_RSA(self.evp);
@@ -332,7 +333,7 @@ impl Drop for PKey {
#[cfg(test)]
mod tests {
- use crypto::hash::HashType::{MD5, SHA1};
+ use crypto::hash::Type::{MD5, SHA1};
#[test]
fn test_gen_pub() {
diff --git a/src/ssl/tests.rs b/src/ssl/tests.rs
index 77a3118d..73f479bf 100644
--- a/src/ssl/tests.rs
+++ b/src/ssl/tests.rs
@@ -3,7 +3,7 @@ use std::old_io::net::tcp::TcpStream;
use std::old_io::{Writer};
use std::thread::Thread;
-use crypto::hash::HashType::{SHA256};
+use crypto::hash::Type::{SHA256};
use ssl::SslMethod::Sslv23;
use ssl::{SslContext, SslStream, VerifyCallback};
use ssl::SslVerifyMode::SslVerifyPeer;
diff --git a/src/x509/mod.rs b/src/x509/mod.rs
index 17b79deb..6c09f8b2 100644
--- a/src/x509/mod.rs
+++ b/src/x509/mod.rs
@@ -8,7 +8,8 @@ use std::ptr;
use asn1::{Asn1Time};
use bio::{MemBio};
-use crypto::hash::{HashType};
+use crypto::hash;
+use crypto::hash::Type as HashType;
use crypto::pkey::{PKey};
use crypto::rand::rand_bytes;
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::fs;
///
-/// use openssl::crypto::hash::HashType;
+/// use openssl::crypto::hash::Type;
/// use openssl::x509::{KeyUsage, X509Generator};
///
/// let gen = X509Generator::new()
/// .set_bitlength(2048)
/// .set_valid_period(365*2)
/// .set_CN("SuperMegaCorp Inc.")
-/// .set_sign_hash(HashType::SHA256)
+/// .set_sign_hash(Type::SHA256)
/// .set_usage(&[KeyUsage::DigitalSignature]);
///
/// let (cert, pkey) = gen.generate().unwrap();
@@ -236,7 +237,7 @@ impl X509Generator {
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
}
@@ -387,7 +388,7 @@ impl<'ctx> X509<'ctx> {
}
/// 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 len = hash_type.md_len();
let v: Vec<u8> = repeat(0).take(len as usize).collect();
diff --git a/src/x509/tests.rs b/src/x509/tests.rs
index c9a655f2..4f24e70c 100644
--- a/src/x509/tests.rs
+++ b/src/x509/tests.rs
@@ -2,7 +2,7 @@ use serialize::hex::FromHex;
use std::old_io::{File, Open, Read};
use std::old_io::util::NullWriter;
-use crypto::hash::HashType::{SHA256};
+use crypto::hash::Type::{SHA256};
use x509::{X509, X509Generator};
use x509::KeyUsage::{DigitalSignature, KeyEncipherment};
use x509::ExtKeyUsage::{ClientAuth, ServerAuth};