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

@hedger/nestjs-encryption

v0.1.5

Published

Encryption Module for NestJS.

Downloads

1,398

Readme

NestJS Encryption Module

NestJS Encrytion is a NestJS 9+ module that provides plug-and-play encryption and decryption functionality to your NestJS application.

  • Uses aes-256-cbc by default, but supports other ciphers as well.
  • Provides a keygen (API and CLI) for generating random and secure encryption keys.
  • Thoroughly tested.

Installation

NestJS Encryption can be installed with your favorite package manager.

# NPM
npm install @hedger/nestjs-encryption

# Yarn
yarn add @hedger/nestjs-encryption

# PNPM
pnpm add @hedger/nestjs-encryption

Setup

Setting up the module inside your NestJS application is a matter of registering the module within your AppModule. The module is registered globally by default and can be used anywhere in your application.

You may use either the forRoot or forRootAsync method to register the module in your AppModule.

Using forRoot

The forRoot method is the simplest way to register the module.

import { EncryptionModule, Cipher } from "@hedger/nestjs-encryption";

@Module({
	imports: [
		EncryptionModule.forRoot({
			key: process.env.APP_KEY,
			cipher: Cipher.AES_256_CBC,
		}),
	],
})
export class AppModule {}

Using forRootAsync

The forRootAsync method allows you to register the module asynchronously, optionally resolving the encryption key from a configuration service. Here's an example that uses the ConfigService from @nestjs/config to resolve the encryption key from the APP_KEY environment variable.

import { ConfigModule, ConfigService } from "@nestjs/config";
import { EncryptionModule, Cipher } from "@hedger/nestjs-encryption";

@Module({
	imports: [
		ConfigModule.forRoot({
			isGlobal: true,
		}),
		EncryptionModule.forRootAsync({
			useFactory: (configService: ConfigService) => ({
				key: configService.get<string>("APP_KEY"),
			}),
			inject: [ConfigService],
		}),
	],
	controllers: [AppController],
	providers: [AppService],
})
export class AppModule {}

Usage

Inject the EncryptionService in your service or controller.

import { EncryptionService } from "@hedger/nestjs-encryption";

@Injectable()
export class FooService {
	constructor(private readonly crypto: EncryptionService) {}

	someMethod() {
		const encrypted = this.crypto.encrypt("some value");
		const decrypted = this.crypto.decrypt(encrypted);
	}
}

Encryption key

This package expects the encryption key to be a base64-encoded string of N random bytes, where N is the key length of the cipher you're using. For example, the aes-256-cbc cipher has a key length of 32 bytes, so the encryption key must be a base64-encoded string of 32 random bytes.

Generating a key

This package provides CLI utility for generating random and secure encryption keys.

# Generates a random key for the aes-256-cbc cipher (default)
npm exec nestjs-encryption-keygen

By default, the keygen generates keys for the aes-256-cbc cipher. You may specify a different cipher by passing the --cipher option.

# Generates a random key for the aes-128-cbc cipher
npm exec nestjs-encryption-keygen --cipher aes-128-cbc

See the Supported ciphers section for a list of supported ciphers.

Generating a key programmatically

Random and secure encryption keys may also be generated programmatically by calling the generateKey method on the EncryptionService class.

import { Cipher, EncryptionService } from "@hedger/nestjs-encryption";

// Pass the desired cipher as the first argument.
const key = EncryptionService.generateKey(Cipher.AES_256_CBC);

Supported ciphers

The following ciphers are supported by this package.

  • aes-256-cbc (default)
  • aes-256-gcm
  • aes-128-cbc
  • aes-128-gcm

License

Copyright © 2023, Nicolas Hedger. Released under the MIT License.