- Notifications
You must be signed in to change notification settings - Fork2
Encrypt your data when storing & decrypt when fetching in indexeddb
License
ujjwalguptaofficial/jsstore-encrypt
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Encrypt your data when storing & decrypt when fetching in IndexedDB
It is a jsstore plugin which register amiddleware. The middleware encrypt or decrypt values based on query.
npm i jsstore-encrypt
https://github.com/ujjwalguptaofficial/jsstore-encrypt/tree/main/examples/
importScripts("https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js")varsecret=CryptoJS.enc.Hex.parse('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f');varJsStoreEncrypt={encrypt(message){constdata=CryptoJS.AES.encrypt(message,secret,{mode:CryptoJS.mode.ECB}).toString();console.log("data",data);returndata;},decrypt(message){vardecryptedBytes=CryptoJS.AES.decrypt(message,secret,{mode:CryptoJS.mode.ECB});returndecryptedBytes.toString(CryptoJS.enc.Utf8);}}
save this code in a javascript file. Let's say we have saved inside file name -jsstore-encrypt.js
This code will be used to encrypt decrypt the value.jsstore-encrypt
search for objectJsStoreEncrypt
and then useencrypt
,decrypt
method, so its important that you follow this pattern.
- Above code uses cryptojs AES algorithm. But you can use any library or algorithm.
- If your code is asychronous, you can return promise.
- In order to query using
where
- the encrypt algorithm should generate the same value always, so that we can encrypt a value and search in stored values.
import{encryptPlugin}from"jsstore-encrypt";varconnection=newJsStore.Connection();connection.addPlugin(encryptPlugin,"path to jsstore_encrypt.js");
consttblStudent={name:'Students',columns:{id:{primaryKey:true,autoIncrement:true},name:{notNull:true,dataType:DATA_TYPE.String},secret:{dataType:DATA_TYPE.String,encrypt:true}asany}};constdataBase:IDataBase={name:dbname,tables:[tblStudent]};
In the above schema, columnsecret
is marked to be encrypted. So only column secret will be encrypted when inserted or updated & decrypted when selecting.
connection.insert({into:"Students",values:[{city:"bangalore",country:"india",gender:"male",name:"ujjwal",secret:"i want to travel the world"}],encrypt:true})
Theencrypt
option tells jsstore-encrypt to encrypt the values. Only column marked withecnrypt
in the database schema will be encrypted - in our casesecret
.
connection.select({from:"Students",decrypt:true,})
Thedecrypt
option tells jsstore-decrypt to decrypt the values. Only column marked withecnrypt
will be decrypted - in our casesecret
column only.
connection.update({in:"Students",encrypt:true,set:{name:'Ujjwal Gupta',secret:"Being more human"}})
In case of update,set
values are encrypted.
In order to filter data usingwhere
- the encrypt algorithm should generate the same value always, so that we can encrypt a value and search in stored values.
connection.select({from:"Students",decrypt:{where:{secret:"Being more human"}}})
When you addwhere
inside decrypt/encrypt, all values inside where are encrypted.
👉 You can also use your normal field without encrypt option similar to how you were using before -
connection.select({from:"Students",decrypt:{where:{secret:"Being more human"}},where:{id:1}})
Note:- Partial where option -regex
,like
etc doesn't work in case of encryption as the values are stored as different data.
About
Encrypt your data when storing & decrypt when fetching in indexeddb
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.