aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChris Cole <[email protected]>2014-12-14 10:02:18 -0500
committerChris Cole <[email protected]>2014-12-14 10:02:18 -0500
commit38682821ade69780e5c125fa00f1f71a3b161ffd (patch)
tree3b75263e862f6c9f1743c9ce4647d111921d7e9f /src
parentAdded BigNum::one(). (diff)
downloadrust-openssl-38682821ade69780e5c125fa00f1f71a3b161ffd.tar.xz
rust-openssl-38682821ade69780e5c125fa00f1f71a3b161ffd.zip
Added BigNum::{from_dec_str,from_hex_str}, BN_dec2bn, and BN_hex2bn.
Diffstat (limited to 'src')
-rw-r--r--src/bn/mod.rs29
1 files changed, 27 insertions, 2 deletions
diff --git a/src/bn/mod.rs b/src/bn/mod.rs
index 7f33e9ed..7605fee0 100644
--- a/src/bn/mod.rs
+++ b/src/bn/mod.rs
@@ -84,8 +84,22 @@ impl BigNum {
})
}
- pub fn one() -> BigNum {
- BigNum::new_from(1).unwrap()
+ pub fn from_dec_str(s: &str) -> Result<BigNum, SslError> {
+ BigNum::new().and_then(|v| unsafe {
+ let ref mut bn_ptr = ffi::BIGNUM_PTR { ptr: v.raw(), };
+ let c_str = s.to_c_str();
+ try_ssl!(ffi::BN_dec2bn(bn_ptr, c_str.as_ptr()));
+ Ok(v)
+ })
+ }
+
+ pub fn from_hex_str(s: &str) -> Result<BigNum, SslError> {
+ BigNum::new().and_then(|v| unsafe {
+ let ref mut bn_ptr = ffi::BIGNUM_PTR { ptr: v.raw(), };
+ let c_str = s.to_c_str();
+ try_ssl!(ffi::BN_hex2bn(bn_ptr, c_str.as_ptr()));
+ Ok(v)
+ })
}
pub fn new_from_slice(n: &[u8]) -> Result<BigNum, SslError> {
@@ -362,6 +376,17 @@ impl BigNum {
str
}
}
+
+ pub fn to_hex_str(&self) -> String {
+ unsafe {
+ let buf = ffi::BN_bn2hex(self.raw());
+ assert!(!buf.is_null());
+ let c_str = CString::new(buf, false);
+ let str = c_str.as_str().unwrap().to_string();
+ ffi::CRYPTO_free(buf as *mut c_void);
+ str
+ }
+ }
}
impl fmt::Show for BigNum {