---
title: Cifratura
project: Total.js V4
version: 0.0.99
section: reference
language: it
license: MIT
url: https://totaljs.es/docs/it/total-js-v4/0-0-99/reference/encryption/
updated: 2026-11-18T16:57:34.578Z
---

# Cifratura

> ENCRYPT(), DECRYPT(), ENCRYPTREQ(), DECRYPTREQ() e CONF.secret.

Le funzioni che cifrano e decifrano i valori e il secret che usano. La guida [Cifratura](../../guides/encryption/) spiega come usarle con esempi; questa pagina è il riferimento esatto.

## API

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

Type: `method`

Cifra un valore con CONF.secret e una chiave.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object|String|Number` | yes |  | Il valore. |
| `key` | `String` | no |  | La tua chiave, combinata con `CONF.secret`. |
| `isUnique` | `Boolean` | no |  | Output diverso per lo stesso input. |

Returns: `String` Il valore cifrato; una stringa vuota per `null`.

Gli oggetti vengono serializzati in JSON, i numeri in stringhe, e una funzione viene chiamata per ottenerne il valore. Il risultato è una stringa che puoi salvare in un cookie o in un URL. Quando `CONF.default_crypto` è impostato al nome di un cifrario di Node.js, il valore viene cifrato con quel cifrario, `key` come chiave del cifrario e `CONF.default_crypto_iv`.

Example: Cifrare un oggetto
```js
var token = ENCRYPT({ id: user.id, expire: NOW.add('1 day') }, 'reset');
```

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

Type: `method`

Decifra un valore prodotto da ENCRYPT().

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String` | yes |  | Il valore cifrato. |
| `key` | `String` | no |  | La stessa chiave usata in `ENCRYPT()`. |
| `jsonConvert` | `Boolean` | no | `true` | Analizza il risultato come JSON. |

Returns: `Object|String|null`

Per impostazione predefinita il risultato viene analizzato come JSON e viene restituito `null` quando non è JSON, quindi passa `false` come `jsonConvert` per una stringa semplice. Restituisce `null` quando il valore non può essere decifrato.

Example: Decifrare
```js
var data = DECRYPT(self.query.token, 'reset');
if (!data || data.expire < NOW)
	return self.invalid('error-token');
```

Example: Stringa semplice
```js
var id = DECRYPT(value, 'key', false);
```

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

Type: `method`

Cifra un valore legato al browser della richiesta.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `req` | `Request|Controller` | yes |  | La richiesta. |
| `value` | `Object|String` | yes |  | Il valore. |
| `key` | `String` | no |  | Chiave di cifratura. |
| `strict` | `Boolean` | no | `false` | Lega anche all'indirizzo IP. |

Returns: `String` Il token.

Salva lo user agent (e con `strict` l'indirizzo IP) accanto a `value` e lo cifra con `ENCRYPT()`. `DECRYPTREQ()` restituisce il valore solo per una richiesta con lo stesso user agent (e indirizzo IP). `req` può essere una richiesta o un controller.

Example: Cookie di accesso
```js
self.cookie('token', ENCRYPTREQ(self, user.id, 'auth'), '1 month');
```

### DECRYPTREQ(req, value, [key])

Type: `method`

Decifra un valore prodotto da ENCRYPTREQ() per lo stesso browser.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `req` | `Request|Controller` | yes |  | La richiesta. |
| `value` | `String` | yes |  | Il token. |
| `key` | `String` | no |  | La stessa chiave usata in `ENCRYPTREQ()`. |

Returns: `Object|String|undefined`

Restituisce il valore, oppure `undefined` quando non può essere decifrato o quando lo user agent (o l'indirizzo IP in modalità strict) è diverso da quello della richiesta.

Example: In AUTH()
```js
var id = DECRYPTREQ($.req, $.cookie('token'), 'auth');
```

### secret : your-secret

Type: `config`

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

Returns: `String`

Per impostazione predefinita è un hash generato per l'app. Imposta un tuo valore in `/config` così i token restano validi quando l'app viene spostata su un altro server. Cambiarlo rende non validi tutti i token cifrati.

Example: config
```js
// /config
secret : a-long-random-secret
```
