Skip to content

Compact and fast library to hash and compare values

License

Notifications You must be signed in to change notification settings

seishinverse/crypto

Repository files navigation

Crypto

Compact and fast crypto library for hashing and comparing hashes.

Installation

npm i @seishinverse/crypto

# or with yarn
yarn add @seishinverse/crypto

# or with pnpm
pnpm i @seishinverse/crypto

Documentation

Example of usage for NestJS

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
  }
}

Example of usage in NodeJS

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

About

Compact and fast library to hash and compare values

Resources

License

Stars

Watchers

Forks