Secure NestJS cryptography module 🔐
MIT License
This library was created to address a common problem encountered when performing cryptographic operations in our projects. It simplifies and streamlines the process, making it easier to implement secure and efficient cryptographic solutions. Additionally, it helps avoid common mistakes, such as the reuse of initialization vectors, reuse of encryption keys, or simple the use of keys that are not cryptographically secure.
Our library employs modern cryptographic standards to provide robust security and protect your data against advanced threats. We utilize a suite of trusted algorithms and practices recognized in the cryptographic community:
This package are available on the npm registry.
yarn add nestjs-cryptography
or
npm install nestjs-cryptography
To access cryptography methods from our CryptographyService
, you could inject it using standard constructor injection
import { Injectable } from '@nestjs/common';
import { CryptographyService } from 'nestjs-cryptography';
@Injectable()
export class SomeService {
constructor(
// Inject using constructor injection
private readonly cryptographyService: CryptographyService
) {}
async someMethod(): Promise<string> {
// Access service methods
return this.cryptographyService.genUUID();
}
}
Once the installation is complete, the CryptographyModule
can be configured as any other
Nest package with forRoot
or forRootAsync
methods.
You could see the complete available settings here
import {
CryptographyModule,
CryptographyOptionsInterface,
} from 'nestjs-cryptography';
@Module({
imports: [
CryptographyModule.forRoot<CryptographyOptionsInterface>({
// The rest of the configuration
encryption: {
symmetric: {
masterKey: '5f7f...46bf'
}
}
}),
],
})
export class AppModule {}
[!TIP] Like other factory providers, our factory function can be async and can inject dependencies through inject. For example, you mat want to get the configuration using the
ConfigurationModule
, so to do this you should use theforRootAsync
method .
import {
CryptographyModule,
CryptographyOptionsInterface,
} from 'nestjs-cryptography';
@Module({
imports: [
CryptographyModule.forRootAsync<CryptographyOptionsInterface>({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
// The rest of the configuration
encryption: {
symmetric: {
masterKey: configService.get<string>('CRYPTOGRAPHY.MASTER_KEY')
}
}
}),
inject: [ConfigService],
}),
],
})
export class AppModule {}
The forRoot()
and forRootAsync
method takes an options object as an argument.
These options are passed through to the underlying cryptographic operations of the instance module.
[!NOTE] Please take a look at the documentation site to see the available methods and the complete configuration