sequelize-encryption
v1.0.3
Published
 [](https://github.com/mithelan/aes-mysql-node) [,'${encryptionKey}') AS CHAR(50))`
2. customQuery - `SELECT [tableFields],CAST(AES_DECRYPT(UNHEX(tableFieldName), encryptionKey) AS CHAR(50))
aliasName FROM tableName`
3. encryptData - `SELECT HEX(AES_ENCRYPT(str, encryptionKey))`
4. decryptData - `SELECT CAST(AES_DECRYPT(UNHEX(encrypted), encryptionKey) as CHAR)`Usage
const { castQuery , customQuery , encryptData, decryptData } = require('sequelize-encryption');castQuery(tableField, encryptionKey);
| column | type | |-----------------|---------------| |
tableField|String| |encryptionKey|String|customQuery( names = [],castName = "",encryption = "",aliasName = "",tableName = "");
| column | type | |-----------------|---------------| |
names|Array| |castName|String| |encryption|Array| |aliasName|String| |tableName|String|encryptData(tableField,encryptionKey);
| column | type | |-----------------|---------------| |
data|String| |encryptionKey|String|decryptData(encryptedValue,encryptionKey);
| column | type | |----------------- |---------------| |
encryptedValue|String| |encryptionKey|String|
Examples
Currently there are 4 methods added.
castQuery
It is more useful when you findAll a model and you need to get a encrypted field changed into decrypted field.
Ex : let persons = await this.personModel.findAll(query);
let query= {
attributes:{
include: [
[
sequelize.literal(
`CAST(AES_DECRYPT(UNHEX(firstName),${encryptionKey}) AS CHAR(50))`
),
'decryptedFirstName',
],
]
}}
Instred of using `CAST(AES_DECRYPT(UNHEX(firstName),${encryption}) AS CHAR(50))`
now we can use,
let query= {
attributes:{
include: [
[
sequelize.literal(
castQuery(firstName,encryptionKey)
),
'decryptedFirstName',
],
]
}}castQuery(firstName,encryptionKey);
NOTE :When passing the encryptionKey wrap with ''- Custom Query
Actual Query :
SELECT id,field2,field3,CAST(AES_DECRYPT(field2, '098a4ad28mde3c4435009c9613749222') AS CHAR(50)) decrypedField FROM personTable`;With Sequelize-encryption actual query :
SELECT ${names.join(",")},CAST(AES_DECRYPT(${castName}, '${encryption}') AS CHAR(50)) ${aliasName} FROM ${tableName}`;With Sequelize-encryption function :
customQuery(['field1','field2],'field1','3c7a4eb821c141726cebd608862da861','decryptedField','personTable');- Encrypt Query & Decrypt Query
You can decrypt and encrypt as same as mysql native functions AES_ENCRYPT and AES_DECRYPT. In this module,we are using the same method used to encrypt and decrypt as mysql.
const { encryptData } = require('sequelize-encryption'); let encryptedFirstName=encryptData(data,encryptionKey);
let decryptFirstName=encryptData(encryptedFirstName,encryptionKey);
| column | type |
|-----------------|---------------|
| data | String |
| encryptionKey | String |
|TYPE|Code|Mysql Query|
|---|---|---|
| String | encryptData(str, encryptionKey) | SELECT HEX(AES_ENCRYPT(str, encryptionKey)) |
| String | decryptData(encryptedField, encryptionKey) | SELECT CAST(AES_DECRYPT(UNHEX(encrypted), encryptionKey) as CHAR)
Run tests
npm testAuthor
👤 mithelan
- Website: https://medium.com/@mithelandev
- Twitter: @kdmithii
- GitHub: @mithelan
- LinkedIn: @MithelanDevanandan
🤝 Contributing
Contributions, issues and feature requests are welcome!
Feel free to check issues page. You can also take a look at the contributing guide.
Show your support
Give a ⭐️ if this project helped you!
📝 License
Copyright © 2022 mithelan.
