66<!-- [![npm downloads][npm-downloads-src]][npm-downloads-href] -->
77<!-- [![Codecov][codecov-src]][codecov-href] -->
88
9- # ts-asn1
9+ # ts-prime
1010
11- > A TypeScript implementation of ASN.1 encoding and decoding with a focus on type safety and standards compliance .
11+ > A TypeScript implementation of probabilistic prime number generation with a focus on performance and security .
1212
1313## Features
1414
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_
15+ - 🔢 ** Probabilistic Prime Generation** _ Generate probable prime numbers of any bit length_
16+ - 🧮 ** Miller-Rabin Testing** _ Uses the Miller-Rabin primality test for efficient prime verification_
17+ - 🧵 ** Web Worker Support** _ Optional multi-threaded prime generation for improved performance_
18+ - ⏱️ ** Non-Blocking Operation** _ Configurable time slicing to prevent UI blocking_
19+ - 🔄 ** Customizable PRNG** _ Support for custom cryptographically secure random number generators_
1920- 🛡️ ** Type Safety** _ Full TypeScript support with comprehensive type definitions_
20- - 🪶 ** Lightweight ** _ No dependencies _
21- - 🔍 ** Debugging ** _ Pretty printing of ASN.1 structures for easier debugging _
21+ - 🔍 ** Configurable Testing ** _ Adjustable number of primality tests based on security requirements _
22+ - 🚀 ** Performance Optimized ** _ Efficient algorithms for generating large prime numbers _
2223
2324## Install
2425
2526``` bash
2627# bun
27- bun install ts-asn1
28+ bun install ts-prime
2829
2930# npm
30- npm install ts-asn1
31+ npm install ts-prime
3132
3233# pnpm
33- pnpm install ts-asn1
34+ pnpm install ts-prime
3435```
3536
3637## Get Started
3738
38- After installing the package, you can import and use the ASN.1 encoding and decoding functions:
39+ After installing the package, you can import and use the prime number generation functions:
3940
4041``` 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"
42+ import { prime } from ' ts-prime'
43+
44+ // Generate a 1024-bit probable prime
45+ prime .generateProbablePrime (1024 , {}, (err , num ) => {
46+ if (err ) {
47+ console .error (' Error generating prime:' , err )
48+ return
49+ }
50+
51+ console .log (' Generated prime:' , num .toString ())
52+ console .log (' Bit length:' , num .bitLength ())
53+ console .log (' Is probably prime:' , num .isProbablePrime (10 ))
54+ })
55+
56+ // With custom options
57+ const options = {
58+ algorithm: ' PRIMEINC' ,
59+ maxBlockTime: 10 , // ms to allow blocking before yielding
60+ millerRabinTests: 15 , // number of primality tests
61+ workers: 2 , // number of web workers to use (if supported)
62+ workLoad: 100 // work units per worker
63+ }
64+
65+ prime .generateProbablePrime (2048 , options , (err , num ) => {
66+ if (err ) {
67+ console .error (' Error generating prime:' , err )
68+ return
69+ }
70+
71+ console .log (' Generated 2048-bit prime:' , num .toString ())
72+ })
73+
74+ // With custom PRNG
75+ const customPRNG = {
76+ getBytesSync : (length ) => {
77+ // Your secure random byte generation logic here
78+ // Must return a string of length 'length'
79+ return secureRandomString (length )
80+ }
81+ }
82+
83+ prime .generateProbablePrime (512 , { prng: customPRNG }, (err , num ) => {
84+ if (err ) {
85+ console .error (' Error generating prime:' , err )
86+ return
87+ }
88+
89+ console .log (' Generated prime with custom PRNG:' , num .toString ())
90+ })
6791```
6892
6993## API Reference
7094
71- ### ASN.1 Types
72-
73- The library supports all standard ASN.1 types:
95+ ### Prime Generation
7496
7597``` 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
98+ function generateProbablePrime(
99+ bits : number ,
100+ options : PrimeOptions ,
101+ callback : (err : Error | null , num ? : BigInteger ) => void
102+ ): void
106103```
107104
108- ### ASN.1 Tag Classes
105+ Generates a random probable prime with the specified number of bits .
109106
110- ``` ts
111- const Class = {
112- UNIVERSAL: 0 ,
113- APPLICATION: 1 ,
114- CONTEXT_SPECIFIC: 2 ,
115- PRIVATE: 3
116- } as const
117- ```
107+ #### Parameters
118108
119- ### Key Functions
109+ - ` bits ` : The number of bits for the prime number .
110+ - ` options ` : Configuration options for prime generation .
111+ - ` callback ` : Function called with the generated prime or an error .
120112
121- #### Encoding/Decoding
113+ #### PrimeOptions
122114
123115` ` ` 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
116+ interface PrimeOptions {
117+ algorithm?: string | { name: string, options?: any }
118+ prng?: {
119+ getBytesSync: (length: number) => string
120+ }
121+ maxBlockTime?: number
122+ millerRabinTests?: number
123+ workers?: number
124+ workLoad?: number
125+ workerScript?: string
126+ }
141127` ` `
142128
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- ` ` `
129+ - ` algorithm ` : The algorithm to use (default : ' PRIMEINC' ).
130+ - ` prng ` : A custom crypto - secure pseudo - random number generator .
131+ - ` maxBlockTime ` : Maximum time (ms ) to block the main thread (default : 10ms ).
132+ - ` millerRabinTests ` : Number of Miller - Rabin tests to perform .
133+ - ` workers ` : Number of web workers to use (- 1 for CPU cores - 1 ).
134+ - ` workLoad ` : Number of potential primes for each worker to check .
135+ - ` workerScript ` : Path to the worker script .
136+
137+ ### Miller - Rabin Tests
138+
139+ The number of Miller - Rabin tests is automatically determined based on the bit size to achieve an error probability of (1 / 2 )^ 80 :
140+
141+ | Bit Size | Tests |
142+ | ---------- | ------ - |
143+ | ≤ 100 | 27 |
144+ | ≤ 150 | 18 |
145+ | ≤ 200 | 15 |
146+ | ≤ 250 | 12 |
147+ | ≤ 300 | 9 |
148+ | ≤ 350 | 8 |
149+ | ≤ 400 | 7 |
150+ | ≤ 500 | 6 |
151+ | ≤ 600 | 5 |
152+ | ≤ 800 | 4 |
153+ | ≤ 1250 | 3 |
154+ | > 1250 | 2 |
158155
159156## Testing
160157
@@ -182,7 +179,7 @@ For casual chit-chat with others using this package:
182179
183180## Postcardware
184181
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 .
182+ " Software that is free, but hopes for a postcard." We love receiving postcards from around the world showing where ` ts-prime ` is being used ! We showcase them on our website too .
186183
187184Our address : Stacks .js , 12665 Village Ln #2306 , Playa Vista , CA 90094 , United States 🌎
188185
@@ -207,10 +204,10 @@ The MIT License (MIT). Please see [LICENSE](https://github.com/stacksjs/stacks/t
207204Made with 💙
208205
209206< ! -- 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
207+ [npm - version - src ]: https :// img.shields.io/npm/v/@stacksjs/ts-prime ?style=flat-square
208+ [npm - version - href ]: https :// npmjs.com/package/@stacksjs/ts-prime
212209[github - actions - src ]: https :// img.shields.io/github/actions/workflow/status/stacksjs/ts-security/ci.yml?style=flat-square&branch=main
213210[github - actions - href ]: https :// github.com/stacksjs/ts-security/actions?query=workflow%3Aci
214211
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 -->
212+ < ! -- [codecov - src ]: https :// img.shields.io/codecov/c/gh/stacksjs/ts-prime /main?style=flat-square
213+ [codecov - href ]: https :// codecov.io/gh/stacksjs/ts-prime -->
0 commit comments