Add `Asn1Integer::from_bn`
This commit is contained in:
parent
79abbeaa81
commit
4ff5f4486f
|
|
@ -33,7 +33,7 @@ use std::slice;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
use bio::MemBio;
|
use bio::MemBio;
|
||||||
use bn::BigNum;
|
use bn::{BigNum, BigNumRef};
|
||||||
use error::ErrorStack;
|
use error::ErrorStack;
|
||||||
use nid::Nid;
|
use nid::Nid;
|
||||||
use string::OpensslString;
|
use string::OpensslString;
|
||||||
|
|
@ -191,6 +191,20 @@ foreign_type_and_impl_send_sync! {
|
||||||
pub struct Asn1IntegerRef;
|
pub struct Asn1IntegerRef;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Asn1Integer {
|
||||||
|
/// Converts a bignum to an `Asn1Integer`.
|
||||||
|
///
|
||||||
|
/// Corresponds to [`BN_to_ASN1_INTEGER`].
|
||||||
|
///
|
||||||
|
/// [`BN_to_ASN1_INTEGER`]: https://www.openssl.org/docs/man1.1.0/crypto/BN_to_ASN1_INTEGER.html
|
||||||
|
pub fn from_bn(bn: &BigNumRef) -> Result<Self, ErrorStack> {
|
||||||
|
unsafe {
|
||||||
|
cvt_p(::ffi::BN_to_ASN1_INTEGER(bn.as_ptr(), ptr::null_mut()))
|
||||||
|
.map(|p| Asn1Integer::from_ptr(p))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Asn1IntegerRef {
|
impl Asn1IntegerRef {
|
||||||
#[allow(missing_docs)]
|
#[allow(missing_docs)]
|
||||||
#[deprecated(since = "0.10.6", note = "use to_bn instead")]
|
#[deprecated(since = "0.10.6", note = "use to_bn instead")]
|
||||||
|
|
@ -306,3 +320,24 @@ cfg_if! {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use bn::BigNum;
|
||||||
|
|
||||||
|
/// Tests conversion between BigNum and Asn1Integer.
|
||||||
|
#[test]
|
||||||
|
fn bn_cvt() {
|
||||||
|
fn roundtrip(bn: BigNum) {
|
||||||
|
let large = Asn1Integer::from_bn(&bn).unwrap();
|
||||||
|
assert_eq!(large.to_bn().unwrap(), bn);
|
||||||
|
}
|
||||||
|
|
||||||
|
roundtrip(BigNum::from_dec_str("1000000000000000000000000000000000").unwrap());
|
||||||
|
roundtrip(-BigNum::from_dec_str("1000000000000000000000000000000000").unwrap());
|
||||||
|
roundtrip(BigNum::from_u32(1234).unwrap());
|
||||||
|
roundtrip(-BigNum::from_u32(1234).unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue