const Encryption = use('Encryption')
const encrypted = Encryption.encrypt('hello world')
You are viewing the legacy version of AdonisJS. Visit https://adonisjs.com for newer docs. This version will receive security patches until the end of 2021.
AdonisJs ships with providers for hashing values and encrypting data.
Hashing values is different to encrypting data, since hashed values cannot be decrypted once encrypted.
The AdonisJs encryption provider uses the Node.js crypto module to encrypt and decrypt values.
Your appKey must be defined inside the config/app.js file before you can encrypt values.
|
const Encryption = use('Encryption')
const encrypted = Encryption.encrypt('hello world')
const Encryption = use('Encryption')
const decrypted = Encryption.decrypt('encrypted value')
The AdonisJs hash provider comes with multiple drivers to hash user data.
By default it uses bcrypt, however there is Argon support via the argon2 npm package.
Multiple drivers are supported by @adonisjs/framework version >=5.0.8 .
|
The config is defined inside the config/hash.js
file:
module.exports = {
driver: 'bcrypt',
bcrypt: {
rounds: 10
},
argon: {
type: 1
}
}
If using the argon driver, you will have to install the argon2 npm package package via npm.
|
Hash a plain string value:
const Hash = use('Hash')
const safePassword = await Hash.make(request.input('password'))
Optionally, inline config can be passed to override config file defaults:
const Hash = use('Hash')
const safeExample = await Hash.make('example', config)
Since you cannot decrypt a hash, you can verify the user input against the previously hashed value.
const Hash = use('Hash')
const isSame = await Hash.verify('plain-value', 'hashed-value')
if (isSame) {
// ...
}