der

    ASN.1 DER parser for MoonBit. Supports PKCS#1 and PKCS#8 RSA private key parsing.

    der
    asn1
    pem
    crypto
    rsa
    pkcs8
    Download zip
    Version
    0.2.1
    License
    MIT
    Last updated
    3 months ago
    Downloads
    56

    No dependencies

    #lindividual/der — ASN.1 DER parser for MoonBit

    Pure MoonBit implementation of ITU-T X.690 Distinguished Encoding Rules. Parses cryptographic key formats: PKCS#1 (RSA), PKCS#8 (wrapped private keys).

    #Features

    • Generic TLV parser: read_tlv, skip_tlv, enter_sequence
    • Integer extraction: read_integer_bytes (big-endian, unsigned, strips DER sign byte)
    • PKCS#8 unwrapping: unwrap_pkcs8
    • RSA key parser: parse_rsa_key (PKCS#1 + PKCS#8 auto-detection)
    • Zero dependencies beyond MoonBit stdlib
    • 11 tests, all targets

    #Quick start

    let (n, d) = @der.parse_rsa_key(der_bytes)

    Feed (n, d) directly into RabitLogic/mjwt's RsaSigner::new(n, d, "RS256").

    #License

    MIT

    DerError

    pub suberror DerError {
    Invalid(String)
    }

    Errors for malformed DER input.

    EcPrivateKey

    pub(all) struct EcPrivateKey {
    curve : String
    d : Bytes
    pub_x : Bytes?
    pub_y : Bytes?
    } derive(
    Debug
    )

    PublicKeyInfo

    pub(all) struct PublicKeyInfo {
    algorithm : String
    key : Bytes
    } derive(
    Debug
    )

    Tag

    pub(all) enum Tag {
    Boolean
    Integer
    BitString
    OctetString
    Null
    Oid
    Utf8String
    PrintableString
    UtcTime
    GeneralizedTime
    Sequence
    Set
    } derive(Eq)

    Universal ASN.1 tag classes used in DER.
    impl Show for Tag

    Tlv

    pub(all) struct Tlv {
    tag : Tag
    value : Bytes
    }

    A raw TLV triple from DER data.

    enter_sequence

    fn enter_sequence(data : Bytes, pos : Int) -> (Int, Int) raise DerError

    from_pem

    fn from_pem(pem : String) -> Bytes

    Strip PEM armor and base64-decode.

    parse_ec_key

    fn parse_ec_key(data : Bytes) -> EcPrivateKey raise DerError

    parse_public_key

    fn parse_public_key(data : Bytes) -> PublicKeyInfo raise DerError

    parse_rsa_key

    fn parse_rsa_key(data : Bytes) -> (Bytes, Bytes) raise DerError

    read_integer_bytes

    fn read_integer_bytes(data : Bytes, pos : Int) -> (Bytes, Int) raise DerError

    read_oid

    fn read_oid(data : Bytes, pos : Int) -> (String, Int) raise DerError

    read_tlv

    fn read_tlv(data : Bytes, pos : Int) -> (Tlv, Int) raise DerError

    skip_tlv

    fn skip_tlv(data : Bytes, pos : Int) -> Int raise DerError

    unwrap_pkcs8

    fn unwrap_pkcs8(data : Bytes) -> Bytes raise DerError

    Source Files