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

jwet

v1.0.4

Published

An npm package to create json web token with encrypted payload

Downloads

334

Readme

JWET (JWT with Encrypted Token)

A specialized JWT implementation that provides optional payload encryption before token generation. JWET enhances standard JWT security by allowing payload encryption before the token is created, making it ideal for sensitive data transmission.

What makes JWET different?

Unlike standard JWT where the payload is only base64url encoded and can be easily decoded, JWET provides an option to encrypt the payload using HMAC-SHA256 before token generation. This adds an extra layer of security for sensitive data.

Installation

npm install jwet

Features

  • ✨ Optional payload encryption using HMAC-SHA256
  • 🔒 Standard JWT token creation and verification
  • 🛡️ Custom base64url encoding implementation
  • 🚀 Zero external dependencies
  • 💪 Built on Node.js crypto module

Usage

Import the module

const { createToken, verifyToken } = require('jwet');

Create Tokens

Standard JWT Token (Without Payload Encryption)

const secretKey = 'your-secret-key';
const payload = {
  userId: "123",
  role: "admin",
  exp: Date.now() + 3600000
};

const token = createToken(payload, secretKey);
// Result: header.payload.signature

Encrypted Payload Token (JWET)

const secretKey = 'your-secret-key';
const payload = {
  userId: "123",
  role: "admin",
  exp: Date.now() + 3600000
};

const encryptedToken = createToken(payload, secretKey, true);
// Result: header.encryptedPayload.signature

Verify Tokens

const isValid = verifyToken(token, secretKey);
if (isValid) {
    console.log('Token is valid');
} else {
    console.log('Token verification failed');
}

API Reference

createToken(payload, secretKey[, toEncrypt])

Creates a token with optional payload encryption.

Parameters:

  • payload (Object): The data to be encoded in the token
  • secretKey (String): The secret key used for signing and encryption
  • toEncrypt (Boolean, optional): When true, encrypts the payload before token creation. Defaults to false

Returns:

  • (String): The generated token

verifyToken(token, secretKey)

Verifies token authenticity.

Parameters:

  • token (String): The token to verify
  • secretKey (String): The secret key used during token creation

Returns:

  • (Boolean): Token validity status

Token Structure

Standard Mode (toEncrypt = false)

header.payload.signature
  • Header: Contains algorithm (HS256) and token type
  • Payload: Base64url encoded JSON data
  • Signature: HMAC-SHA256 hash of header and payload

Encrypted Mode (toEncrypt = true)

header.encryptedPayload.signature
  • Header: Contains algorithm (HS256) and token type
  • EncryptedPayload: HMAC-SHA256 encrypted and base64url encoded data
  • Signature: HMAC-SHA2