pub struct DERWriter<'a> { /* private fields */ }
Expand description

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

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]);

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]);

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]);

Writes u64 as an ASN.1 INTEGER value.

Writes i32 as an ASN.1 INTEGER value.

Writes u32 as an ASN.1 INTEGER value.

Writes i16 as an ASN.1 INTEGER value.

Writes u16 as an ASN.1 INTEGER value.

Writes i8 as an ASN.1 INTEGER value.

Writes u8 as an ASN.1 INTEGER value.

Writes BigInt as an ASN.1 INTEGER value.

Examples
use yasna;
use num_bigint::BigInt;
let der = yasna::construct_der(|writer| {
    writer.write_bigint(
        &BigInt::parse_bytes(b"1234567890", 10).unwrap())
});
assert_eq!(der, vec![2, 4, 73, 150, 2, 210]);
Features

This method is enabled by num feature.

[dependencies]
yasna = { version = "*", features = ["num-bigint"] }

Writes BigUint as an ASN.1 INTEGER value.

Examples
use yasna;
use num_bigint::BigUint;
let der = yasna::construct_der(|writer| {
    writer.write_biguint(
        &BigUint::parse_bytes(b"1234567890", 10).unwrap())
});
assert_eq!(der, vec![2, 4, 73, 150, 2, 210]);
Features

This method is enabled by num feature.

[dependencies]
yasna = { version = "*", features = ["num-bigint"] }

Writes BitVec as an ASN.1 BITSTRING value.

Examples
use yasna;
use bit_vec::BitVec;
let der = yasna::construct_der(|writer| {
    writer.write_bitvec(&
        [1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1,
            0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1]
        .iter().map(|&i| i != 0).collect())
});
assert_eq!(&der, &[3, 5, 3, 206, 213, 116, 24]);
Features

This method is enabled by bit-vec feature.

[dependencies]
yasna = { version = "*", features = ["bit-vec"] }

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]);

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]);

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]);

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]);

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]);

Writes the ASN.1 NULL value.

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

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.

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]);

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]);

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]);

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]);

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]);

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]);

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]);

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"] }

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"] }

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]);

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]);

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]);

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);

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

Formats the value using the given formatter. Read more

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

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.