Spanish Total.js
English
Documentation For AI agents
Contents

Encryption

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

.md GitHub
On this page

The functions that encrypt and decrypt values, and the secret they use. The guide Encryption explains how to use them with examples; this page is the exact reference.

API

method

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

Encrypts a value with CONF.secret and a key.
ParameterTypeDescription
value Object|String|Number The value.
key? String Your key, combined with CONF.secret.
isUnique? Boolean Different output for the same input.

Returns String The encrypted value; an empty string for null.

Objects are serialized to JSON, numbers to strings, and a function is called for its value. The result is a string you can store in a cookie or a URL. When CONF.default_crypto is set to a Node.js cipher name, the value is encrypted with that cipher, key as the cipher key and CONF.default_crypto_iv.

Encrypt an object
var token = ENCRYPT({ id: user.id, expire: NOW.add('1 day') }, 'reset');
method

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

Decrypts a value from ENCRYPT().
ParameterTypeDescription
value String The encrypted value.
key? String The same key as in ENCRYPT().
jsonConvert? Boolean Parse the result as JSON. Default: true

Returns Object|String|null

By default the result is parsed as JSON and null is returned when it is not JSON, so pass false as jsonConvert for a plain string. Returns null when the value cannot be decrypted.

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

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

Encrypts a value bound to the browser of the request.
ParameterTypeDescription
req Request|Controller The request.
value Object|String The value.
key? String Encryption key.
strict? Boolean Bind to the IP address too. Default: false

Returns String The token.

Stores the user agent (and with strict the IP address) next to value and encrypts it with ENCRYPT(). DECRYPTREQ() returns the value only for a request with the same user agent (and IP address). req can be a request or a controller.

Sign-in cookie
self.cookie('token', ENCRYPTREQ(self, user.id, 'auth'), '1 month');
method

DECRYPTREQ(req, value, [key])

Decrypts a value from ENCRYPTREQ() for the same browser.
ParameterTypeDescription
req Request|Controller The request.
value String The token.
key? String The same key as in ENCRYPTREQ().

Returns Object|String|undefined

Returns the value, or undefined when it cannot be decrypted, or the user agent (or the IP address in strict mode) differs from the request.

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

secret : your-secret

The secret of the app used by ENCRYPT() and DECRYPT().

Returns String

Defaults to a hash generated for the app. Set your own value in /config so tokens stay valid when the app moves to another server. Changing it makes all encrypted tokens invalid.

config
// /config
secret : a-long-random-secret
Made in Europe