📝Crypto

The module that handles encryption and decryption.

Imports

import { CryptoService } from 'niro-health';

Environment Variables

name
description

CRYPTO_PASSWORD

Password used in the encryption and decryption process.

Write .env file with environment variable. Below has an example:

.env
CRYPTO_PASSWORD=o4t7bNT5CbOnlqBcah40R99zierzSQz3MS9Ssceqq2U=

Application Variables

name
values
description

crypto_driver

redis, sqlite, fs

The driver used for store metadata of encryption.

How to set variable

You can set the application variable in your main module and/or in the module that will inject the service.

import { Inject, Injectable } from '@nestjs/common';
import type { IConfigurationService } from 'niro-health';

@Injectable()
export class AppService {
  constructor(
    @Inject('IConfigurationService')
    private readonly configurationService: IConfigurationService,
  ) {
  this.initialize();
}

  async initialize() {
    return this.configurationService.setVariable('crypto_driver', 'redis');
  }
}

Method of Use

To use this module, you need to inject it into the desired service.

import { Module } from '@nestjs/common';
import {
  CryptoService,
  ConfigurationService,
  ValidatorRegexpService,
  StringExService,
  RedisService,
  DebugService,
  SqliteService,
  RandomService,
} from 'niro-health';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: 'ICryptoService',
      useClass: CryptoService,
    },
    {
      provide: 'IConfigurationService',
      useClass: ConfigurationService,
    },
    {
      provide: 'IValidatorRegexpService',
      useClass: ValidatorRegexpService,
    },
    {
      provide: 'IStringExService',
      useClass: StringExService,
    },
    {
      provide: 'IRedisService',
      useClass: RedisService,
    },
    {
      provide: 'IDebugService',
      useClass: DebugService,
    },
    {
      provide: 'ISqliteService',
      useClass: SqliteService,
    },
    {
      provide: 'IRandomService',
      useClass: RandomService,
    },
  ],
})
export class AppModule {}

Injecting the module into our service.

import { Inject, Injectable } from '@nestjs/common';
import type { ICryptoService } from 'niro-health';

@Injectable()
export class AppService {
  constructor(
    @Inject('ICryptoService')
    private readonly cryptoService: ICryptoService,
  ) {}

  async encrypt() {
    return await this.cryptoService.encrypt('Hello World');
  }
}

With dependencies

You will need to inject the following interfaces.

Interface
Service

IConfigurationService

IValidatorRegexpService

IStringExService

IRedisService

IDebugService

ISqliteService

IRandomService

Properties

Property
Scope
Description

get _driver

private

The driver used to store the metadata of encryption.

get _isRedis

private

Check if the driver is Redis.

get _isSqlite

private

Check if the driver is SQLite.

get _isFileSystem

private

Check if the driver is FileSystem.

get _fileSystemPath

private

Returns the path of the file in disk of the FileSystem.

get _fileSystemExistsData

private

Check if the file exists in disk of the FileSystem.

get _fileSystemEncode

private

Returns the encode of the file in disk of the FileSystem.

Methods

Method
Scope
Description

_createTable

private

It create the table in the SQLite database.

_fileSystemWrite

private

It writes the data in the file in disk of the FileSystem.

_fileSystemRead

private

It reads the data in the file in disk of the FileSystem.

_fileSystemDataParse

private

It parses the data in the file in disk of the FileSystem.

_createFileSystem

private

It creates the file in disk of the FileSystem.

_fileSystemAppend

private

It append data in the file in disk of the FileSystem.

_fileSystemFind

private

It finds a key in the file in disk of the FileSystem.

_fileSystemDelete

private

It deletes a key in the file in disk of the FileSystem.

_insert

private

It inserts a key and a value in the SQLite database.

_find

private

It find a key in the SQLite database.

_del

private

It deletes a key from the database.

_createHash

private

It takes a string, creates a hash of it, and returns the first 32 characters of the hash.

_getHash

private

It creates a hash of a random string.

_serialize

private

It takes a string, hashes it, and returns the hash.

_compress

private

It takes a value of type T, converts it to a string, compresses it, and returns the compressed string.

_decompress

private

It takes a string, decompresses it, and returns the result as a generic type.

_save

private

It saves a value to the cache.

_saveBuffer

private

It saves a buffer to the cache.

_load

private

It loads a value from the cache, decompresses it, and returns it.

_loadBuffer

private

It loads a buffer from the cache.

_delete

private

It takes a variable number of strings, serializes them, and then passes them to the Redis client's del function.

encrypt

public

It encrypts a string using a password and saves the encrypted string, the initialization vector, and the authentication tag to the database.

decrypt

public

It decrypts the encrypted string using the password and the IV and the tag.

Last updated