> For the complete documentation index, see [llms.txt](https://guilhermesantos.gitbook.io/niro-health/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://guilhermesantos.gitbook.io/niro-health/modules/crypto.md).

# Crypto

### Imports

{% tabs %}
{% tab title="Service" %}

```typescript
import { CryptoService } from 'niro-health';
```

{% endtab %}

{% tab title="Module" %}

```typescript
import { CryptoModule } from 'niro-health';
```

{% endtab %}

{% tab title="Interface" %}

```typescript
import type { ICryptoService } from 'niro-health';
```

{% endtab %}
{% endtabs %}

### Environment Variables

<table><thead><tr><th width="220">name</th><th>description</th></tr></thead><tbody><tr><td><code>CRYPTO_PASSWORD</code></td><td><code>Password used in the encryption and decryption process.</code></td></tr></tbody></table>

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

{% code title=".env" %}

```bash
CRYPTO_PASSWORD=o4t7bNT5CbOnlqBcah40R99zierzSQz3MS9Ssceqq2U=
```

{% endcode %}

### Application Variables

<table><thead><tr><th width="260">name</th><th>values</th><th>description</th></tr></thead><tbody><tr><td><code>crypto_driver</code></td><td><mark style="color:blue;"><strong><code>redis</code></strong></mark>, <mark style="color:orange;"><strong><code>sqlite</code></strong></mark>, <mark style="color:green;"><strong><code>fs</code></strong></mark></td><td><code>The driver used for store metadata of encryption.</code></td></tr></tbody></table>

#### How to set variable

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

```typescript
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.

{% code overflow="wrap" %}

```typescript
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 {}
```

{% endcode %}

Injecting the module into our service.

```typescript
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.

<table><thead><tr><th width="308">Interface</th><th>Service</th></tr></thead><tbody><tr><td><code>IConfigurationService</code></td><td><a href="/pages/qVNUf3tpeJXEx7j4GiuW">Link to Service documentation</a></td></tr><tr><td><code>IValidatorRegexpService</code></td><td><a href="/pages/O8NjCtcCgk2uaVu1oyq5">Link to Service documentation</a></td></tr><tr><td><code>IStringExService</code></td><td><a href="/pages/9boqQn1bIK4z9uqBM3n0">Link to Service documentation</a></td></tr><tr><td><code>IRedisService</code></td><td><a href="/pages/oMMkCq1HflhjKmTTReq6">Link to Service documentation</a></td></tr><tr><td><code>IDebugService</code></td><td><a href="/pages/uyjr6SUpKSsZ5yQcFfOM">Link to Service documentation</a></td></tr><tr><td><code>ISqliteService</code></td><td><a href="/pages/9A0vLb6zPW7bRsK9sDGw">Link to Service documentation</a></td></tr><tr><td><code>IRandomService</code></td><td><a href="/pages/KD8jDSxoNLbexvPgv0ps">Link to Service documentation</a></td></tr></tbody></table>

### Properties

<table><thead><tr><th width="301.3333333333333">Property</th><th width="127" align="center">Scope</th><th>Description</th></tr></thead><tbody><tr><td><mark style="color:blue;"><strong>get</strong></mark> <code>_driver</code></td><td align="center"><code>private</code></td><td><code>The driver used to store the metadata of encryption.</code></td></tr><tr><td><mark style="color:blue;"><strong>get</strong></mark> <code>_isRedis</code></td><td align="center"><code>private</code></td><td><code>Check if the driver is Redis.</code></td></tr><tr><td><mark style="color:blue;"><strong>get</strong></mark> <code>_isSqlite</code></td><td align="center"><code>private</code></td><td><code>Check if the driver is SQLite.</code></td></tr><tr><td><mark style="color:blue;"><strong>get</strong></mark> <code>_isFileSystem</code></td><td align="center"><code>private</code></td><td><code>Check if the driver is FileSystem.</code></td></tr><tr><td><mark style="color:blue;"><strong>get</strong></mark> <code>_fileSystemPath</code></td><td align="center"><code>private</code></td><td><code>Returns the path of the file in disk of the FileSystem.</code></td></tr><tr><td><mark style="color:blue;"><strong>get</strong></mark> <code>_fileSystemExistsData</code></td><td align="center"><code>private</code></td><td><code>Check if the file exists in disk of the FileSystem.</code></td></tr><tr><td><mark style="color:blue;"><strong>get</strong></mark> <code>_fileSystemEncode</code></td><td align="center"><code>private</code></td><td><code>Returns the encode of the file in disk of the FileSystem.</code></td></tr></tbody></table>

### Methods

<table><thead><tr><th width="301.3333333333333">Method</th><th width="129" align="center">Scope</th><th>Description</th></tr></thead><tbody><tr><td><code>_createTable</code></td><td align="center"><code>private</code></td><td><code>It create the table in the SQLite database.</code></td></tr><tr><td><code>_fileSystemWrite</code></td><td align="center"><code>private</code></td><td><code>It writes the data in the file in disk of the FileSystem.</code></td></tr><tr><td><code>_fileSystemRead</code></td><td align="center"><code>private</code></td><td><code>It reads the data in the file in disk of the FileSystem.</code></td></tr><tr><td><code>_fileSystemDataParse</code></td><td align="center"><code>private</code></td><td><code>It parses the data in the file in disk of the FileSystem.</code></td></tr><tr><td><code>_createFileSystem</code></td><td align="center"><code>private</code></td><td><code>It creates the file in disk of the FileSystem.</code></td></tr><tr><td><code>_fileSystemAppend</code></td><td align="center"><code>private</code></td><td><code>It append data in the file in disk of the FileSystem.</code></td></tr><tr><td><code>_fileSystemFind</code></td><td align="center"><code>private</code></td><td><code>It finds a key in the file in disk of the FileSystem.</code></td></tr><tr><td><code>_fileSystemDelete</code></td><td align="center"><code>private</code></td><td><code>It deletes a key in the file in disk of the FileSystem.</code></td></tr><tr><td><code>_insert</code></td><td align="center"><code>private</code></td><td><code>It inserts a key and a value in the SQLite database.</code></td></tr><tr><td><code>_find</code></td><td align="center"><code>private</code></td><td><code>It find a key in the SQLite database.</code></td></tr><tr><td><code>_del</code></td><td align="center"><code>private</code></td><td><code>It deletes a key from the database.</code></td></tr><tr><td><code>_createHash</code></td><td align="center"><code>private</code></td><td><code>It takes a string, creates a hash of it, and returns the first 32 characters of the hash.</code></td></tr><tr><td><code>_getHash</code></td><td align="center"><code>private</code></td><td><code>It creates a hash of a random string.</code></td></tr><tr><td><code>_serialize</code></td><td align="center"><code>private</code></td><td><code>It takes a string, hashes it, and returns the hash.</code></td></tr><tr><td><code>_compress</code></td><td align="center"><code>private</code></td><td><code>It takes a value of type T, converts it to a string, compresses it, and returns the compressed string.</code></td></tr><tr><td><code>_decompress</code></td><td align="center"><code>private</code></td><td><code>It takes a string, decompresses it, and returns the result as a generic type.</code></td></tr><tr><td><code>_save</code></td><td align="center"><code>private</code></td><td><code>It saves a value to the cache.</code></td></tr><tr><td><code>_saveBuffer</code></td><td align="center"><code>private</code></td><td><code>It saves a buffer to the cache.</code></td></tr><tr><td><code>_load</code></td><td align="center"><code>private</code></td><td><code>It loads a value from the cache, decompresses it, and returns it.</code></td></tr><tr><td><code>_loadBuffer</code></td><td align="center"><code>private</code></td><td><code>It loads a buffer from the cache.</code></td></tr><tr><td><code>_delete</code></td><td align="center"><code>private</code></td><td><code>It takes a variable number of strings, serializes them, and then passes them to the Redis client's del function.</code></td></tr><tr><td><code>encrypt</code></td><td align="center"><code>public</code></td><td><code>It encrypts a string using a password and saves the encrypted string, the initialization vector, and the authentication tag to the database.</code></td></tr><tr><td><code>decrypt</code></td><td align="center"><code>public</code></td><td><code>It decrypts the encrypted string using the password and the IV and the tag.</code></td></tr></tbody></table>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://guilhermesantos.gitbook.io/niro-health/modules/crypto.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
