Struct yasna::DERWriter[][src]

pub struct DERWriter<'a> { /* fields omitted */ }

A writer object that accepts an ASN.1 value.

The two main sources of DERWriterSeq are:

Examples

use yasna;
let der = yasna::construct_der(|writer| {
    writer.write_i64(10)
});
assert_eq!(der, vec![2, 1, 10]);

Implementations

impl<'a> DERWriter<'a>[src]

pub fn write_bool(self, val: bool)[src]

Writes bool as an ASN.1 BOOLEAN value.

Examples

use yasna;
let der = yasna::construct_der(|writer| {
    writer.write_bool(true)
});
assert_eq!(der, vec![1, 1, 255]);

pub fn write_enum(self, val: i64)[src]

Writes i64 as an ASN.1 ENUMERATED value.

Examples

use yasna;
let der = yasna::construct_der(|writer| {
    writer.write_enum(2)
});
assert_eq!(der, vec![10, 1, 2]);

pub fn write_i64(self, val: i64)[src]

Writes i64 as an ASN.1 INTEGER value.

Examples

use yasna;
let der = yasna::construct_der(|writer| {
    writer.write_i64(1234567890)
});
assert_eq!(der, vec![2, 4, 73, 150, 2, 210]);

pub fn write_u64(self, val: u64)[src]

Writes u64 as an ASN.1 INTEGER value.

pub fn write_i32(self, val: i32)[src]

Writes i32 as an ASN.1 INTEGER value.

pub fn write_u32(self, val: u32)[src]

Writes u32 as an ASN.1 INTEGER value.

pub fn write_i16(self, val: i16)[src]

Writes i16 as an ASN.1 INTEGER value.

pub fn write_u16(self, val: u16)[src]

Writes u16 as an ASN.1 INTEGER value.

pub fn write_i8(self, val: i8)[src]

Writes i8 as an ASN.1 INTEGER value.

pub fn write_u8(self, val: u8)[src]

Writes u8 as an ASN.1 INTEGER value.

pub fn write_bitvec_bytes(self, bytes: &[u8], len: usize)[src]

Writes &[u8] and usize as an ASN.1 BITSTRING value.

This function is similar to write_bitvec, but is available even if the bit-vec feature is disabled.

Examples

use yasna;
let der_1 = yasna::construct_der(|writer| {
    writer.write_bitvec_bytes(&[117, 13, 64], 18)
});
let der_2 = yasna::construct_der(|writer| {
    writer.write_bitvec_bytes(&[117, 13, 65], 18)
});
assert_eq!(&der_1, &[3, 4, 6, 117, 13, 64]);
assert_eq!(&der_2, &[3, 4, 6, 117, 13, 64]);

pub fn write_bytes(self, bytes: &[u8])[src]

Writes &[u8] as an ASN.1 OCTETSTRING value.

Examples

use yasna;
let der = yasna::construct_der(|writer| {
    writer.write_bytes(b"Hello!")
});
assert_eq!(der, vec![4, 6, 72, 101, 108, 108, 111, 33]);

pub fn write_utf8_string(self, string: &str)[src]

Writes &str as an ASN.1 UTF8String value.

Examples

use yasna;
let der = yasna::construct_der(|writer| {
    writer.write_utf8_string("Hello!")
});
assert_eq!(der, vec![12, 6, 72, 101, 108, 108, 111, 33]);

pub fn write_ia5_string(self, string: &str)[src]

Writes &str as an ASN.1 IA5String value.

Examples

use yasna;
let der = yasna::construct_der(|writer| {
    writer.write_ia5_string("Hello!")
});
assert_eq!(der, vec![22, 6, 72, 101, 108, 108, 111, 33]);

pub fn write_bmp_string(self, string: &str)[src]

Writes &str as an ASN.1 BMPString value.

Examples

use yasna;
let der = yasna::construct_der(|writer| {
    writer.write_bmp_string("❤πü2?")
});
assert_eq!(der, vec![30, 10, 39, 100, 3, 192, 0, 252, 0, 50, 0, 63]);

pub fn write_null(self)[src]

Writes the ASN.1 NULL value.

Examples

use yasna;
let der = yasna::construct_der(|writer| {
    writer.write_null()
});
assert_eq!(der, vec![5, 0]);

pub fn write_oid(self, oid: &ObjectIdentifier)[src]

Writes an ASN.1 object identifier.

Examples

use yasna;
use yasna::models::ObjectIdentifier;
let der = yasna::construct_der(|writer| {
    writer.write_oid(&ObjectIdentifier::from_slice(
        &[1, 2, 840, 113549, 1, 1]))
});
assert_eq!(&der, &[6, 8, 42, 134, 72, 134, 247, 13, 1, 1]);

Panics

It panics when the OID cannot be canonically encoded in BER.

pub fn write_utf8string(self, string: &str)[src]

Writes an ASN.1 UTF8String.

Examples

use yasna;
let der = yasna::construct_der(|writer| {
    writer.write_utf8string("gnaw ροκανίζω 𪘂る")
});
assert_eq!(&der, &[
    12, 29, 103, 110, 97, 119, 32, 207, 129, 206, 191, 206,
    186, 206, 177, 206, 189, 206, 175, 206, 182, 207,
    137, 32, 240, 170, 152, 130, 227, 130, 139]);

pub fn write_sequence<T, F>(self, callback: F) -> T where
    F: FnOnce(&mut DERWriterSeq<'_>) -> T, 
[src]

Writes ASN.1 SEQUENCE.

This function uses the loan pattern: callback is called back with a DERWriterSeq, to which the contents of the SEQUENCE is written.

This is equivalent to write_sequence_of in behaviors.

Examples

use yasna;
let der = yasna::construct_der(|writer| {
    writer.write_sequence(|writer| {
        writer.next().write_i64(10);
        writer.next().write_bool(true);
    })
});
assert_eq!(der, vec![48, 6, 2, 1, 10, 1, 1, 255]);

pub fn write_sequence_of<T, F>(self, callback: F) -> T where
    F: FnOnce(&mut DERWriterSeq<'_>) -> T, 
[src]

Writes ASN.1 SEQUENCE OF.

This function uses the loan pattern: callback is called back with a DERWriterSeq, to which the contents of the SEQUENCE OF are written.

This is equivalent to write_sequence in behaviors.

Examples

use yasna;
let der = yasna::construct_der(|writer| {
    writer.write_sequence_of(|writer| {
        for &i in &[10, -129] {
            writer.next().write_i64(i);
        }
    })
});
assert_eq!(der, vec![48, 7, 2, 1, 10, 2, 2, 255, 127]);

pub fn write_set<T, F>(self, callback: F) -> T where
    F: FnOnce(&mut DERWriterSet<'_>) -> T, 
[src]

Writes ASN.1 SET.

This function uses the loan pattern: callback is called back with a DERWriterSet, to which the contents of the SET are written.

For SET OF values, use write_set_of instead.

Examples

use yasna;
let der = yasna::construct_der(|writer| {
    writer.write_set(|writer| {
        writer.next().write_i64(10);
        writer.next().write_bool(true);
    })
});
assert_eq!(der, vec![49, 6, 1, 1, 255, 2, 1, 10]);

pub fn write_set_of<T, F>(self, callback: F) -> T where
    F: FnOnce(&mut DERWriterSet<'_>) -> T, 
[src]

Writes ASN.1 SET OF.

This function uses the loan pattern: callback is called back with a DERWriterSet, to which the contents of the SET OF are written.

For SET values, use write_set instead.

Examples

use yasna;
let der = yasna::construct_der(|writer| {
    writer.write_set_of(|writer| {
        for &i in &[10, -129] {
            writer.next().write_i64(i);
        }
    })
});
assert_eq!(der, vec![49, 7, 2, 1, 10, 2, 2, 255, 127]);

pub fn write_numeric_string(self, string: &str)[src]

Writes an ASN.1 NumericString.

Examples

use yasna;
let der = yasna::construct_der(|writer| {
    writer.write_numeric_string("128 256")
});
assert_eq!(&der, &[18, 7, 49, 50, 56, 32, 50, 53, 54]);

pub fn write_printable_string(self, string: &str)[src]

Writes an ASN.1 PrintableString.

Examples

use yasna;
let der = yasna::construct_der(|writer| {
    writer.write_printable_string("Co., Ltd.")
});
assert_eq!(&der, &[19, 9, 67, 111, 46, 44, 32, 76, 116, 100, 46]);

pub fn write_utctime(self, datetime: &UTCTime)[src]

Writes an ASN.1 UTCTime.

Examples

use yasna;
use yasna::models::UTCTime;
use chrono::{Utc,TimeZone};
let der = yasna::construct_der(|writer| {
    writer.write_utctime(
        &UTCTime::from_datetime(&Utc.timestamp(378820800, 0)))
});
assert_eq!(&der, &[
    23, 13, 56, 50, 48, 49, 48, 50, 49, 50, 48, 48, 48, 48, 90]);

Features

This method is enabled by chrono feature.

[dependencies]
yasna = { version = "*", features = ["chrono"] }

pub fn write_generalized_time(self, datetime: &GeneralizedTime)[src]

Writes an ASN.1 GeneralizedTime.

Examples

use yasna;
use yasna::models::GeneralizedTime;
use chrono::{Utc,TimeZone};
let der = yasna::construct_der(|writer| {
    writer.write_generalized_time(
        &GeneralizedTime::from_datetime(
            &Utc.timestamp(500159309, 724_000_000)))
});
assert_eq!(&der, &[
    24, 19, 49, 57, 56, 53, 49, 49, 48, 54, 50,
    49, 48, 56, 50, 57, 46, 55, 50, 52, 90]);

Features

This method is enabled by chrono feature.

[dependencies]
yasna = { version = "*", features = ["chrono"] }

pub fn write_visible_string(self, string: &str)[src]

Writes an ASN.1 VisibleString.

Examples

use yasna;
let der = yasna::construct_der(|writer| {
    writer.write_visible_string("Hi!")
});
assert_eq!(&der, &[26, 3, 72, 105, 33]);

pub fn write_tagged<T, F>(self, tag: Tag, callback: F) -> T where
    F: FnOnce(DERWriter<'_>) -> T, 
[src]

Writes a (explicitly) tagged value.

Examples

use yasna::{self,Tag};
let der = yasna::construct_der(|writer| {
    writer.write_tagged(Tag::context(3), |writer| {
        writer.write_i64(10)
    })
});
assert_eq!(der, vec![163, 3, 2, 1, 10]);

Note: you can achieve the same using write_tagged_implicit:

use yasna::{self,Tag};
let der = yasna::construct_der(|writer| {
    writer.write_tagged_implicit(Tag::context(3), |writer| {
        writer.write_sequence(|writer| {
            let writer = writer.next();
            writer.write_i64(10)
        })
    })
});
assert_eq!(der, vec![163, 3, 2, 1, 10]);

pub fn write_tagged_implicit<T, F>(self, tag: Tag, callback: F) -> T where
    F: FnOnce(DERWriter<'_>) -> T, 
[src]

Writes an implicitly tagged value.

Examples

use yasna::{self,Tag};
let der = yasna::construct_der(|writer| {
    writer.write_tagged_implicit(Tag::context(3), |writer| {
        writer.write_i64(10)
    })
});
assert_eq!(der, vec![131, 1, 10]);

pub fn write_tagged_der(self, der: &TaggedDerValue)[src]

Writes the arbitrary tagged DER value in der.

Examples

use yasna;
use yasna::models::TaggedDerValue;
use yasna::tags::TAG_OCTETSTRING;
let tagged_der_value = TaggedDerValue::from_tag_and_bytes(TAG_OCTETSTRING, b"Hello!".to_vec());
let der1 = yasna::construct_der(|writer| {
    writer.write_tagged_der(&tagged_der_value)
});
let der2 = yasna::construct_der(|writer| {
    writer.write_bytes(b"Hello!")
});
assert_eq!(der1, der2);

pub fn write_der(self, der: &[u8])[src]

Writes &[u8] into the DER output buffer directly. Properly encoded tag and length must be included at the start of the passed buffer.

Examples

use yasna;
let raw_der = yasna::construct_der(|writer| {
    writer.write_der(b"\x04\x06Hello!")
});
let der = yasna::construct_der(|writer| {
    writer.write_bytes(b"Hello!")
});
assert_eq!(raw_der, der);

Trait Implementations

impl<'a> Debug for DERWriter<'a>[src]

Auto Trait Implementations

impl<'a> RefUnwindSafe for DERWriter<'a>

impl<'a> Send for DERWriter<'a>

impl<'a> Sync for DERWriter<'a>

impl<'a> Unpin for DERWriter<'a>

impl<'a> !UnwindSafe for DERWriter<'a>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.