Spanish Total.js
Italiano
Documentazione Per agenti IA
Indice

Cifratura

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

.md GitHub
In questa pagina

Le funzioni che cifrano e decifrano i valori e il secret che usano. La guida Cifratura spiega come usarle con esempi; questa pagina è il riferimento esatto.

API

method

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

Cifra un valore con CONF.secret e una chiave.
ParametroTipoDescrizione
value Object|String|Number Il valore.
key? String La tua chiave, combinata con CONF.secret.
isUnique? Boolean Output diverso per lo stesso input.

Restituisce 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.

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

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

Decifra un valore prodotto da ENCRYPT().
ParametroTipoDescrizione
value String Il valore cifrato.
key? String La stessa chiave usata in ENCRYPT().
jsonConvert? Boolean Analizza il risultato come JSON. Predefinito: true

Restituisce 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.

Decifrare
var data = DECRYPT(self.query.token, 'reset');
if (!data || data.expire < NOW)
	return self.invalid('error-token');
Stringa semplice
var id = DECRYPT(value, 'key', false);
method

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

Cifra un valore legato al browser della richiesta.
ParametroTipoDescrizione
req Request|Controller La richiesta.
value Object|String Il valore.
key? String Chiave di cifratura.
strict? Boolean Lega anche all'indirizzo IP. Predefinito: false

Restituisce 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.

Cookie di accesso
self.cookie('token', ENCRYPTREQ(self, user.id, 'auth'), '1 month');
method

DECRYPTREQ(req, value, [key])

Decifra un valore prodotto da ENCRYPTREQ() per lo stesso browser.
ParametroTipoDescrizione
req Request|Controller La richiesta.
value String Il token.
key? String La stessa chiave usata in ENCRYPTREQ().

Restituisce 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.

In AUTH()
var id = DECRYPTREQ($.req, $.cookie('token'), 'auth');
config

secret : your-secret

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

Restituisce 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.

config
// /config
secret : a-long-random-secret
Fatto in Europa