diff options
| author | Steven Fackler <[email protected]> | 2018-05-24 21:32:13 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2018-05-24 21:32:13 -0700 |
| commit | 4f3d72d98c527f571ac50fcb41317bc44d0fbfda (patch) | |
| tree | fcef80401d7a31c1349b877f72b7318b67bd557e /openssl/src/hash.rs | |
| parent | Merge pull request #933 from sfackler/bogus-sni (diff) | |
| parent | Rename X509Ref::fingerprint to X509Ref::digest and avoid allocating (diff) | |
| download | rust-openssl-4f3d72d98c527f571ac50fcb41317bc44d0fbfda.tar.xz rust-openssl-4f3d72d98c527f571ac50fcb41317bc44d0fbfda.zip | |
Merge pull request #934 from sfackler/digest-algo
Add some digest support
Diffstat (limited to 'openssl/src/hash.rs')
| -rw-r--r-- | openssl/src/hash.rs | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/openssl/src/hash.rs b/openssl/src/hash.rs index 582a2ada..dd4d136c 100644 --- a/openssl/src/hash.rs +++ b/openssl/src/hash.rs @@ -4,6 +4,10 @@ use std::io; use std::io::prelude::*; use std::ops::{Deref, DerefMut}; +use error::ErrorStack; +use nid::Nid; +use {cvt, cvt_p}; + cfg_if! { if #[cfg(ossl110)] { use ffi::{EVP_MD_CTX_free, EVP_MD_CTX_new}; @@ -12,9 +16,6 @@ cfg_if! { } } -use error::ErrorStack; -use {cvt, cvt_p}; - #[derive(Copy, Clone)] pub struct MessageDigest(*const ffi::EVP_MD); @@ -23,6 +24,22 @@ impl MessageDigest { MessageDigest(x) } + /// Returns the `MessageDigest` corresponding to an `Nid`. + /// + /// This corresponds to [`EVP_get_digestbynid`]. + /// + /// [`EVP_get_digestbynid`]: https://www.openssl.org/docs/man1.1.0/crypto/EVP_DigestInit.html + pub fn from_nid(type_: Nid) -> Option<MessageDigest> { + unsafe { + let ptr = ffi::EVP_get_digestbynid(type_.as_raw()); + if ptr.is_null() { + None + } else { + Some(MessageDigest(ptr)) + } + } + } + pub fn md5() -> MessageDigest { unsafe { MessageDigest(ffi::EVP_md5()) } } @@ -234,8 +251,8 @@ impl Drop for Hasher { /// store the digest data. #[derive(Copy)] pub struct DigestBytes { - buf: [u8; ffi::EVP_MAX_MD_SIZE as usize], - len: usize, + pub(crate) buf: [u8; ffi::EVP_MAX_MD_SIZE as usize], + pub(crate) len: usize, } impl Clone for DigestBytes { @@ -405,4 +422,12 @@ mod tests { hash_test(MessageDigest::ripemd160(), test); } } + + #[test] + fn from_nid() { + assert_eq!( + MessageDigest::from_nid(Nid::SHA256).unwrap().as_ptr(), + MessageDigest::sha256().as_ptr() + ); + } } |