Skip to content

adrianlivr/nest-pbkdf2

Repository files navigation

Nest Logo

The PBKDF2 Nest module for key derivation

Configure

app.module.ts

...
@Module({
	...
	imports: [
		...
		Pbkdf2Module.forRoot({
			// Default values
			hashBytes: 32, /* optional */
			saltBytes: 16, /* optional */
			digest: 'sha512', /* optional */
			iterations: 65535, /* optional */
			encoding: 'hex', /* optional */
		}),
		...
		// For asynchronous configuration
		...
		Pbkdf2Module.forRootAsync({
			import: [ConfigModule],
			useFactory: (config: ConfigService) => ({
				hashBytes: config.get('HASH_BYTES')
			})
			inject: [ConfigService]
		}),
		...
		// OR
		AesGcmModule.forRootAsync({
			useClass: Pbkdf2ConfigClass
		}),
		...
	],
	...
})
...

your.service.ts

...
@Injectable()
export class YourService{
	constructor(private readonly pbkdf2Service: Pbkdf2Service){
	}

	async hash(text: string): Promise<string> {
		return await this.pbkdf2Service.hash(text);
	}

	hashSync(text: string): string {
    return this.pbkdf2Service.hash(text);
	}

  async compare(password: string, hash: string): Promise<boolean> {
    return await this.pbkdf2Service.compare(password, hash);
  }

	compareSync(password: string, hash: string): boolean {
		return this.pbkdf2Service.compare(password, hash);
	}
}
...
If you want hash and compare passwords with security, use argon2 with the 2id version

Use Asynchronous methods for better performance

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages