npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

js-x509-utils

v1.0.7

Published

Universal Module of X.509 Certificate Utilities in JavaScript

Downloads

24,902

Readme

Universal Module of X.509 Certificate Utilities in JavaScript

npm version License: MIT

WARNING: At this time this solution should be considered suitable for research and experimentation, further code and security review is needed before utilization in a production application.

Introduction and Overview

This library is designed to be 'universal' as a utility to handle X.509 in JavaScript, i.e., it works both on most browsers and on Node.js just by importing from npm/source code. This library provides APIs to convert between Json Web Key (JWK) and X.509 DER/PEM.

Installation

At your project directory, do either one of the following.

  • From npm/yarn:
    $ npm install --save js-x509-utils // npm
    $ yarn add js-x509-utils // yarn
  • From GitHub:
    $ git clone https://github.com/junkurihara/jscu.git
    $ cd js-crypto-utils/packages/js-x509-utils
    & yarn build

Then you should import the package as follows.

import x509 from 'js-x509-utils'; // for npm
import x509 from 'path/to/js-x509-utils/dist/index.js'; // for github

The bundled file is also given as js-x509-utils/dist/x509.bundle.js for a use case where the module is imported as a window.x509 object via script tags.

Usage

This library always uses JWK-formatted keys (RFC7517) to do any operations. If you utilize keys of other format, like PEM, please use js-crypto-key-utils to convert them to JWK.

Create X.509 from JWK-formatted public key

ECDSA

const publicJwk = {kty: 'EC', crv: 'P-256', x: '...', y: '...'}; // public key to be signed
const privateJwk = {ktyp: 'EC', crv: 'P-256', x: '...', y: '...', d: '...'}; // private key

const name = { // this is optional
      countryName: 'JP',
      stateOrProvinceName: 'Tokyo',
      localityName: 'Chiyoda',
      organizationName: 'example',
      organizationalUnitName: 'Research',
      commonName: 'example.com'
    };

// sign
x509.fromJwk(
  publicJwk,
  privateJwk,
  'pem',
  {
    signature: 'ecdsa-with-sha256', // signature algorithm
    days: 365, // expired in days
    issuer: name, // issuer
    subject: name // assume that issuer = subject, i.e., self-signed certificate
  },
  'pem' // output signature is in PEM. DER-encoded signature is available with 'der'.
).then( (cert) => {
  // now you get the certificate in PEM string
});

RSA-PSS

const publicJwk = {kty: 'RSA', n: '...', e: '...'}; // public key to be signed
const privateJwk = {ktyp: 'RSA', n: '...', e: '...', d: '...', p: '...', q: '...', ...}; // private key

const name = {
  countryName: 'JP',
  stateOrProvinceName: 'Tokyo',
  localityName: 'Chiyoda',
  organizationName: 'example',
  organizationalUnitName: 'Research',
  commonName: 'example.com'
};

x509.fromJwk(
  publicJwk,
  privateJwk,
  'pem',
  {
    signature: 'rsassaPss',
    days: 365,
    issuer: name,
    subject: name,
    pssParams: {
      saltLength: 32, // if unspecified, 20 will be applied as default value
      hash: 'SHA-256' // if unspecified, 'SHA-1' will be applied as default value (but I do not not recommend SHA-1)
    }
  }
).then( (crt) => {
  // now you get a certificate
});

RSASSA-PKCS1-v1_5

const publicJwk = {kty: 'RSA', n: '...', e: '...'}; // public key to be signed
const privateJwk = {ktyp: 'RSA', n: '...', e: '...', d: '...', p: '...', q: '...', ...}; // private key

const name = {
  countryName: 'JP',
  stateOrProvinceName: 'Tokyo',
  localityName: 'Chiyoda',
  organizationName: 'example',
  organizationalUnitName: 'Research',
  commonName: 'example.com'
};

x509.fromJwk(
  publicJwk,
  privateJwk,
  'pem',
  {
    signature: 'sha256WithRSAEncryption',
    days: 365,
    issuer: name,
    subject: name
  }
).then( (crt) => {
  // now you get a certificate
});

Extract JWK from X.509 certificate

const crtsample = '-----BEGIN CERTIFICATE-----...';
const jwkey = x509.toJwk(crtsample, 'pem');
// now you get JWK public key from PEM-formatted certificate

Extract signature and message body from X.509 certificate

const crtsample = '-----BEGIN CERTIFICATE-----...';
const parsed = x509.parse(crtsample, 'pem');
// now you get parsed object from PEM-formatted certificate
// {tbsCertificate, signatureValue, signatureAlgorithm}

Note

Limitations

Legacy IE is NOT supported at all. Also the followings are not supported in Edge.

  • ECDSA: ecdsa-with-sha1
  • RSASSA-PKCS-v1_5: Sha1WithRsaEncryption
  • RSA-PSS: All

Note that referring to RFC, Sha1WithRsaEncryption is the default algorithm when empty params for RSA-SSA-PKCS-v1_5, and hence MS environment does not support such existing certificates. Hence we strongly recommend to use other modern browsers.

ECDSA

At this point, this library supports only certificate signed with ECDSA using the following curve for elliptic curve cryptography.

  • P-256 (secp256r1)
  • P-384 (secp384r1)
  • P-521 (secp521r1)
  • P-256K (secp256k1)

License

Licensed under the MIT license, see LICENSE file.