Cleaned up BigNum constructors

This commit is contained in:
Valerii Hiora 2014-10-14 22:39:07 +03:00
parent 60dce4c219
commit 3164ac0214
1 changed files with 11 additions and 25 deletions

View File

@ -79,41 +79,27 @@ macro_rules! with_bn_in_ctx(
) )
impl BigNum { impl BigNum {
// FIXME: squash 3 constructors into one
pub fn new() -> Result<BigNum, SslError> { pub fn new() -> Result<BigNum, SslError> {
unsafe { unsafe {
ffi::init(); ffi::init();
let v = ffi::BN_new();
if v.is_null() { let v = try_ssl_null!(ffi::BN_new());
Err(SslError::get()) Ok(BigNum(v))
} else {
Ok(BigNum(v))
}
} }
} }
pub fn new_from(n: u64) -> Result<BigNum, SslError> { pub fn new_from(n: u64) -> Result<BigNum, SslError> {
unsafe { BigNum::new().and_then(|v| unsafe {
ffi::init(); try_ssl!(ffi::BN_set_word(v.raw(), n as c_ulong));
let bn = ffi::BN_new(); Ok(v)
if bn.is_null() || ffi::BN_set_word(bn, n as c_ulong) == 0 { })
Err(SslError::get())
} else {
Ok(BigNum(bn))
}
}
} }
pub fn new_from_slice(n: &[u8]) -> Result<BigNum, SslError> { pub fn new_from_slice(n: &[u8]) -> Result<BigNum, SslError> {
unsafe { BigNum::new().and_then(|v| unsafe {
ffi::init(); try_ssl_null!(ffi::BN_bin2bn(n.as_ptr(), n.len() as c_int, v.raw()));
let bn = ffi::BN_new(); Ok(v)
if bn.is_null() || ffi::BN_bin2bn(n.as_ptr(), n.len() as c_int, bn).is_null() { })
Err(SslError::get())
} else {
Ok(BigNum(bn))
}
}
} }
pub fn checked_sqr(&self) -> Result<BigNum, SslError> { pub fn checked_sqr(&self) -> Result<BigNum, SslError> {