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 πŸ™

Β© 2026 – Pkg Stats / Ryan Hefner

@newput-newlink/security

v1.0.4

Published

Encryption Middleware for NestJS πŸš€πŸ”’ Overview This module implements Hybrid Encryption in a NestJS middleware, ensuring that all incoming requests are decrypted and outgoing responses are encrypted using a combination of RSA and AES encryption.

Readme

Encryption Middleware for NestJS πŸš€πŸ”’ Overview This module implements Hybrid Encryption in a NestJS middleware, ensuring that all incoming requests are decrypted and outgoing responses are encrypted using a combination of RSA and AES encryption.

πŸ”Ή Features βœ… Hybrid Encryption (RSA + AES-256-GCM) for speed and security βœ… Seamless Middleware Integration in NestJS βœ… Automatic Encryption & Decryption of request/response payloads βœ… Prevents Recursive Encryption for multiple middleware layers βœ… Tamper-proof Authentication Tag (authTag) for AES encryption

1️⃣ Installation First, install the required dependencies:

sh Copy Edit npm install crypto 2️⃣ How It Works πŸ”Ή Hybrid Encryption (RSA + AES) Flow 1️⃣ Client-Side Encryption

Generates a random AES key & IV (Initialization Vector) Encrypts the data using AES-256-GCM Encrypts the AES key using RSA Public Key Sends { encryptedData, iv, authTag, encryptedAESKey } in the request 2️⃣ Middleware Decryption (Server-Side)

Decrypts the AES key using RSA Private Key Uses the AES key to decrypt encryptedData Ensures integrity using authTag Processes the decrypted request 3️⃣ Server-Side Response Encryption

Generates a new AES key & IV Encrypts the response with AES-256-GCM Encrypts the AES key using RSA Public Key Sends { encryptedData, iv, authTag, encryptedAESKey } back to the client 3️⃣ Middleware Usage πŸ“Œ Step 1: Register Middleware In your app.module.ts, apply the middleware globally:

ts Copy Edit import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'; import { EncryptionMiddleware } from './encryption.middleware';

@Module({}) export class AppModule implements NestModule { configure(consumer: MiddlewareConsumer) { consumer.apply(EncryptionMiddleware).forRoutes('*'); // Apply to all routes } } 4️⃣ Configuration πŸ“Œ Step 2: Provide RSA Keys Your RSA keys must be stored securely. Configure them in an environment file or pass them dynamically:

ts Copy Edit { rsaConfig: { privateKey: process.env.RSA_PRIVATE_KEY, // Keep this secret publicKey: process.env.RSA_PUBLIC_KEY // Used for encryption } } 5️⃣ Encryption & Decryption Logic πŸ“Œ Encrypting Data (AES + RSA) ts Copy Edit encryptData(data: string): { encryptedData: string; iv: string; authTag: string; encryptedAESKey: string } { const aesKey = crypto.randomBytes(32); // AES-256 key const iv = crypto.randomBytes(16); // IV for AES-GCM

const cipher = crypto.createCipheriv('aes-256-gcm', aesKey, iv); let encrypted = cipher.update(data, 'utf8', 'base64'); encrypted += cipher.final('base64');

const encryptedAESKey = crypto.publicEncrypt( { key: this.publicKey, padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, }, aesKey );

return { encryptedData: encrypted, iv: iv.toString('base64'), authTag: cipher.getAuthTag().toString('base64'), encryptedAESKey: encryptedAESKey.toString('base64') }; } πŸ“Œ Decrypting Data (RSA + AES) ts Copy Edit decryptData(encryptedPayload: { encryptedData: string; iv: string; authTag: string; encryptedAESKey: string }): string { const { encryptedData, iv, authTag, encryptedAESKey } = encryptedPayload;

const decryptedAESKey = crypto.privateDecrypt( { key: this.privateKey, padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, }, Buffer.from(encryptedAESKey, 'base64') );

const decipher = crypto.createDecipheriv('aes-256-gcm', decryptedAESKey, Buffer.from(iv, 'base64')); decipher.setAuthTag(Buffer.from(authTag, 'base64'));

let decrypted = decipher.update(encryptedData, 'base64', 'utf8'); decrypted += decipher.final('utf8');

return decrypted; } 6️⃣ Example Request & Response Format πŸ“Œ Encrypted Request (Client to Server) json Copy Edit { "encryptedData": "mYF83Vh+...", "iv": "sdfu8JKo...", "authTag": "0gA6X9ft...", "encryptedAESKey": "L2mp5JTo..." } πŸ“Œ Encrypted Response (Server to Client) json Copy Edit { "encryptedData": "mR4T3Dd+...", "iv": "Kjs73Dpd...", "authTag": "1qX9H2ba...", "encryptedAESKey": "R2nd7LPo..." } 7️⃣ Security Best Practices βœ… πŸ” Keep the RSA private key secure (never expose it in frontend or logs). πŸ”‘ Rotate AES keys periodically to enhance security. ⚠️ Validate decrypted data before processing to avoid attacks. πŸ“Œ Store encryption logs securely to debug potential failures.