What is @peculiar/asn1-rsa?
@peculiar/asn1-rsa is an npm package that provides ASN.1 schema definitions for RSA cryptographic algorithms. It is useful for encoding and decoding RSA keys and signatures in ASN.1 format, which is a standard interface for representing cryptographic data.
What are @peculiar/asn1-rsa's main functionalities?
Encoding RSA Public Key
This code demonstrates how to encode an RSA public key into ASN.1 format using the @peculiar/asn1-rsa package.
const { RSAPublicKey } = require('@peculiar/asn1-rsa');
const { AsnConvert } = require('@peculiar/asn1-schema');
const publicKey = new RSAPublicKey({
modulus: new Uint8Array([0x00, 0x01, 0x02, 0x03]),
publicExponent: new Uint8Array([0x01, 0x00, 0x01])
});
const asn1PublicKey = AsnConvert.serialize(publicKey);
console.log(asn1PublicKey);
Decoding RSA Public Key
This code demonstrates how to decode an ASN.1 encoded RSA public key using the @peculiar/asn1-rsa package.
const { RSAPublicKey } = require('@peculiar/asn1-rsa');
const { AsnConvert } = require('@peculiar/asn1-schema');
const asn1PublicKey = new Uint8Array([/* ASN.1 encoded public key bytes */]);
const publicKey = AsnConvert.parse(asn1PublicKey, RSAPublicKey);
console.log(publicKey);
Encoding RSA Private Key
This code demonstrates how to encode an RSA private key into ASN.1 format using the @peculiar/asn1-rsa package.
const { RSAPrivateKey } = require('@peculiar/asn1-rsa');
const { AsnConvert } = require('@peculiar/asn1-schema');
const privateKey = new RSAPrivateKey({
version: 0,
modulus: new Uint8Array([0x00, 0x01, 0x02, 0x03]),
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
privateExponent: new Uint8Array([0x00, 0x01, 0x02, 0x03]),
prime1: new Uint8Array([0x00, 0x01]),
prime2: new Uint8Array([0x00, 0x01]),
exponent1: new Uint8Array([0x00, 0x01]),
exponent2: new Uint8Array([0x00, 0x01]),
coefficient: new Uint8Array([0x00, 0x01])
});
const asn1PrivateKey = AsnConvert.serialize(privateKey);
console.log(asn1PrivateKey);
Decoding RSA Private Key
This code demonstrates how to decode an ASN.1 encoded RSA private key using the @peculiar/asn1-rsa package.
const { RSAPrivateKey } = require('@peculiar/asn1-rsa');
const { AsnConvert } = require('@peculiar/asn1-schema');
const asn1PrivateKey = new Uint8Array([/* ASN.1 encoded private key bytes */]);
const privateKey = AsnConvert.parse(asn1PrivateKey, RSAPrivateKey);
console.log(privateKey);
Other packages similar to @peculiar/asn1-rsa
node-forge
node-forge is a comprehensive library for implementing various cryptographic algorithms in JavaScript. It includes support for RSA key generation, encryption, and decryption, as well as ASN.1 encoding and decoding. Compared to @peculiar/asn1-rsa, node-forge offers a broader range of cryptographic functionalities beyond just ASN.1 schema definitions.
asn1js
asn1js is a library for encoding and decoding ASN.1 data structures in JavaScript. It provides low-level tools for working with ASN.1, including support for various cryptographic algorithms. While it is more general-purpose compared to @peculiar/asn1-rsa, it can be used to achieve similar results with more manual effort.
pkijs
pkijs is a library built on top of asn1js that provides high-level APIs for working with public key infrastructure (PKI) and cryptographic operations. It includes support for RSA keys and certificates, making it a more comprehensive solution compared to @peculiar/asn1-rsa, which focuses specifically on ASN.1 schema definitions for RSA.