加密

加密

使用加密服务,你可以在应用中加密和解密值。加密基于 aes-256-cbc 算法,并且我们在最终输出上附加了一个完整性哈希(HMAC),以防止值被篡改。

encryption 服务使用存储在 config/app.ts 文件中的 appKey 作为加密值的密钥。

  • 建议妥善保管 appKey,并使用环境变量将其注入到你的应用中。任何能够访问该密钥的人都可以解密值。

  • 密钥长度至少应为 16 个字符,并具有加密安全的随机值。你可以使用 node ace generate:key 命令生成密钥。

  • 如果你决定稍后更改密钥,将无法解密现有值。这将导致现有的 cookie 和用户会话失效。

加密值

你可以使用 encryption.encrypt 方法加密值。该方法接受要加密的值,以及一个可选的时间段,超过该时间段后该值将被视为过期。

import encryption from '@adonisjs/core/services/encryption'
const encrypted = encryption.encrypt('hello world')

定义一个时间段,超过该时间段后值将被视为过期且无法解密。

const encrypted = encryption.encrypt('hello world', '2 hours')

解密值

可以使用 encryption.decrypt 方法解密加密后的值。该方法接受加密后的值作为第一个参数。

import encryption from '@adonisjs/core/services/encryption'
encryption.decrypt(encryptedValue)

支持的数据类型

传递给 encrypt 方法的值会使用 JSON.stringify 序列化为字符串。因此,你可以使用以下 JavaScript 数据类型。

  • string
  • number
  • bigInt
  • boolean
  • null
  • object
  • array
import encryption from '@adonisjs/core/services/encryption'
// Object
encryption.encrypt({
id: 1,
fullName: 'virk',
})
// Array
encryption.encrypt([1, 2, 3, 4])
// Boolean
encryption.encrypt(true)
// Number
encryption.encrypt(10)
// BigInt
encryption.encrypt(BigInt(10))
// Data objects are converted to ISO string
encryption.encrypt(new Date())

使用自定义密钥

你可以直接创建 Encryption 类的实例 来使用自定义密钥。

import { Encryption } from '@adonisjs/core/encryption'
const encryption = new Encryption({
secret: 'alongrandomsecretkey',
})