Skip to main content

encode_sequence/
encode_sequence.rs

1//! Example: Building and encoding ASN.1 sequences
2//!
3//! This example demonstrates how to build complex ASN.1 structures
4//! using sequences and encode them to DER format.
5//!
6//! Run with: cargo run --example encode_sequence
7
8use synta::{Boolean, Decoder, Element, Encoding, Integer, OctetStringRef, Sequence, ToDer};
9
10fn main() {
11    println!("=== ASN.1 Sequence Encoding Example ===\n");
12
13    // Example 1: Simple sequence with different types
14    simple_sequence();
15
16    // Example 2: Nested sequences
17    nested_sequence();
18
19    // Example 3: Sequence roundtrip
20    roundtrip_example();
21}
22
23fn simple_sequence() {
24    println!("Example 1: Simple Sequence");
25    println!("--------------------------");
26
27    // Create a sequence with multiple elements
28    let mut seq = Sequence::new();
29    let octet_data = vec![0xDE, 0xAD, 0xBE, 0xEF];
30    seq.push(Element::Integer(Integer::from(42)));
31    seq.push(Element::Boolean(Boolean::new(true)));
32    seq.push(Element::OctetString(OctetStringRef::new(&octet_data)));
33
34    println!("Created sequence with {} elements", seq.iter().count());
35
36    let encoded = seq.to_der().unwrap();
37    println!("Encoded to {} bytes: {:02X?}", encoded.len(), encoded);
38    println!();
39}
40
41fn nested_sequence() {
42    println!("Example 2: Nested Sequences");
43    println!("---------------------------");
44
45    // Create an inner sequence
46    let mut inner = Sequence::new();
47    inner.push(Element::Integer(Integer::from(1)));
48    inner.push(Element::Integer(Integer::from(2)));
49    inner.push(Element::Integer(Integer::from(3)));
50
51    // Create outer sequence containing the inner one
52    let mut outer = Sequence::new();
53    outer.push(Element::Sequence(inner));
54    outer.push(Element::Integer(Integer::from(100)));
55
56    println!("Created nested structure");
57
58    let encoded = outer.to_der().unwrap();
59    println!("Encoded to {} bytes: {:02X?}", encoded.len(), encoded);
60
61    // Parse the structure
62    println!("\nParsing structure:");
63    println!("  Tag: 0x{:02X} (SEQUENCE)", encoded[0]);
64    println!("  Length: {} bytes", encoded[1]);
65    println!();
66}
67
68fn roundtrip_example() {
69    println!("Example 3: Roundtrip Encoding/Decoding");
70    println!("--------------------------------------");
71
72    // Create a complex structure
73    let mut seq = Sequence::new();
74    seq.push(Element::Integer(Integer::from(12345)));
75    seq.push(Element::Boolean(Boolean::new(false)));
76    seq.push(Element::Integer(Integer::from(-67890)));
77
78    println!("Original sequence:");
79    println!("  Element 1: Integer(12345)");
80    println!("  Element 2: Boolean(false)");
81    println!("  Element 3: Integer(-67890)");
82
83    // Encode
84    let encoded = seq.to_der().unwrap();
85    println!("\nEncoded: {} bytes", encoded.len());
86
87    // Decode — Sequence<'_> borrows from the input buffer, so use Decoder directly
88    let mut decoder = Decoder::new(&encoded, Encoding::Der);
89    let decoded: Sequence = decoder.decode().unwrap();
90
91    println!("\nDecoded sequence:");
92    for (i, element) in decoded.into_elements().unwrap().iter().enumerate() {
93        match element {
94            Element::Integer(int) => {
95                println!("  Element {}: Integer({})", i + 1, int.as_i64().unwrap())
96            }
97            Element::Boolean(b) => println!("  Element {}: Boolean({})", i + 1, b.value()),
98            _ => println!("  Element {}: {:?}", i + 1, element),
99        }
100    }
101
102    println!("\nRoundtrip successful!");
103    println!();
104}