# 3. `oid_usage.rs` — Working with Object Identifiers
[← Example index](index.md) · [oid_usage.rs on Codeberg](https://codeberg.org/abbra/synta/src/branch/main/examples/oid_usage.rs)
Create OIDs from dot-notation and arc components, encode and decode them,
compare OIDs, and look up well-known algorithm OIDs.
## Source
```rust,ignore
//! Example: Working with ASN.1 Object Identifiers (OIDs)
//!
//! This example demonstrates how to create, encode, decode, and use
//! Object Identifiers which are commonly used in X.509 certificates
//! and other cryptographic protocols.
//!
//! Run with: cargo run --example oid_usage
use std::str::FromStr;
use synta::{Decoder, Encoder, Encoding, ObjectIdentifier};
fn main() {
println!("=== ASN.1 Object Identifier (OID) Example ===\n");
// Common OIDs used in cryptography and X.509
common_oids();
// Encoding and decoding
encode_decode_example();
// Working with OID components
oid_components();
}
fn common_oids() {
println!("Common OIDs in Cryptography:");
println!("-----------------------------");
let oids = vec![
("RSA Encryption", vec![1, 2, 840, 113549, 1, 1, 1]),
("SHA-256", vec![2, 16, 840, 1, 101, 3, 4, 2, 1]),
("ECDSA with SHA-256", vec![1, 2, 840, 10045, 4, 3, 2]),
("Common Name (CN)", vec![2, 5, 4, 3]),
("Country (C)", vec![2, 5, 4, 6]),
("Organization (O)", vec![2, 5, 4, 10]),
];
for (name, components) in oids {
let oid = ObjectIdentifier::new(&components).unwrap();
println!(" {}: {}", name, oid);
}
println!();
}
fn encode_decode_example() {
println!("Encoding and Decoding Example:");
println!("------------------------------");
// Create OID for RSA encryption: 1.2.840.113549.1.1.1
let components = vec![1, 2, 840, 113549, 1, 1, 1];
let oid = ObjectIdentifier::new(&components).unwrap();
println!("Created OID: {}", oid);
// Encode to DER
let mut encoder = Encoder::new(Encoding::Der);
encoder.encode(&oid).unwrap();
let encoded = encoder.finish().unwrap();
println!("Encoded: {} bytes", encoded.len());
println!("Bytes: {:02X?}", encoded);
// Decode
let mut decoder = Decoder::new(&encoded, Encoding::Der);
let decoded: ObjectIdentifier = decoder.decode().unwrap();
println!("Decoded: {}", decoded);
println!("Match: {}", oid == decoded);
println!();
}
fn oid_components() {
println!("Working with OID Components:");
println!("----------------------------");
// Parse OID from string notation
let oid_str = "2.5.29.19"; // Basic Constraints extension
let oid = ObjectIdentifier::from_str(oid_str).unwrap();
println!("OID: {}", oid);
println!("Components: {:?}", oid.components());
println!("Number of components: {}", oid.components().len());
// Access individual components
let components = oid.components();
for (i, component) in components.iter().enumerate() {
println!(" Component {}: {}", i, component);
}
println!();
// Common X.509 extension OIDs
println!("Common X.509 Extension OIDs:");
let extensions = vec![
("Subject Alternative Name", "2.5.29.17"),
("Basic Constraints", "2.5.29.19"),
("Key Usage", "2.5.29.15"),
("Extended Key Usage", "2.5.29.37"),
("CRL Distribution Points", "2.5.29.31"),
("Authority Key Identifier", "2.5.29.35"),
];
for (name, oid_str) in extensions {
let oid = ObjectIdentifier::from_str(oid_str).unwrap();
println!(" {}: {}", name, oid);
}
}
```