---
title: Cifratura
project: Total.js V5
version: 0.0.20
section: reference
language: it
license: MIT
url: https://totaljs.es/docs/it/total-js-v5/0-0-20/reference/encryption/
updated: 2026-11-18T16:57:19.575Z
---

# Cifratura

> ENCRYPT(), DECRYPT(), ENCRYPTREQ(), DECRYPTREQ() e le chiavi $crypto, $cryptoiv e secret.

Le funzioni che cifrano e decifrano i valori e le chiavi che scelgono il cifrario. La guida [Cifratura](../../guides/encryption/) spiega come funzionano insieme, con esempi.

## API

### ENCRYPT(value, key, [unique])

Type: `method`

Cifra un valore in una stringa.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | Qualsiasi valore serializzabile in JSON. |
| `key` | `String` | yes |  | Una chiave che deve essere usata anche per decifrare. |
| `unique` | `Boolean` | no |  | Un output casuale per lo stesso input. |

Returns: `String` Il valore cifrato.

`value` viene serializzato in JSON; `null` o `undefined` dà una stringa vuota. L'algoritmo integrato usa `CONF.secret` insieme a `key`; con `unique` l'output è diverso a ogni chiamata. Quando `CONF.$crypto` è impostato, il valore viene cifrato con quel cifrario Node.js e `CONF.$cryptoiv`, e `key` deve avere la lunghezza in byte del cifrario (`unique` non viene usato). `F.encrypt()` è la stessa funzione.

Example: Link di reset
```js
var token = ENCRYPT({ id: user.id, expire: NOW.add('1 day') }, 'reset');
$.success(token);
```

### DECRYPT(value, key, [tojson])

Type: `method`

Decifra una stringa creata da ENCRYPT().

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String` | yes |  | La stringa cifrata. |
| `key` | `String` | yes |  | La chiave usata da `ENCRYPT()`. |
| `tojson` | `Boolean` | no | `true` | Analizza il risultato come JSON. |

Returns: `Object|String` Il valore, oppure `null`.

Decifra una stringa creata da `ENCRYPT()` con la stessa `key`. Con `tojson` (predefinito) il risultato viene analizzato come JSON; una stringa non valida, creata con un'altra chiave o che non contiene JSON restituisce `null`. Con `tojson: false` ottieni il testo decifrato. Quando `CONF.$crypto` è impostato, il valore viene decifrato con quel cipher di Node.js, e un valore con un numero dispari di caratteri esadecimali restituisce subito `null`. `F.decrypt()` è la stessa funzione.

Example: Verificare un token
```js
var data = DECRYPT($.query.token, 'reset');
if (!data || data.expire < NOW) {
	$.invalid(400);
	return;
}
```

### ENCRYPTREQ($, value, key, [strict])

Type: `method`

Cifra un valore legato al client della richiesta.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `$` | `Object` | yes |  | Il `$` di un'azione o di un controller. |
| `value` | `Object` | yes |  | Qualsiasi valore serializzabile in JSON. |
| `key` | `String` | yes |  | La chiave. |
| `strict` | `Boolean` | no |  | Lega il valore anche all'indirizzo IP. |

Returns: `String` Il valore cifrato.

Salva un hash dell'header `User-Agent` insieme al valore, e con `strict` anche l'indirizzo IP. `DECRYPTREQ()` restituisce il valore solo per lo stesso client. `F.encryptreq()` è la stessa funzione.

Example: Token di download
```js
var token = ENCRYPTREQ($, { file: model.id }, 'download', true);
$.success(token);
```

### DECRYPTREQ($, value, key)

Type: `method`

Decifra un valore creato da ENCRYPTREQ() per lo stesso client.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `$` | `Object` | yes |  | Il `$` di un'azione o di un controller. |
| `value` | `String` | yes |  | La stringa cifrata. |
| `key` | `String` | yes |  | La chiave usata da `ENCRYPTREQ()`. |

Returns: `Object` Il valore, oppure `undefined`.

Restituisce `undefined` quando il valore è vuoto, non valido, o proviene da un altro browser (o da un altro indirizzo IP per un valore strict). `F.decryptreq()` è la stessa funzione.

Example: Controllare il token
```js
var data = DECRYPTREQ($, $.query.token, 'download');
if (!data) {
	$.invalid(401);
	return;
}
```

### $crypto : aes-256-cbc

Type: `config`

Un cifrario Node.js per ENCRYPT() e DECRYPT().

Returns: `String`

Qualsiasi algoritmo di `crypto.createCipheriv()`. Senza di esso viene usato l'algoritmo integrato. Le chiavi devono allora avere la lunghezza in byte del cifrario (32 caratteri per `aes-256-cbc`).

Example: Config
```js
// /config
$crypto      : aes-256-cbc
$cryptoiv    : 00112233445566778899aabbccddeeff
```

### $cryptoiv : <hex or base64>

Type: `config`

Il vettore di inizializzazione per $crypto.

Returns: `Buffer`

Letto come base64 quando contiene lettere maiuscole, `=`, `/` o `+`, altrimenti come hex. Il valore predefinito sono i primi 16 byte dell'hash dell'applicazione.

Example: Config
```js
$cryptoiv : 00112233445566778899aabbccddeeff
```

### secret : <string>

Type: `config`

Il segreto dell'applicazione usato da ENCRYPT() e DECRYPT().

Returns: `String`

Viene mescolato nella chiave dell'algoritmo integrato, quindi i valori cifrati con un altro segreto non vengono decifrati. Non ha un valore predefinito (`CONF.secret` è `undefined`), quindi imposta il tuo; lo stesso segreto mantiene validi i token tra server diversi.

Example: Config
```js
secret : a-long-random-string
```
