///|
test "read a single element" {
let value = try! @asn1.DerReader::from_bytes(b"\x02\x01\x2a").read_single()
inspect(value.tag().number, content="2")
inspect(value.content().length(), content="1")
}///|
test "decode an integer" {
let content = try! @asn1.DerReader::from_bytes(b"\x02\x01\x2a").read_expected(
@asn1.TAG_INTEGER,
)
inspect(try! @asn1.decode_integer(content).to_string(), content="42")
}///|
test "decode an object identifier" {
let content = try! @asn1.DerReader::from_bytes(
b"\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x01",
).read_expected(@asn1.TAG_OBJECT_IDENTIFIER)
inspect(
try! @asn1.decode_object_identifier(content).to_string(),
content="1.2.840.113549.1.1.1",
)
}///|
test "encode an integer" {
inspect(@hex.encode(try! @asn1.encode_int(128)), content="02020080")
inspect(@hex.encode(try! @asn1.encode_int(-129)), content="0202ff7f")
}///|
test "build a sequence" {
let writer = @asn1.DerWriter::new()
writer.add(try! @asn1.encode_int(1)[:])
writer.add(try! @asn1.encode_boolean(true)[:])
inspect(@hex.encode(try! writer.to_sequence()), content="30060201010101ff")
}///|
test "decode and re-encode" {
let bytes : Bytes = b"\x30\x08\x02\x01\x2a\x30\x03\x01\x01\xff"
let value = try! @asn1.DerReader::from_bytes(bytes).read_single()
inspect(value.child(1).child(0).as_boolean(), content="true")
inspect(
@hex.encode(try! value.encode()) == @hex.encode(bytes),
content="true",
)
}///|
test "encode a time" {
let time = @asn1.Asn1Time::new(2024, 5, 6, 7, 8, 9)
inspect(time.to_string(), content="2024-05-06T07:08:09Z")
inspect(
@hex.encode(try! @asn1.encode_utc_time(time)),
content="170d3234303530363037303830395a",
)
}pub(all) suberror DerError {
UnexpectedEof
InvalidTag(Int)
IndefiniteLength
Truncated(Int)
LengthTooLong(Int)
NonMinimalLength(Int)
ExpectedTag(String, String)
InvalidValue(String)
TrailingData(Int)
} derive(Debug)pub struct Asn1Time {
year : Int
month : Int
day : Int
hour : Int
minute : Int
second : Int
utc_offset_minutes : Int
}pub struct DerReader {
input : BytesView
pos : Int
}let TAG_BOOLEAN : Intfn compare_octets(a : BytesView, b : BytesView) -> Intfn days_in_month(year : Int, month : Int) -> Intfn is_leap_year(year : Int) -> BoolInstall
Download zipPure-MoonBit ASN.1 DER codec and X.509 certificate toolkit.