1.0.0 • Published 5 months ago

@seishinverse/crypto v1.0.0

Weekly downloads
-
License
-
Repository
-
Last release
5 months ago

Crypto

Compact and fast crypto module for hashing and comparing hashes

Installation

npm i @seishinverse/crypto

# or with yarn
yarn add @seishinverse/crypto

# or with pnpm
pnpm i @seishinverse/crypto

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.
  */
  signUp() {
    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
1.0.0

5 months ago

0.0.3

5 months ago

0.0.2

5 months ago

0.0.1

5 months ago