@edirect/encrypt-modules
v11.0.54
Published
Encrypt Modules
Maintainers
Keywords
Readme
@edirect/encrypt-modules
Encryption utilities for eDirect NestJS applications. Provides four NestJS modules for different encryption algorithms: PGP, Binary PGP, AES-256, and RSA — plus a ZIP module for password-protected archive creation.
Features
- PGP encryption/decryption using OpenPGP.js (
openpgp) - Binary PGP for binary data encryption
- AES-256-CBC (and other AES variants) using Node.js native
crypto - RSA encryption/decryption using
node-rsa - ZIP password-protected archive creation
- NestJS module integration for each encryption type
Installation
pnpm add @edirect/encrypt-modules
# or
npm install @edirect/encrypt-modulesAvailable Modules
PGP Encryption (PgpEncryptModule)
Asymmetric encryption using OpenPGP keys:
import { Module } from '@nestjs/common';
import { PgpEncryptModule } from '@edirect/encrypt-modules';
@Module({
imports: [PgpEncryptModule],
})
export class AppModule {}import { Injectable } from '@nestjs/common';
import { PgpEncryptService } from '@edirect/encrypt-modules';
@Injectable()
export class SecureDataService {
constructor(private readonly pgp: PgpEncryptService) {}
async encryptPayload(data: string, publicKey: string): Promise<string> {
return this.pgp.encrypt(publicKey, data);
}
async decryptPayload(
encryptedData: string,
privateKey: string,
passphrase?: string
): Promise<string> {
return this.pgp.decrypt(privateKey, encryptedData, passphrase);
}
}PgpEncryptService API:
| Method | Signature | Description |
| --------- | ------------------------------------------------------------------------------- | ------------------------------------ |
| encrypt | (pgpKey: string, dataToEncrypt: string): Promise<string> | Encrypt data using a PGP public key |
| decrypt | (pgpKey: string, encryptedData: string, passphrase?: string): Promise<string> | Decrypt data using a PGP private key |
AES Encryption (AesEncryptModule)
Symmetric encryption using Node.js crypto:
import { AesEncryptModule } from '@edirect/encrypt-modules';
@Module({ imports: [AesEncryptModule] })
export class AppModule {}import { AesEncryptService } from '@edirect/encrypt-modules';
// Default: AES-256-CBC
const encrypted = aesService.encrypt(
'secret data',
'my-32-char-passphrase-here!!!!!'
);
const decrypted = aesService.decrypt(
encrypted,
'my-32-char-passphrase-here!!!!!'
);
// With custom IV and encoding
const encrypted2 = aesService.encrypt('data', passphrase, {
iv: customIv,
encryptionMethod: 'aes-256-cbc',
outputEncoding: 'base64',
});AesEncryptService API:
| Method | Signature | Description |
| --------- | ----------------------------------------------------------------------------------------------------- | -------------------------- |
| encrypt | (message: string, passphrase: CipherKey, options?: IaesEncryptOptionalParameters): string | Synchronous AES encryption |
| decrypt | (encryptedMessage: string, passphrase: BinaryLike, options?: IaesDecryptOptionalParameters): string | Synchronous AES decryption |
RSA Encryption (RsaEncryptModule)
Asymmetric encryption using node-rsa:
import { RsaEncryptModule } from '@edirect/encrypt-modules';
@Module({ imports: [RsaEncryptModule] })
export class AppModule {}import { RsaEncryptService } from '@edirect/encrypt-modules';
const encrypted = rsaService.encrypt(data, 512, 'utf8');
const decrypted = rsaService.decrypt(encrypted, privateKey, 'utf8');ZIP with Password (ZipEncryptModule)
Create password-protected ZIP archives:
import { ZipEncryptModule } from '@edirect/encrypt-modules';
@Module({ imports: [ZipEncryptModule] })
export class AppModule {}AES Optional Parameters
| Option | Type | Default | Description |
| ------------------ | ------------------ | --------------- | ---------------------------------- |
| iv | BinaryLike | Random | Initialization vector |
| encryptionMethod | string | 'aes-256-cbc' | Cipher algorithm |
| inputEncoding | Encoding | 'utf8' | Input data encoding |
| outputEncoding | Encoding | 'hex' | Output encoding for encrypted data |
| options | TransformOptions | — | Additional stream options |
Security Notes
- Never hardcode encryption keys in source code. Use environment variables.
- For PGP keys, store them in secure secret management (e.g., Kubernetes Secrets, AWS Secrets Manager).
- AES keys must be exactly 32 bytes for AES-256. Use a key derivation function (KDF) if your passphrase is shorter.
