Trait yasna::DEREncodable
source · pub trait DEREncodable {
// Required method
fn encode_der<'a>(&self, writer: DERWriter<'a>);
}
Expand description
Types encodable in DER.
§Examples
use yasna;
let der = yasna::encode_der::<i64>(&65535);
assert_eq!(&der, &[2, 3, 0, 255, 255]);
§Limitations
Rust types don’t correspond to ASN.1 types one-to-one. Not all kinds
of ASN.1 types can be encoded via default DEREncodable
implementation.
If you want to encode ASN.1, you may implement DEREncodable
for your
own types or use construct_der
.
§Default implementations
- The encoder for
Vec<T>
/[T]
is implemented as SEQUENCE OF encoder. ()
as NULL encoder.- Tuples (except
()
) as SEQUENCE encoder. Vec<u8>
/[u8]
as OCTETSTRING encoder.BitVec
as BITSTRING encoder.String
/str
as UTF8String encoder.i64
,u64
,i32
,u32
,i16
,u16
,BigInt
,BigUint
as INTEGER encoder. (u8
is avoided because of confliction.)bool
as BOOLEAN encoder.ObjectIdentifier
as OBJECTT IDENTIFIER encoder.UTCTime
/GeneralizedTime
as UTCTime/GeneralizedTime encoder.
Required Methods§
sourcefn encode_der<'a>(&self, writer: DERWriter<'a>)
fn encode_der<'a>(&self, writer: DERWriter<'a>)
Writes the value as an DER-encoded ASN.1 value.
§Examples
use yasna::{DEREncodable,DERWriter};
struct Entry {
name: String,
age: i64,
}
impl DEREncodable for Entry {
fn encode_der(&self, writer: DERWriter) {
writer.write_sequence(|writer| {
writer.next().write_visible_string(&self.name);
writer.next().write_i64(self.age);
})
}
}
fn main() {
let entry = Entry {
name: String::from("John"),
age: 32,
};
let der = yasna::encode_der(&entry);
assert_eq!(&der, &[48, 9, 26, 4, 74, 111, 104, 110, 2, 1, 32]);
}