pub struct DerRef { /* private fields */ }
Expand description

DerRef is a wrapper of [u8] and represents DER.

This struct is ‘Unsized’, and user will usually uses a reference.

Implementations

Parses bytes starting with octets of ‘ASN.1 DER’ and returns a reference to DerRef.

This function ignores extra octet(s) at the end of bytes if any.

This function is same to <&DerRef>::try_from.

Warnings

ASN.1 does not allow some universal identifier for DER, however, this function will accept such an identifier. For example, ‘Octet String’ must be primitive in DER, but this function returns Ok for constructed Octet String DER.

Examples
use bsn1::DerRef;

// Represents '8' as Integer.
let bytes0: &[u8] = &[0x02, 0x01, 0x08];
let der0 = DerRef::from_bytes(bytes0).unwrap();

// The result is not changed even if extra octets are added to the end.
let bytes1: &[u8] = &[0x02, 0x01, 0x08, 0x00, 0xff];
let der1 = DerRef::from_bytes(bytes1).unwrap();

assert_eq!(der0, der1);

Provides a reference from bytes without any check.

bytes must not include any extra octet.

If it is not sure whether bytes is valid octets as an ‘DER’ or not, use TryFrom implementation or from_bytes.

Safety

The behavior is undefined if bytes is not formatted as a DER.

Examples
use bsn1::DerRef;

let bytes: &[u8] = &[0x02, 0x01, 0x08];  // Represents '8' as Integer.
let _der: &DerRef = unsafe { DerRef::from_bytes_unchecked(bytes) };

Returns a reference to the IdRef of self .

Examples
use bsn1::{DerRef, IdRef};

let bytes: &[u8] = &[0x02, 0x01, 0x04];  // Represents '4' as Integer.
let der = DerRef::from_bytes(bytes).unwrap();

assert_eq!(IdRef::integer(), der.id());

Returns Length to represent the length of contents.

Note that DER does not allow indefinite Length. The return value must be Length::Definite.

Warnings

Length stands for the length octets in DER; i.e. the length of the contents. The total byte count of the DER is greater than the value.

Examples
use bsn1::{DerRef, Length};

let bytes: &[u8] = &[0x04, 0x02, 0x00, 0xff];  // Represents '[0x00, 0xff]' as Octet String
let der = DerRef::from_bytes(bytes).unwrap();

assert_eq!(Length::Definite(2), der.length());

Returns a reference to the contents octets of self .

Examples
use bsn1::{ContentsRef, DerRef};

let bytes: &[u8] = &[0x04, 0x02, 0x00, 0xff];  // Represents '[0x00, 0xff]' as Octet String
let der = DerRef::from_bytes(bytes).unwrap();
let contents = ContentsRef::from_bytes(&bytes[2..]);

assert_eq!(contents, der.contents());

Provides a reference to the inner slice.

Example
use bsn1::DerRef;

// This octets represents `3` as integer.
let bytes = vec![0x02, 0x01, 0x03];

let der = DerRef::from_bytes(&bytes).unwrap();
assert_eq!(&bytes, der.as_bytes());

Trait Implementations

Converts this type into a shared reference of the (usually inferred) input type.
Immutably borrows from an owned value. Read more
Immutably borrows from an owned value. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Feeds this value into the given Hasher. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more

Parses bytes starting with octets of ‘ASN.1 DER’ and returns a reference to DerRef .

This function ignores extra octet(s) at the end of bytes if any.

This function is same to DerRef::from_bytes.

Warnings

ASN.1 does not allow some universal identifier for DER, however, this function will accept such an identifier. For example, ‘Octet String’ must be primitive in DER, but this function returns Ok for constructed Octet String DER.

The type returned in the event of a conversion error.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more