0.3.0 • Published 3 months ago

@voiceflow/encryption v0.3.0

Weekly downloads
-
License
ISC
Repository
-
Last release
3 months ago

encryption

Voiceflow encryption library

Install

yarn add @voiceflow/encryption

Usage

Basic encryption/decryption

import crypto from 'crypto';
import { encryptAes256Gcm, decryptAes256Gcm } from '@voiceflow/encryption';

// create a key
const createAES256Key = () =>
  new Promise<string>((resolve) => {
    crypto.generateKey('aes', { length: 256 }, (err, key) => {
      if (err) throw err;
      resolve(key.export().toString('base64'));
    });
  });
const encryptionKey = await createAES256Key();

// Encrypt data
const plaintext = 'sensitive data';
const encrypted = encryptAes256Gcm(plaintext, encryptionKey);

// Decrypt data
const decrypted = decryptAes256Gcm(encrypted, encryptionKey);
console.log(decrypted); // 'sensitive data'

Using with NestJS

import { Module } from '@nestjs/common';
import { EncryptionModule } from '@voiceflow/encryption';

@Module({
  imports: [
    EncryptionModule.register({
      encryptionKey: 'your-base64-encoded-key',
    }),
  ],
})
export class AppModule {}

Or with async configuration:

import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { EncryptionModule } from '@voiceflow/encryption';

@Module({
  imports: [
    ConfigModule.forRoot(),
    EncryptionModule.registerAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => ({
        encryptionKey: configService.get('ENCRYPTION_KEY'),
      }),
    }),
  ],
})
export class AppModule {}

Then inject and use the service:

import { Injectable } from '@nestjs/common';
import { EncryptionService } from '@voiceflow/encryption';

@Injectable()
export class YourService {
  constructor(private readonly encryptionService: EncryptionService) {}

  encryptSensitiveData(data: string) {
    return this.encryptionService.encrypt(data);
  }

  decryptSensitiveData(encryptedData) {
    return this.encryptionService.decrypt(encryptedData);
  }
}
0.3.0

3 months ago

0.2.0

4 months ago