1.0.3 ā€¢ Published 2 years ago

sequelize-encryption v1.0.3

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

Welcome to sequelize-encryption šŸ‘‹

Version Documentation License: '' Twitter: kdmithii

sequelize-encryption is tool where you can use to encrypt and decrypt the fields same as mysql native aes_encrypt and aes_decrypt and casting and custom methods available

šŸ  Homepage

Install

npm i sequelize-encryption

or

yarn add sequelize-encryption

Generate a encryption key

openssl rand -hex 16

sample-> 3c7a4eb821c141726cebd608862da861

Available methods

  1. castQuery - `CAST(AES_DECRYPT(UNHEX(tableFieldName}),'${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');
  1. castQuery(tableField, encryptionKey);

    columntype
    tableFieldString
    encryptionKeyString
  1. customQuery( names = [],castName = "",encryption = "",aliasName = "",tableName = "");

    columntype
    namesArray
    castNameString
    encryptionArray
    aliasNameString
    tableNameString
  2. encryptData(tableField,encryptionKey);

    columntype
    dataString
    encryptionKeyString
  3. decryptData(encryptedValue,encryptionKey);

    columntype
    encryptedValueString
    encryptionKeyString

Examples

Currently there are 4 methods added.

  1. 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 ''
  1. 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');
  1. 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);

columntype
dataString
encryptionKeyString
TYPECodeMysql Query
StringencryptData(str, encryptionKey)SELECT HEX(AES_ENCRYPT(str, encryptionKey))
StringdecryptData(encryptedField, encryptionKey)SELECT CAST(AES_DECRYPT(UNHEX(encrypted), encryptionKey) as CHAR)

Run tests

npm test

Author

šŸ‘¤ mithelan

šŸ¤ 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.