|
| 1 | +<p align="center"><img src="../../.github/art/cover.jpg" alt="Social Card of this repo"></p> |
| 2 | + |
| 3 | +[![npm version][npm-version-src]][npm-version-href] |
| 4 | +[![GitHub Actions][github-actions-src]][github-actions-href] |
| 5 | +[](http://commitizen.github.io/cz-cli/) |
| 6 | +<!-- [![npm downloads][npm-downloads-src]][npm-downloads-href] --> |
| 7 | +<!-- [![Codecov][codecov-src]][codecov-href] --> |
| 8 | + |
| 9 | +# ts-aes |
| 10 | + |
| 11 | +> A TypeScript implementation of AES encryption and decryption with a focus on type safety and standards compliance. |
| 12 | +
|
| 13 | +## Features |
| 14 | + |
| 15 | +- 🔒 **DER Compliant** _Implements ASN.1 encoding & decoding according to standard_ |
| 16 | +- 🔄 **Comprehensive Type Support** _Support for INTEGER, BIT STRING, OCTET STRING, NULL, OBJECT IDENTIFIER, SEQUENCE, SET, and more_ |
| 17 | +- 🧩 **Validation** _Validate ASN.1 structures against expected schemas_ |
| 18 | +- 🔧 **Flexible Parsing** _Support for both strict DER and more lenient BER parsing_ |
| 19 | +- 🛡️ **Type Safety** _Full TypeScript support with comprehensive type definitions_ |
| 20 | +- 🪶 **Lightweight** _No dependencies_ |
| 21 | +- 🔍 **Debugging** _Pretty printing of ASN.1 structures for easier debugging_ |
| 22 | + |
| 23 | +## Install |
| 24 | + |
| 25 | +```bash |
| 26 | +# bun |
| 27 | +bun install ts-asn1 |
| 28 | + |
| 29 | +# npm |
| 30 | +npm install ts-asn1 |
| 31 | + |
| 32 | +# pnpm |
| 33 | +pnpm install ts-asn1 |
| 34 | +``` |
| 35 | + |
| 36 | +## Get Started |
| 37 | + |
| 38 | +After installing the package, you can import and use the ASN.1 encoding and decoding functions: |
| 39 | + |
| 40 | +```ts |
| 41 | +import { asn1 } from 'ts-asn1' |
| 42 | +import { utils } from 'ts-security-utils' |
| 43 | + |
| 44 | +// Create an ASN.1 INTEGER |
| 45 | +const intValue = asn1.integerToDer(123) |
| 46 | +console.log(utils.bytesToHex(intValue)) // "7b" |
| 47 | + |
| 48 | +// Parse ASN.1 DER encoded data |
| 49 | +const derData = utils.hexToBytes('300a02010102010202010304010a') |
| 50 | +const asn1Object = asn1.fromDer(derData) |
| 51 | +console.log(asn1.prettyPrint(asn1Object)) |
| 52 | +// SEQUENCE { |
| 53 | +// INTEGER 1 |
| 54 | +// INTEGER 2 |
| 55 | +// INTEGER 3 |
| 56 | +// OCTET STRING 0a |
| 57 | +// } |
| 58 | + |
| 59 | +// Convert dates to/from ASN.1 generalized time |
| 60 | +const date = new Date('2025-03-01T12:00:00Z') |
| 61 | +const genTime = asn1.dateToGeneralizedTime(date) |
| 62 | +console.log(genTime) // "20250301120000Z" |
| 63 | + |
| 64 | +// Convert back to a date |
| 65 | +const parsedDate = asn1.generalizedTimeToDate(genTime) |
| 66 | +console.log(parsedDate.toISOString()) // "2025-03-01T12:00:00.000Z" |
| 67 | +``` |
| 68 | + |
| 69 | +## API Reference |
| 70 | + |
| 71 | +### ASN.1 Types |
| 72 | + |
| 73 | +The library supports all standard ASN.1 types: |
| 74 | + |
| 75 | +```ts |
| 76 | +const Type = { |
| 77 | + BOOLEAN: 1, |
| 78 | + INTEGER: 2, |
| 79 | + BITSTRING: 3, |
| 80 | + OCTETSTRING: 4, |
| 81 | + NULL: 5, |
| 82 | + OID: 6, |
| 83 | + OBJECT_DESCRIPTOR: 7, |
| 84 | + EXTERNAL: 8, |
| 85 | + REAL: 9, |
| 86 | + ENUMERATED: 10, |
| 87 | + EMBEDDED_PDV: 11, |
| 88 | + UTF8: 12, |
| 89 | + RELATIVE_OID: 13, |
| 90 | + SEQUENCE: 16, |
| 91 | + SET: 17, |
| 92 | + NUMERIC_STRING: 18, |
| 93 | + PRINTABLE_STRING: 19, |
| 94 | + T61_STRING: 20, |
| 95 | + VIDEOTEX_STRING: 21, |
| 96 | + IA5_STRING: 22, |
| 97 | + UTC_TIME: 23, |
| 98 | + GENERALIZED_TIME: 24, |
| 99 | + GRAPHIC_STRING: 25, |
| 100 | + VISIBLE_STRING: 26, |
| 101 | + GENERAL_STRING: 27, |
| 102 | + UNIVERSAL_STRING: 28, |
| 103 | + CHARACTER_STRING: 29, |
| 104 | + BMP_STRING: 30 |
| 105 | +} as const |
| 106 | +``` |
| 107 | + |
| 108 | +### ASN.1 Tag Classes |
| 109 | + |
| 110 | +```ts |
| 111 | +const Class = { |
| 112 | + UNIVERSAL: 0, |
| 113 | + APPLICATION: 1, |
| 114 | + CONTEXT_SPECIFIC: 2, |
| 115 | + PRIVATE: 3 |
| 116 | +} as const |
| 117 | +``` |
| 118 | + |
| 119 | +### Key Functions |
| 120 | + |
| 121 | +#### Encoding/Decoding |
| 122 | + |
| 123 | +```ts |
| 124 | +// Convert to/from DER encoding |
| 125 | +function fromDer(bytes: Uint8Array, options?: FromDerOptions): Asn1Object |
| 126 | +function toDer(obj: Asn1Object): Buffer |
| 127 | + |
| 128 | +// Convert integers to/from DER |
| 129 | +function integerToDer(n: number): Buffer |
| 130 | +function derToInteger(bytes: Uint8Array): number |
| 131 | + |
| 132 | +// Convert OIDs to/from DER |
| 133 | +function oidToDer(oid: string): Buffer |
| 134 | +function derToOid(bytes: Uint8Array): string |
| 135 | + |
| 136 | +// Date conversions |
| 137 | +function dateToGeneralizedTime(date: Date): string |
| 138 | +function generalizedTimeToDate(genTime: string): Date |
| 139 | +function dateToUtcTime(date: Date): string |
| 140 | +function utcTimeToDate(utcTime: string): Date |
| 141 | +``` |
| 142 | + |
| 143 | +#### Utility Functions |
| 144 | + |
| 145 | +```ts |
| 146 | +// Create a copy of an ASN.1 object |
| 147 | +function copy(obj: Asn1Object): Asn1Object |
| 148 | +
|
| 149 | +// Compare two ASN.1 objects for equality |
| 150 | +function equals(obj1: any, obj2: any): boolean |
| 151 | +
|
| 152 | +// Validate an ASN.1 object against a schema |
| 153 | +function validate(obj: Asn1Object, validator: Validator, capture?: Record<string, any>, errors?: string[]): boolean |
| 154 | +
|
| 155 | +// Pretty print an ASN.1 object for debugging |
| 156 | +function prettyPrint(obj: Asn1Object, indent?: string): string |
| 157 | +``` |
| 158 | + |
| 159 | +## Testing |
| 160 | + |
| 161 | +```bash |
| 162 | +bun test |
| 163 | +``` |
| 164 | + |
| 165 | +## Changelog |
| 166 | + |
| 167 | +Please see our [releases](https://github.com/stacksjs/ts-security/releases) page for more information on what has changed recently. |
| 168 | + |
| 169 | +## Contributing |
| 170 | + |
| 171 | +Please review the [Contributing Guide](https://github.com/stacksjs/contributing) for details. |
| 172 | + |
| 173 | +## Community |
| 174 | + |
| 175 | +For help, discussion about best practices, or any other conversation that would benefit from being searchable: |
| 176 | + |
| 177 | +[Discussions on GitHub](https://github.com/stacksjs/stacks/discussions) |
| 178 | + |
| 179 | +For casual chit-chat with others using this package: |
| 180 | + |
| 181 | +[Join the Stacks Discord Server](https://discord.gg/stacksjs) |
| 182 | + |
| 183 | +## Postcardware |
| 184 | + |
| 185 | +"Software that is free, but hopes for a postcard." We love receiving postcards from around the world showing where `ts-asn1` is being used! We showcase them on our website too. |
| 186 | + |
| 187 | +Our address: Stacks.js, 12665 Village Ln #2306, Playa Vista, CA 90094, United States 🌎 |
| 188 | + |
| 189 | +## Sponsors |
| 190 | + |
| 191 | +We would like to extend our thanks to the following sponsors for funding Stacks development. If you are interested in becoming a sponsor, please reach out to us. |
| 192 | + |
| 193 | +- [JetBrains](https://www.jetbrains.com/) |
| 194 | +- [The Solana Foundation](https://solana.com/) |
| 195 | + |
| 196 | +## Credits |
| 197 | + |
| 198 | +- [Dave Longley](https://github.com/dlongley) |
| 199 | +- [node-forge](https://github.com/digitalbazaar/forge) |
| 200 | +- [Chris Breuer](https://github.com/chrisbbreuer) |
| 201 | +- [All Contributors](../../contributors) |
| 202 | + |
| 203 | +## License |
| 204 | + |
| 205 | +The MIT License (MIT). Please see [LICENSE](https://github.com/stacksjs/stacks/tree/main/LICENSE.md) for more information. |
| 206 | + |
| 207 | +Made with 💙 |
| 208 | + |
| 209 | +<!-- Badges --> |
| 210 | +[npm-version-src]: https://img.shields.io/npm/v/@stacksjs/ts-asn1?style=flat-square |
| 211 | +[npm-version-href]: https://npmjs.com/package/@stacksjs/ts-asn1 |
| 212 | +[github-actions-src]: https://img.shields.io/github/actions/workflow/status/stacksjs/ts-security/ci.yml?style=flat-square&branch=main |
| 213 | +[github-actions-href]: https://github.com/stacksjs/ts-security/actions?query=workflow%3Aci |
| 214 | + |
| 215 | +<!-- [codecov-src]: https://img.shields.io/codecov/c/gh/stacksjs/ts-asn1/main?style=flat-square |
| 216 | +[codecov-href]: https://codecov.io/gh/stacksjs/ts-asn1 --> |
0 commit comments