Skip to content

A PEM to JWK tool converts a Privacy Enhanced Mail (PEM) encoded certificate to a JSON Web Key (JWK) format, containing information such as the public key and its algorithms. Useful for using PEM certificates in applications that expect JWK input.

Notifications You must be signed in to change notification settings

sirwebdev/pem2jwk-parser

Repository files navigation

Pem2Jwk

A tool to convert a PEM file to a JSON Web Key (JWK). AThe package provides methods to convert both public and private PEM files, as well as a method to convert a private PEM to a public JWK.

Requirements

Table of Contents


fromPublic

This method takes a public PEM file and converts it to a JSON Web Key (JWK).

Syntax

Pem2Jwk.fromPublic<T>(publicPem: PEM, extraKeys?: T): PublicJWK<T>

Parameters

  • publicPem: The public PEM file. Can be a string or a Buffer.
  • extraKeys (optional): An object that provides additional information to be included in the JWK.

Return Value

The public JWK.

Example

const publicPem = `-----BEGIN PUBLIC KEY-----
...
-----END PUBLIC KEY-----`;
const publicJwk = Pem2Jwk.fromPublic(publicPem);
console.log(publicJwk);

fromPrivate

This method takes a private PEM file and converts it to a JSON Web Key (JWK).

Syntax

Pem2Jwk.fromPrivate<T>(privatePem: PEM, extraKeys?: T): PrivateJWK<T>

Parameters

  • privatePem: The private PEM file. Can be a string or a Buffer.
  • extraKeys (optional): An object that provides additional information to be included in the JWK.

Return Value

The private JWK.

Example

const privatePem = `-----BEGIN RSA PRIVATE KEY-----
...
-----END RSA PRIVATE KEY-----`;

const privateJwk = Pem2Jwk.fromPrivate(privatePem);
console.log(privateJwk);

fromPrivateToPublic

This method converts a private PEM key to a public JWK. The private PEM key is passed as a parameter to the method, and the public JWK is returned.

The method works by first creating a temporary PEM file from the private PEM key and then using openssl to generate a public key from that file. The public key is then converted to a JWK using the fromPublic method.

Syntax

Pem2Jwk.fromPrivateToPublic<T>(privatePem: PEM, extraKeys?: T): Promise<PublicJWK<T>>

Parameters

  • privatePem: The private PEM file. Can be a string or a Buffer.
  • extraKeys (optional): An object that provides additional information to be included in the JWK.

Return Value

A Promise that resolves to the public JWK.

Example

const privatePem = `-----BEGIN RSA PRIVATE KEY-----
...
-----END RSA PRIVATE KEY-----`;

Pem2Jwk.fromPrivateToPublic(privatePem).then(publicJwk => {
  console.log(publicJwk);
});

About

A PEM to JWK tool converts a Privacy Enhanced Mail (PEM) encoded certificate to a JSON Web Key (JWK) format, containing information such as the public key and its algorithms. Useful for using PEM certificates in applications that expect JWK input.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published