Compact and fast crypto library for hashing and comparing hashes.
npm i @seishinverse/crypto
# or with yarn
yarn add @seishinverse/crypto
# or with pnpm
pnpm i @seishinverse/crypto
In first import CryptoModule
in AppModule
.
import { CryptoModule } from '@seishinverse/crypto';
import { Module } from '@nestjs/common';
@Module({
imports: [CryptoModule.forRoot()],
})
export class AppModule {}
Then inject service with decorator InjectCrypto
.
import { CryptoModule, InjectCrypto, CryptoService } from '@seishinverse/crypto';
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
constructor(@InjectCrypto() private cryptoService: CryptoService) {}
/*
Generating salt and hashing password.
*/
#() {
const SALT_ROUNDS = 5;
const hashSalt = this.cryptoService.genSalt(SALT_ROUNDS);
const hashedPassword = this.cryptoService.hash('very secret pass!', hashSalt);
// ...
}
/*
Comparing current password with incoming.
*/
signIn() {
const currentPassword = 'very secret pass!';
const incomingPassword = 'not secret pass:(';
const isPasswordEqual = this.cryptoService.compare(incomingPassword, currentPassword);
console.log(isPasswordEqual); // false
}
}
Import CryptoService
and initialize it.
import { CryptoService } from '@seishinverse/crypto';
const cryptoService = new CryptoService();
/*
Generating salt and hashing password.
*/
const SALT_ROUNDS = 5;
const hashSalt = this.cryptoService.genSalt(SALT_ROUNDS);
const hashedPassword = this.cryptoService.hash('very secret pass!', hashSalt);
/*
Comparing current password with incoming.
*/
const currentPassword = hashedPassword;
const incomingPassword = 'not secret pass:(';
const isPasswordEqual = this.cryptoService.compare(incomingPassword, currentPassword);
console.log(isPasswordEqual); // false