{"id":27401260,"url":"https://github.com/edjcase/motoko_asn1","last_synced_at":"2026-01-28T06:41:28.864Z","repository":{"id":286026130,"uuid":"960094622","full_name":"edjCase/motoko_asn1","owner":"edjCase","description":"A Motoko library for encoding and decoding ASN.1/DER values","archived":false,"fork":false,"pushed_at":"2025-04-11T17:55:17.000Z","size":26,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-11T18:48:35.354Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Motoko","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/edjCase.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-04-03T21:10:27.000Z","updated_at":"2025-04-11T17:55:21.000Z","dependencies_parsed_at":"2025-04-03T23:35:34.720Z","dependency_job_id":null,"html_url":"https://github.com/edjCase/motoko_asn1","commit_stats":null,"previous_names":["edjcase/motoko_asn1"],"tags_count":0,"template":false,"template_full_name":"edjCase/motoko-library-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edjCase%2Fmotoko_asn1","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edjCase%2Fmotoko_asn1/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edjCase%2Fmotoko_asn1/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edjCase%2Fmotoko_asn1/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/edjCase","download_url":"https://codeload.github.com/edjCase/motoko_asn1/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248819370,"owners_count":21166474,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-04-14T03:54:33.684Z","updated_at":"2026-01-28T06:41:28.858Z","avatar_url":"https://github.com/edjCase.png","language":"Motoko","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Motoko ASN.1\n\n[![MOPS](https://img.shields.io/badge/MOPS-asn1-blue)](https://mops.one/asn1)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/yourusername/motoko_asn1/blob/main/LICENSE)\n\nA Motoko implementation of ASN.1 (Abstract Syntax Notation One) encoding and decoding.\n\n## ⚠️ Partial Implementation\n\nThis library implements the core ASN.1 DER encoding and decoding functionality, but is missing several ASN.1 types:\n\n**Missing Types (by usage frequency):**\n\n1. ENUMERATED - Used in protocols like SNMP, LDAP\n2. BMPString - Common in certificates and internationalized applications\n3. NumericString - Used in telephony and financial protocols\n4. VisibleString/ISO646String - Legacy systems and restricted environments\n5. TeletexString/T61String - Older X.509 certificates\n6. REAL - Scientific and technical applications\n7. DATE, DATE-TIME - Modern alternatives to UTCTime/GeneralizedTime\n8. RELATIVE-OID - X.500 directory services\n9. Other less common types (EXTERNAL, GraphicString, UniversalString, etc.)\n\n## Package\n\n### MOPS\n\n```bash\nmops add asn1\n```\n\nTo set up MOPS package manager, follow the instructions from the [MOPS Site](https://mops.one)\n\n## Quick Start\n\n### Example 1: Encoding ASN.1\n\n```motoko\nimport ASN1 \"mo:asn1\";\n\n// Create an ASN.1 SEQUENCE value\nlet sequence = #sequence([\n    #integer(123),\n    #utf8String(\"Hello ASN.1\"),\n    #boolean(true)\n]);\n\n// Encode to DER format\nlet bytes : [Nat8] = ASN1.toBytes(sequence, #der);\n...\n```\n\nOR use a buffer\n\n```motoko\nimport ASN1 \"mo:asn1\";\nimport Buffer \"mo:buffer\";\n\n// Create an ASN.1 SEQUENCE value\nlet sequence = #sequence([\n    #integer(123),\n    #utf8String(\"Hello ASN.1\"),\n    #boolean(true)\n]);\nlet list = List.empty\u003cNat8\u003e();\n\n// Encode to DER format\nASN1.toBytesBuffer(Buffer.fromList(list), sequence, #der);\n// 'list' now contains DER bytes\n...\n```\n\n### Example 2: Decoding to ASN.1\n\n```motoko\nimport ASN1 \"mo:asn1\";\nimport Result \"mo:core/Result\";\nimport Debug \"mo:core/Debug\";\n\n// Assuming 'derBytes' contains DER-encoded data\nlet derBytes : [Nat8] = [...];\n\n// Decode from DER\nlet value : ASN1.ASN1Value = switch (ASN1.fromBytes(derBytes.vals(), #der)) {\n    case (#err(msg)) return #err(msg);\n    case (#ok(value)) value;\n};\n```\n\n### Example 3: Pretty-Printing ASN.1 Values\n\n```motoko\nimport ASN1 \"mo:asn1\";\nimport Debug \"mo:core/Debug\";\n\n// Create an ASN.1 structure\nlet certificate = #sequence([\n    #objectIdentifier([1, 2, 840, 113549, 1, 1, 11]), // sha256WithRSAEncryption\n    #utf8String(\"Example Certificate\"),\n    #contextSpecific({\n        tagNumber = 0;\n        constructed = true;\n        value = ?#sequence([\n            #utctime(\"220101000000Z\"),\n            #utctime(\"230101000000Z\")\n        ])\n    })\n]);\n\n// Print a human-readable representation\nlet prettyText = ASN1.toText(certificate);\nDebug.print(prettyText);\n// Output:\n// SEQUENCE {\n//   OBJECT IDENTIFIER: 1.2.840.113549.1.1.11\n//   UTF8String: Example Certificate\n//   [0] CONSTRUCTED {\n//     SEQUENCE {\n//       UTCTime: 220101000000Z\n//       UTCTime: 230101000000Z\n//     }\n//   }\n// }\n```\n\n## API Reference\n\n### Types\n\n```motoko\npublic type TagClass = {\n    #universal;\n    #application;\n    #contextSpecific;\n    #private_;\n};\n\npublic type ASN1Value = {\n    #boolean : Bool;\n    #integer : Int;\n    #bitString : BitString;\n    #octetString : [Nat8];\n    #null_;\n    #objectIdentifier : [Nat];\n    #utf8String : Text;\n    #printableString : Text;\n    #ia5String : Text;\n    #utctime : Text;\n    #generalizedTime : Text;\n    #sequence : [ASN1Value];\n    #set : [ASN1Value];\n    // Context-specific types\n    #contextSpecific : ContextSpecificASN1Value;\n    // Unknown types - store raw data\n    #unknown : UnknownASN1Value;\n};\n\npublic type BitString = {\n    data : [Nat8];\n    // Number of unused bits in the last byte (0-7)\n    unusedBits : Nat8;\n};\n\npublic type ContextSpecificASN1Value = {\n    tagNumber : Nat;\n    constructed : Bool;\n    value : ?ASN1Value;\n};\n\npublic type UnknownASN1Value = {\n    tagClass : TagClass;\n    tagNumber : Nat;\n    constructed : Bool;\n    data : [Nat8];\n};\n\npublic type Encoding = {\n    #der;\n};\n```\n\n### Functions\n\n```motoko\n// Main encoding/decoding functions\npublic func fromBytes(bytes : [Nat8], encoding : Encoding) : Result.Result\u003cASN1Value, Text\u003e;\n\npublic func toBytes(value : ASN1Value, encoding : Encoding) : [Nat8];\n\npublic func toBytesToBuffer(buffer : Buffer.Buffer\u003cNat\u003e, value : ASN1Value, encoding : Encoding);\n\n// Utility function for pretty-printing\npublic func toText(value : ASN1Value) : Text;\n```\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedjcase%2Fmotoko_asn1","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fedjcase%2Fmotoko_asn1","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedjcase%2Fmotoko_asn1/lists"}