pub struct Integer<'a> { /* private fields */ }
Expand description
ASN.1 INTEGER
type
Generic representation for integer types. BER/DER integers can be of any size, so it is not possible to store them as simple integers (they are stored as raw bytes).
The internal representation can be obtained using .as_ref()
.
Note
Methods from/to BER and DER encodings are also implemented for primitive types
(u8
, u16
to u128
, and i8
to i128
).
In most cases, it is easier to use these types directly.
Examples
Creating an Integer
use asn1_rs::Integer;
// unsigned
let i = Integer::from(4);
assert_eq!(i.as_ref(), &[4]);
// signed
let j = Integer::from(-2);
assert_eq!(j.as_ref(), &[0x0, 0xff, 0xff, 0xff, 0xfe]);
Converting an Integer
to a primitive type (using the TryInto
trait)
use asn1_rs::{Error, Integer};
use std::convert::TryInto;
let i = Integer::new(&[0x12, 0x34, 0x56, 0x78]);
// converts to an u32
let n: u32 = i.try_into().unwrap();
// Same, but converting to an u16: will fail, value cannot fit into an u16
let i = Integer::new(&[0x12, 0x34, 0x56, 0x78]);
assert_eq!(i.try_into() as Result<u16, _>, Err(Error::IntegerTooLarge));
Encoding an Integer
to DER
use asn1_rs::{Integer, ToDer};
let i = Integer::from(4);
let v = i.to_der_vec().unwrap();
assert_eq!(&v, &[2, 1, 4]);
// same, with primitive types
let v = 4.to_der_vec().unwrap();
assert_eq!(&v, &[2, 1, 4]);
Implementations
sourceimpl<'a> Integer<'a>
impl<'a> Integer<'a>
Trait Implementations
sourceimpl<'a> CheckDerConstraints for Integer<'a>
impl<'a> CheckDerConstraints for Integer<'a>
fn check_constraints(any: &Any<'_>) -> Result<()>
sourceimpl ToDer for Integer<'_>
impl ToDer for Integer<'_>
sourcefn to_der_len(&self) -> Result<usize>
fn to_der_len(&self) -> Result<usize>
Get the length of the object, when encoded Read more
sourcefn write_der_header(&self, writer: &mut dyn Write) -> SerializeResult<usize>
fn write_der_header(&self, writer: &mut dyn Write) -> SerializeResult<usize>
Attempt to write the DER header to this writer.
sourcefn write_der_content(&self, writer: &mut dyn Write) -> SerializeResult<usize>
fn write_der_content(&self, writer: &mut dyn Write) -> SerializeResult<usize>
Attempt to write the DER content (all except header) to this writer.
sourcefn to_der_vec(&self) -> SerializeResult<Vec<u8>>
fn to_der_vec(&self) -> SerializeResult<Vec<u8>>
Write the DER encoded representation to a newly allocated Vec<u8>
.
sourcefn to_der_vec_raw(&self) -> SerializeResult<Vec<u8>>
fn to_der_vec_raw(&self) -> SerializeResult<Vec<u8>>
Similar to using to_vec
, but uses provided values without changes.
This can generate an invalid encoding for a DER object. Read more
sourcefn write_der(&self, writer: &mut dyn Write) -> SerializeResult<usize>
fn write_der(&self, writer: &mut dyn Write) -> SerializeResult<usize>
Attempt to write the DER encoded representation (header and content) into this writer. Read more
sourcefn write_der_raw(&self, writer: &mut dyn Write) -> SerializeResult<usize>
fn write_der_raw(&self, writer: &mut dyn Write) -> SerializeResult<usize>
Similar to using to_der
, but uses provided values without changes.
This can generate an invalid encoding for a DER object. Read more
impl DerAutoDerive for Integer<'_>
impl<'a> Eq for Integer<'a>
impl<'a> StructuralEq for Integer<'a>
impl<'a> StructuralPartialEq for Integer<'a>
Auto Trait Implementations
impl<'a> RefUnwindSafe for Integer<'a>
impl<'a> Send for Integer<'a>
impl<'a> Sync for Integer<'a>
impl<'a> Unpin for Integer<'a>
impl<'a> UnwindSafe for Integer<'a>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more