Skip to content

Commit

Permalink
Support EVP_get_digestbyname binding
Browse files Browse the repository at this point in the history
  • Loading branch information
crab2313 committed Dec 17, 2020
1 parent 74f380e commit 4240381
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions openssl/src/hash.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use ffi;
use std::ffi::CString;
use std::fmt;
use std::io;
use std::io::prelude::*;
Expand Down Expand Up @@ -46,6 +47,25 @@ impl MessageDigest {
}
}

/// Returns the `MessageDigest` corresponding to an algorithm name.
///
/// This corresponds to [`EVP_get_digestbyname`].
///
/// [`EVP_get_digestbyname`]: https://www.openssl.org/docs/man1.1.0/crypto/EVP_DigestInit.html
pub fn from_name(name: &str) -> Option<MessageDigest> {
#[cfg(not(ossl110))]
ffi::init();
let name = CString::new(name).ok()?;
unsafe {
let ptr = ffi::EVP_get_digestbyname(name.as_ptr());
if ptr.is_null() {
None
} else {
Some(MessageDigest(ptr))
}
}
}

pub fn null() -> MessageDigest {
unsafe { MessageDigest(ffi::EVP_md_null()) }
}
Expand Down Expand Up @@ -614,4 +634,12 @@ mod tests {
MessageDigest::sha256().as_ptr()
);
}

#[test]
fn from_name() {
assert_eq!(
MessageDigest::from_name("SHA256").unwrap().as_ptr(),
MessageDigest::sha256().as_ptr()
)
}
}

0 comments on commit 4240381

Please sign in to comment.