What is jsrsasign?
The jsrsasign npm package is a comprehensive library for cryptographic operations, including RSA/DSA/ECDSA key generation, digital signatures, X.509 certificate handling, and more. It is widely used for implementing security features in JavaScript applications.
What are jsrsasign's main functionalities?
RSA Key Generation
This feature allows you to generate RSA key pairs. The code sample demonstrates generating a 2048-bit RSA key pair using the KEYUTIL.generateKeypair method.
const rsaKeypair = KEYUTIL.generateKeypair('RSA', 2048);
console.log(rsaKeypair);
Digital Signature
This feature allows you to create digital signatures. The code sample shows how to sign a message using an RSA private key and the SHA256withRSA algorithm.
const rsa = new RSAKey();
rsa.readPrivateKeyFromPEMString('-----BEGIN PRIVATE KEY-----...');
const sig = new KJUR.crypto.Signature({alg: 'SHA256withRSA'});
sig.init(rsa);
sig.updateString('message');
const signature = sig.sign();
console.log(signature);
X.509 Certificate Handling
This feature allows you to handle X.509 certificates. The code sample demonstrates reading a PEM-encoded certificate and extracting the subject string.
const pemCert = '-----BEGIN CERTIFICATE-----...';
const x509 = new X509();
x509.readCertPEM(pemCert);
console.log(x509.getSubjectString());
JWT (JSON Web Token) Handling
This feature allows you to create and verify JSON Web Tokens (JWT). The code sample shows how to sign a JWT using the HS256 algorithm and a secret key.
const header = {alg: 'HS256', typ: 'JWT'};
const payload = {sub: '1234567890', name: 'John Doe', iat: 1516239022};
const sHeader = JSON.stringify(header);
const sPayload = JSON.stringify(payload);
const sJWT = KJUR.jws.JWS.sign('HS256', sHeader, sPayload, 'secret');
console.log(sJWT);
Other packages similar to jsrsasign
crypto
The 'crypto' module is a built-in Node.js module that provides cryptographic functionality, including a set of wrappers for OpenSSL's hash, HMAC, cipher, decipher, sign, and verify functions. It is more low-level compared to jsrsasign and is typically used for server-side cryptographic operations.
jsonwebtoken
The 'jsonwebtoken' package is a popular library for creating and verifying JSON Web Tokens (JWT). It is more specialized than jsrsasign, focusing specifically on JWT handling, and is widely used in authentication and authorization scenarios.
node-forge
The 'node-forge' package is a comprehensive library for implementing various cryptographic operations in JavaScript. It provides functionalities for key generation, digital signatures, encryption/decryption, and more. It is similar to jsrsasign in terms of the breadth of cryptographic features it offers.
jsrsasign
jsrsasign TOP | github | Wiki | DOWNLOADS | TUTORIALS | API REFERENCE | Online Tool | DEMO | NODE TOOL | AddOn | DONATE
The 'jsrsasign' (RSA-Sign JavaScript Library) is an opensource free cryptography library supporting RSA/RSAPSS/ECDSA/DSA signing/validation, ASN.1, PKCS#1/5/8 private/public key, X.509 certificate, CRL, OCSP, CMS SignedData, TimeStamp, CAdES JSON Web Signature/Token/Key in pure JavaScript.
Public page is https://kjur.github.io/jsrsasign .
Your bugfix and pull request contribution are always welcomed :)
NOTICE FOR COMMING 11.0.0 RELEASE
The "jsrsasign" library is a long lived JavaScript library from 2010 developed with old JavaScript style and backword compatibility. From coming release 11.0.0, following are planed and suport them gradually:
- Stop to support Internet Explorer.
- Stop to support bower.
- Modern ECMA functions will be introduced such as Promise, let, Array methods or class.
- API document generator will be changed from Jsdoc Toolkit to JSDoc3.
- Module bandler will be used such as browserify or webpack.
- Not to use YUI compressor.
- Unit test framework will be changed from QUnit and mocha to jest.
- W3C Web Crypto API support.
- split into some modules besides jsrsasign have been all in package before 11.0.0.
NEWS
- 2023-Mar-12: 10.7.0 Release. Now supports custom X.509 extension and custom OIDs by new "Add-on" architecture. (See here in detail)
- 2021-Nov-21: 10.5.0 Release. Now supports secp521r1(P-521) ECDSA.
- 2021-Apr-14: Security advisory and update for CVE-2021-30246 RSA signature validation vulnerability published
- 2020-Oct-05: jsrsasign won Google Open Source Peer Bonus Award. Thank you Google.
- 2020-Sep-23: 10.0.0 released for CMS SignedData related class including timestamp and CAdES architecture update
- 2020-Aug-24: 9.1.0 released to new CRL APIs align with certificate
- 2020-Aug-19: 9.0.0 released for major update of certificate and CSR generation and parsing without backward compatibility. Please see migration guide in detail.
- 2020-Aug-02: twitter account @jsrsasign started for announcement. please follow.
HIGHLIGHTS
- Swiss Army Knife style all in one package crypto and PKI library
- available on Node.js and browsers
- Long live open source software from 2010
- very easy API to use
- powerful various format key loader and ASN.1 API
- rich document and samples
- no dependency to other library
- no dependency to W3C Web Cryptography API nor OpenSSL
- no dependency on newer ECMAScirpt function. So old browsers also supported.
- very popular crypto library with 1M+ npm downloads/month
- supports "Add-on" architecture
INSTALL
Node NPM
> npm install jsrsasign jsrsasign-util
Bower
> bower install jsrsasign
Or include in HTML from many CDN sites
> <script src="https://cdnjs.cloudflare.com/ajax/libs/jsrsasign/8.0.20/jsrsasign-all-min.js"></script>
USAGE
Loading encrypted PKCS#5 private key:
> var rs = require('jsrsasign');
> var rsu = require('jsrsasign-util');
> var pem = rsu.readFile('z1.prv.p5e.pem');
> var prvKey = rs.KEYUTIL.getKey(pem, 'passwd');
Sign string 'aaa' with the loaded private key:
> var sig = new a.Signature({alg: 'SHA1withRSA'});
> sig.init(prvKey);
> sig.updateString('aaa');
> var sigVal = sig.sign();
> sigVal
'd764dcacb...'
MORE TUTORIALS AND SAMPLES
RECENT SECURITY ADVISORY
Here is full published security advisory list.
DONATIONS
If you like jsrsasign and my other project, you can support their development by donation through any of the platform/services below. Thank you as always.
You can sponsor jsrsasign with the GitHub Sponsors program.
Cryptocurrency
You can donate cryptocurrency to jsrsasign using the following addresses: