The Encrypt Storage
is a wrapper
for native Storage
of browser.
Using the crypto-js
library as an encryption engine, it saves the encrypted data on the selected storage
in the same way as the native Storage
.
NOTE: Nothing on the front end is entirely secure. The library's proposal is to make it difficult for the user to see the data through the console, but as the secret key is on the front end, if the user searches hard enough, he will end up finding it. Just to make it clear that nothing is completely secure on the front end. Thank you for your attention.
- Save encrypted data in
localStorage
andsessionStorage
- Recover encrypted data
- Use in the same way as native Storage
- Use with
stateManagement
persisters (vuex-persist
andredux-persist
)
To run this project in the development mode, you'll need to have a basic environment with NodeJs and Yarn installed.
Using npm:
$ npm install encrypt-storage
Or yarn:
$ yarn add encrypt-storage
const { EncryptStorage } = require('encrypt-storage');
const encryptStorage = EncryptStorage('secret_key', options);
module.exports = encryptStorage
import { EncryptStorage } from 'encrypt-storage';
export const encryptStorage = EncryptStorage('secret_key', options);
Create a file in your utils folder or a folder of your choice. But I advise you to use it as a singleton, so to speak, for better use.
π¦ src
β£ π utils
β β π index.ts
β π index.ts
Directory Layout
// const { EncryptStorage } = require('encrypt-storage');
export const encryptStorage = EncryptStorage('secret_key', options)
The secretKey
parameter is a string
you encrypt your data. If you use a framework
like ReactJS
or VueJS
prefer to store this data in your application's .env
file:
// const { EncryptStorage } = require('encrypt-storage');
export const encryptStorage = EncryptStorage(process.env.SECRET_KEY, options)
The options object consists of the following properties:
key | type | default |
---|---|---|
prefix | string |
null |
storageType | localStorage or sessionStorage |
localStorage |
stateManagementUse | boolean |
false |
prefix: default null
- is optional and is the prefix of all keys used in the selected storage as shown below:
//...
export const encryptStorage = EncryptStorage('secret_key', {
...,
prefix: '@prefix'
});
in your storage:
stateManagementUse: default false
- is a boolean
value that, when true allows the use of it with vuex-persist
and redux-persist
:
...
const persistConfig = {
key: 'root',
storage: encryptStorage,
whitelist: ['navigation'],
...
};
...
const vuexLocal = new VuexPersistence({
storage: encryptStorage
})
storageType: default localStorage
- is the type of storage that will be used, at the moment only localStorage
and sessionStorage
are allowed:
//...
export const encryptStorage = EncryptStorage('secret_key', {
...,
storageType: 'sessionStorage'
});
In the case of aws-amplify
, if you want to use the facility of not needing to use JSON.parse
in the rest of the application, prefer to create an instance within the amplify
configuration file, as follows:
import Amplify from 'aws-amplify';
import { EncryptStorage } from 'encrypt-storage';
const encryptStorage = EncryptStorage('secret_key',{
...,
stateManagementUse: true,
});
...
Amplify.configure({
Auth: {
...,
storage: encryptStorage,
},
});
The usage follows the premise that encryptStorage has been "instantiated" in another file. In the example, the utils folder is used and JS imports.
...
import { encryptStorage } from './utils';
encryptStorage.setItem('user', { name: 'John', age: 36 });
the getItem function is slightly different from the native api because it does not need to have its data treated, when the data exists, by JSON.parse as follows:
...
encryptStorage.setItem('user', { name: 'John', age: 36 });
const decryptedData = encryptStorage.getItem('user');
// { name: 'John', age: 36 } (is an object Javascript, not a string)
Remove an item from selectedStorage
...
encryptStorage.removeItem('user');
Remove an item from selectedStorage
...
encryptStorage.setItem('12345678:user', { id: 1234 });
encryptStorage.setItem('12345678:item', { id: 5678 });
encryptStorage.removeItemFromPattern('12345678');
// items '12345678:user' and '12345678:item' are removed from 'selectedStorage'
Clear all data from selectedStorage
...
encryptStorage.clear();
Returns a key of index
param or null
if not exists
...
encryptStorage.key(0);
Returns a quantity
of keys
existents in selectedStorage
...
encryptStorage.length;
Encrypt any string and rerturn encrypted value
...
import { encryptStorage } from './utils';
const encryptedValue = encryptStorage.encryptString('any_string');
// 'U2FsdGVkX1/jvF6fLkGI3aALI9ssWNAPAeZ5nxeskh8='
Decrypt any string (encrypted with encryptString
) and return decrypted value
...
import { encryptStorage } from './utils';
const decryptedValue = encryptStorage.decryptString('U2FsdGVkX1/jvF6fLkGI3aALI9ssWNAPAeZ5nxeskh8=');
// 'any_string'