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 🙏

© 2024 – Pkg Stats / Ryan Hefner

crypterjs

v1.0.0

Published

A simple and secure library for encrypting and decrypting data using AES-256-GCM with PBKDF2 key derivation. Ideal for securing JWT tokens and sensitive information.

Downloads

20

Readme

Crypterjs

Crypterjs is a simple encryption and decryption service built on top of Node.js' crypto module. It provides an easy-to-use interface for securely encrypting and decrypting data with AES-256-GCM and PBKDF2 key derivation.

⚠️ Important: This library is not intended for password hashing. Use dedicated hashing algorithms (like bcrypt, argon2, etc.) for password hashing, as they are designed for one-way flows. Crypterjs is for two-way encryption and decryption of data such as tokens, strings, or numbers.

Features

  • AES-256-GCM encryption.
  • PBKDF2 key derivation with configurable iterations.
  • Randomized IV (Initialization Vector) and salt for each encryption.
  • Customizable options for encoding, salt length, and iteration count.

Installation

Install Crypterjs via npm:

npm install crypterjs

Usage

You can import Crypterjs in both CommonJS and ESM environments.

CommonJS:

const Crypterjs = require('crypterjs');

ES Modules:

import Crypterjs from 'crypterjs';

Use Cases

1. Encrypting and Decrypting Strings

Here’s an example of how to use Crypterjs for encryption and decryption.

const Crypterjs = require('crypterjs'); // Or use import in ESM
const crypter = new Crypterjs('your-secret-key'); // Create a new instance of Crypterjs
const encryptedValue = crypter.encrypt('Hello, world!'); // Encrypt a value
console.log('Encrypted:', encryptedValue);
const decryptedValue = crypter.decrypt(encryptedValue); // Decrypt the value
console.log('Decrypted:', decryptedValue);

2. Encrypting and Decrypting Numbers

You can also encrypt and decrypt numbers like user IDs, order numbers, or any other numerical data you want to secure:

const Crypterjs = require('crypterjs'); // Or use import in ESM
const crypter = new Crypterjs('your-secret-key'); // Create a new instance of Crypterjs
const encryptedValue = crypter.encrypt(12345); // Encrypt a value
console.log('Encrypted:', encryptedValue);
const decryptedValue = crypter.decrypt(encryptedValue); // Decrypt the value
console.log('Decrypted:', decryptedValue);

3. Encrypting and Decrypting JWT

When dealing with JWT tokens, you often need to encrypt them before sending or storing them for an extra layer of security. You can use Crypterjs to encrypt and decrypt JWT tokens using a secret key (which may or may not be the same key you use for signing the JWT): Important Note: Once the JWT token is encrypted using Crypterjs, the resulting encrypted token cannot be decoded or inspected using the jwt.io website. Therefore, the original payload will not be visible or verifiable without decrypting the token first.

const jwt = require('jsonwebtoken');
const Crypterjs = require('crypterjs');
const secretKey = 'your-jwt-secret-key';
const crypter = new Crypterjs('your-jwt-secret-key'); // Secret key used for both JWT signing and Crypterjs
const token = jwt.sign({ userId: 123 }, secretKey, { expiresIn: '1h' }); // Sign the JWT token
const encryptedToken = crypter.encrypt(token); // Encrypt the JWT token before storing or sending
console.log('Encrypted JWT:', encryptedToken);

const decryptedToken = crypter.decrypt(encryptedToken); // Decrypt the JWT token before verifying
console.log('Decrypted JWT:', decryptedToken);

const verified = jwt.verify(decryptedToken, secretKey); // Verify the decrypted JWT
console.log('Verified JWT Payload:', verified);

Options

You can customize Crypterjs behavior by passing an options object when initializing:

const options = {
  encoding: 'base64',         // Default: 'hex'
  saltLength: 32,             // Default: 64
  pbkdf2Iterations: 50000     // Default: 100000
};
const crypter = new Crypterjs('your-secret-key', options);

API

Constructor

constructor(secret: string, options?: CrypterOptions)

  • secret: The secret key used for encryption and decryption (required). You should treat this secret similarly to how you handle JWT secret keys or other sensitive keys in your application.
  • options (optional): An object to customize encryption:
    • encoding (string): The encoding used for the output string. Default is 'hex'.
    • pbkdf2Iterations (number): Number of iterations for the PBKDF2 key derivation function. Default is 100000.
    • saltLength (number): Length of the random salt in bytes. Default is 64.

Methods

encrypt(value: string | number | null | undefined): string

Encrypts the provided value.

  • value: The value to encrypt (string or number).
  • Returns: The encrypted string.
  • Throws: An error if value is null or undefined.

decrypt(value: string | null | undefined): string

Decrypts the provided encrypted string.

  • value: The encrypted string to decrypt.
  • Returns: The decrypted value as a string.
  • Throws: An error if value is null or undefined.