Encryption
ENCRYPT(), DECRYPT(), ENCRYPTREQ(), DECRYPTREQ() and the keys $crypto, $cryptoiv and secret.
On this page
The functions that encrypt and decrypt values, and the keys that choose the cipher. The guide Encryption explains how they work together with examples.
API
method
ENCRYPT(value, key, [unique])
Encrypts a value into a string.
ENCRYPT(value, key, [unique])| Parameter | Type | Description |
|---|---|---|
value |
Object |
Any JSON-serializable value. |
key |
String |
A key that must also be used to decrypt. |
unique? |
Boolean |
A random output for the same input. |
Returns String The encrypted value.
value is serialized to JSON; null or undefined gives an empty string. The built-in algorithm uses CONF.secret together with key; with unique the output differs on every call. When CONF.$crypto is set, the value is encrypted with that Node.js cipher and CONF.$cryptoiv, and key must have the byte length of the cipher (unique is not used). F.encrypt() is the same function.
var token = ENCRYPT({ id: user.id, expire: NOW.add('1 day') }, 'reset');
$.success(token);
method
DECRYPT(value, key, [tojson])
Decrypts a string made by ENCRYPT().
DECRYPT(value, key, [tojson])| Parameter | Type | Description |
|---|---|---|
value |
String |
The encrypted string. |
key |
String |
The key used by ENCRYPT(). |
tojson? |
Boolean |
Parse the result as JSON. Default: true |
Returns Object|String The value, or null.
Decrypts a string made by ENCRYPT() with the same key. With tojson (default) the result is parsed as JSON; a string that is not valid, was made with another key or does not contain JSON returns null. With tojson: false you get the decrypted text. When CONF.$crypto is set, the value is decrypted with that Node.js cipher, and a value with an odd number of hex characters returns null at once. F.decrypt() is the same function.
var data = DECRYPT($.query.token, 'reset');
if (!data || data.expire < NOW) {
$.invalid(400);
return;
}
method
ENCRYPTREQ($, value, key, [strict])
Encrypts a value bound to the client of the request.
ENCRYPTREQ($, value, key, [strict])| Parameter | Type | Description |
|---|---|---|
$ |
Object |
The $ of an action or a controller. |
value |
Object |
Any JSON-serializable value. |
key |
String |
The key. |
strict? |
Boolean |
Bind the value to the IP address too. |
Returns String The encrypted value.
Stores a hash of the User-Agent header with the value, and with strict also the IP address. DECRYPTREQ() returns the value only for the same client. F.encryptreq() is the same function.
var token = ENCRYPTREQ($, { file: model.id }, 'download', true);
$.success(token);
method
DECRYPTREQ($, value, key)
Decrypts a value made by ENCRYPTREQ() for the same client.
DECRYPTREQ($, value, key)| Parameter | Type | Description |
|---|---|---|
$ |
Object |
The $ of an action or a controller. |
value |
String |
The encrypted string. |
key |
String |
The key used by ENCRYPTREQ(). |
Returns Object The value, or undefined.
Returns undefined when the value is empty, invalid, or comes from another browser (or another IP address for a strict value). F.decryptreq() is the same function.
var data = DECRYPTREQ($, $.query.token, 'download');
if (!data) {
$.invalid(401);
return;
}
config
$crypto : aes-256-cbc
A Node.js cipher for ENCRYPT() and DECRYPT().
$crypto : aes-256-cbcReturns String
Any algorithm of crypto.createCipheriv(). Without it the built-in algorithm is used. Keys must then have the byte length of the cipher (32 characters for aes-256-cbc).
// /config
$crypto : aes-256-cbc
$cryptoiv : 00112233445566778899aabbccddeeff
config
$cryptoiv : <hex or base64>
The initialization vector for $crypto.
$cryptoiv : <hex or base64>Returns Buffer
Read as base64 when it contains upper-case letters, =, / or +, otherwise as hex. The default is the first 16 bytes of the application hash.
$cryptoiv : 00112233445566778899aabbccddeeff
config
secret : <string>
The application secret used by ENCRYPT() and DECRYPT().
secret : <string>Returns String
It is mixed into the key of the built-in algorithm, so values encrypted with another secret do not decrypt. It has no default (CONF.secret is undefined), so set your own; the same secret keeps tokens valid across servers.
secret : a-long-random-string