Skip to content

csendin/rsa-jwt-keys

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

RSA JWT Key Pair Generator

A Node.js script for generating RSA key pairs asynchronously and demonstrating their use with JSON Web Tokens (JWT). This script enables secure token generation and verification by leveraging RSA public/private key cryptography.

Features

  • Generates RSA key pairs with configurable key sizes.
  • Saves keys in PEM format (.pem) for cryptographic protocols.
  • Encodes keys in Base64 and saves them as .txt files.
  • Demonstrates integration with JWT for secure token signing and verification.
  • Includes error handling for reliable execution.

Prerequisites

  • Node.js: Ensure you have Node.js installed. You can download it from nodejs.org.

Usage

Generating RSA Keys

  1. Run the script to generate RSA keys:

    node key-jwt.js
  2. Upon execution, the following files will be generated in the project root:

    • public-key.pem: Public key in PEM format.
    • private-key.pem: Private key in PEM format.
    • public-key-base64.txt: Public key encoded in Base64.
    • private-key-base64.txt: Private key encoded in Base64.

Using RSA Keys with JWT

The RSA key pairs generated by this script are ideal for signing and verifying JSON Web Tokens (JWT). Here's a brief explanation:

  • Signing JWTs: Use the private key to sign JWTs. This ensures that only the holder of the private key can create valid tokens.
  • Verifying JWTs: Use the public key to verify the authenticity of the JWT. This ensures that the token was signed by the corresponding private key and has not been tampered with.

Customizing Key Size

You can modify the keySize variable in the key-jwt.js file to change the RSA key size. The default size is 2048 bits.

const keySize = 2048; // Change this value to your desired key size

Error Handling

The script includes error handling to manage issues during key generation or file writing. Check the console output for error messages if something goes wrong.

Contributions

Contributions are welcome! Feel free to submit issues or pull requests to enhance the functionality of this script.

About

RSA keys for JWT authentication.

Topics

Resources

Stars

Watchers

Forks