Cifratura
ENCRYPT(), DECRYPT(), ENCRYPTREQ(), DECRYPTREQ() e CONF.secret.
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.
ENCRYPT(value, [key], [isUnique])| Parametro | Tipo | Descrizione |
|---|---|---|
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.
var token = ENCRYPT({ id: user.id, expire: NOW.add('1 day') }, 'reset');
method
DECRYPT(value, [key], [jsonConvert])
Decifra un valore prodotto da ENCRYPT().
DECRYPT(value, [key], [jsonConvert])| Parametro | Tipo | Descrizione |
|---|---|---|
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.
var data = DECRYPT(self.query.token, 'reset');
if (!data || data.expire < NOW)
return self.invalid('error-token');
var id = DECRYPT(value, 'key', false);
method
ENCRYPTREQ(req, value, [key], [strict])
Cifra un valore legato al browser della richiesta.
ENCRYPTREQ(req, value, [key], [strict])| Parametro | Tipo | Descrizione |
|---|---|---|
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.
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.
DECRYPTREQ(req, value, [key])| Parametro | Tipo | Descrizione |
|---|---|---|
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.
var id = DECRYPTREQ($.req, $.cookie('token'), 'auth');
config
secret : your-secret
Il segreto dell'app usato da ENCRYPT() e DECRYPT().
secret : your-secretRestituisce 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
secret : a-long-random-secret