Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add possibility to use two instances in parallel #23

Merged
merged 8 commits into from
Jan 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,22 @@ var ls = new SecureLS();
`Contructor` accepts a configurable `Object` with all three keys being optional.


| Config Keys | default | accepts |
| --------------------- | -------------- | ----------------------------------------- |
| **encodingType** | Base64 | `base64`/`aes`/`des`/`rabbit`/`rc4`/`''` |
| **isCompression** | `true` | `true`/`false` |
| **encryptionSecret** | PBKDF2 value | String |

**Note:** `encryptionSecret` will only be used for the Encryption and Decryption of data with `AES`, `DES`, `RC4`, `RABBIT`, and the library will discard it if no encoding / Base64 encoding method is choosen.

| Config Keys | default | accepts |
| ------------------------ | -------------- | ----------------------------------------- |
| **encodingType** | Base64 | `base64`/`aes`/`des`/`rabbit`/`rc4`/`''` |
| **isCompression** | `true` | `true`/`false` |
| **encryptionSecret** | PBKDF2 value | String |
| **encryptionNamespace** | null | String |

**Note:** `encryptionSecret` will only be used for the Encryption and Decryption of data
with `AES`, `DES`, `RC4`, `RABBIT`, and the library will discard it if no encoding / Base64
encoding method is choosen.

`encryptionNamespace` is used to make multiple instances with different `encryptionSecret`
and/or different `encryptionSecret` possible.

var ls1 = new SecureLS({encodingType: 'des', encryptionSecret: 'my-secret-key-1'});
var ls2 = new SecureLS({encodingType: 'aes', encryptionSecret: 'my-secret-key-2'});

**Examples:**

Expand Down
29 changes: 27 additions & 2 deletions dist/secure-ls.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
export interface SecureLS {
new(config?: { isCompression: boolean, encodingType: string, encryptionSecret: string }): SecureLS;
export = SecureLS;

import * as LZString from 'lz-string';
import {CipherHelper, Encoder} from 'crypto-js';

declare class SecureLS {
constructor(config?: { isCompression?: boolean, encodingType?: string, encryptionSecret?: string , encryptionNamespace?: string });
getEncryptionSecret(): string;
get(key: string, isAllKeysData?: boolean): any;
getDataFromLocalStorage(key: string): string | null;
Expand All @@ -13,4 +18,24 @@ export interface SecureLS {
processData(data: any | string, isAllKeysData: boolean): string;
setMetaData(): void;
getMetaData(): { keys: string[] };

_name: 'secure-ls';
Base64: SecureLS.Base64;
LZString: LZString.LZStringStatic;
AES: CipherHelper;
DES: CipherHelper;
RABBIT: CipherHelper;
RC4: CipherHelper;
enc: {
Latin1: Encoder;
_Utf8: Encoder;
};
}

declare namespace SecureLS{
interface Base64 {
_keyStr: string;
encode(e: string);
decode(e: string);
}
}
18 changes: 12 additions & 6 deletions dist/secure-ls.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/secure-ls.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/secure-ls.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/secure-ls.min.js.map

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions example/aes-compressed-realm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var data = {data: [{age: 1}, {age: '2'}]};
var aesCRealm1 = new SecureLS({encodingType: 'aes', encryptionSecret: 'secret1', encryptionNamespace: 'realm1'});
var key1 = 'aes__compressed_1';
var ae = aesCRealm1.AES.encrypt(JSON.stringify(data), '');
var bde = aesCRealm1.AES.decrypt(ae.toString(), '');
var de = bde.toString(aesCRealm1.enc._Utf8);
var aesCRealm2 = new SecureLS({encodingType: 'aes', encryptionSecret: 'secret2', encryptionNamespace: 'realm2'});
var key2 = 'aes__compressed_2';
var ae2 = aesCRealm2.AES.encrypt(JSON.stringify(data), '');
var bde2 = aesCRealm2.AES.decrypt(ae2.toString(), '');
var de2 = bde2.toString(aesCRealm2.enc._Utf8);

aesCRealm1.set(key1, data);
console.log('AES Compressed Realm1');
console.log(localStorage.getItem(key1));
console.log(aesCRealm1.get(key1));
console.log('____________________________________');

aesCRealm2.set(key2, data);
console.log('AES Compressed Realm2');
console.log(localStorage.getItem(key2));
console.log(aesCRealm2.get(key2));
console.log('____________________________________');
1 change: 1 addition & 0 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<script type="text/javascript" src="only-compressed.js"></script>
<script type="text/javascript" src="base64-compressed.js"></script>
<script type="text/javascript" src="aes-compressed.js"></script>
<script type="text/javascript" src="aes-compressed-realm.js"></script>
<script type="text/javascript" src="aes-uncompressed.js"></script>
<script type="text/javascript" src="des-compressed.js"></script>
<script type="text/javascript" src="des-uncompressed.js"></script>
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "secure-ls",
"version": "1.2.4",
"version": "1.2.5",
"description": "Secure localStorage data with high level of encryption and data compression",
"main": "./dist/secure-ls.js",
"typings": "./dist/secure-ls.d.ts",
Expand All @@ -14,6 +14,8 @@
"coveralls": "cat ./coverage/lcov.info | node node_modules/.bin/coveralls"
},
"devDependencies": {
"@types/crypto-js": "^3.1.43",
"@types/lz-string": "^1.3.32",
"babel": "6.3.13",
"babel-core": "6.1.18",
"babel-eslint": "5.0.0",
Expand Down
17 changes: 11 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,23 @@ export default class SecureLS {

this.config = {
isCompression: true,
encodingType: constants.EncrytionTypes.BASE64
encodingType: constants.EncrytionTypes.BASE64,
encryptionSecret: config.encryptionSecret,
encryptionNamespace: config.encryptionNamespace
};
this.config.isCompression = typeof config.isCompression !== 'undefined' ?
config.isCompression :
true;
this.config.encodingType = (typeof config.encodingType !== 'undefined' || config.encodingType === '') ?
config.encodingType.toLowerCase() :
constants.EncrytionTypes.BASE64;
this.config.encryptionSecret = config.encryptionSecret;

this.ls = localStorage;
this.init();
};

init() {
let metaData = this.getMetaData() || {};
let metaData = this.getMetaData();

this.WarningEnum = this.constants.WarningEnum;
this.WarningTypes = this.constants.WarningTypes;
Expand Down Expand Up @@ -88,7 +89,7 @@ export default class SecureLS {
}

getEncryptionSecret(key) {
let metaData = this.getMetaData() || {};
let metaData = this.getMetaData();
let obj = this.utils.getObjectFromKey(metaData.keys, key);

if (!obj) {
Expand Down Expand Up @@ -289,11 +290,15 @@ export default class SecureLS {
}, true);

// Store the data to localStorage
this.setDataToLocalStorage(this.utils.metaKey, dataToStore);
this.setDataToLocalStorage(this.getMetaKey(), dataToStore);
};

getMetaData() {
return this.get(this.utils.metaKey, true);
return this.get(this.getMetaKey(), true) || {};
};

getMetaKey() {
return this.utils.metaKey + (this.config.encryptionNamespace ? '__' + this.config.encryptionNamespace : '');
}

};
2 changes: 1 addition & 1 deletion test/library.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import './localStorage.spec';
import './utils.spec';
import './ls-data-compression.spec';
import './ls-data-enc-dec.spec';
import './functional.spec';
import './functional.spec';
55 changes: 50 additions & 5 deletions test/ls-data-enc-dec.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ describe('Encryption / Decryption Tests ->', () => {
lib.set(key, data);

// corresponding to [1, 2, 3] => "⪂恢ೠ☎ڰځ᠁쁺Ÿીꀜ鄈Àኀ퐁᠁肢ϙ㑀娃࠰Ⲁ찠̨ư༠ǟ踈Ÿ耀 " i.e. compressed AES encrypted
valueStored = lib.LZString.compressToUTF16(lib.AES.encrypt(JSON.stringify(data), lib.utils.encryptionSecret).toString());
valueStored = lib.LZString.compressToUTF16(
lib.AES.encrypt(JSON.stringify(data), lib.utils.encryptionSecret).toString()
);

expect(mockLS.storage[key]).to.exist;
expect(mockLS.storage[key]).to.be.a('string');
Expand Down Expand Up @@ -135,12 +137,13 @@ describe('Encryption / Decryption Tests ->', () => {
expect(lib.utils.encryptionSecret).to.equal('mySecretKey123');

// corresponding to [1, 2, 3] => "⪂恢ೠ☎ڰځ᠁쁺Ÿીꀜ鄈Àኀ퐁᠁肢ϙ㑀娃࠰Ⲁ찠̨ư༠ǟ踈Ÿ耀 " i.e. compressed AES encrypted
valueStored = lib.LZString.compressToUTF16(lib.AES.encrypt(JSON.stringify(data), lib.utils.encryptionSecret).toString());
valueStored = lib.LZString.compressToUTF16(
lib.AES.encrypt(JSON.stringify(data), lib.utils.encryptionSecret).toString()
);

expect(mockLS.storage[key]).to.exist;
expect(mockLS.storage[key]).to.be.a('string');


// Can't check exact values since CryptoJS encryption is time-dependent
// expect(mockLS.storage[key]).to.equal(valueStored);

Expand Down Expand Up @@ -222,7 +225,9 @@ describe('Encryption / Decryption Tests ->', () => {
lib.set(key, data);

// corresponding to [1, 2, 3] => "⪂恢ೠ☎ڰځ᠉쁡㠓䌄倈쁺ᆰୀ䬐ʐɀ挀喠儴ݲ " i.e. compressed DES encrypted
valueStored = lib.LZString.compressToUTF16(lib.DES.encrypt(JSON.stringify(data), lib.utils.encryptionSecret).toString());
valueStored = lib.LZString.compressToUTF16(
lib.DES.encrypt(JSON.stringify(data), lib.utils.encryptionSecret).toString()
);

expect(mockLS.storage[key]).to.exist;
expect(mockLS.storage[key]).to.be.a('string');
Expand Down Expand Up @@ -369,7 +374,9 @@ describe('Encryption / Decryption Tests ->', () => {
lib.set(key, data);

// corresponding to [1, 2, 3] => "⪂恢ೠ☎ڰځ᠍䁅̘ࡀ⡀⢀丈٠ⶀ㙸໠ވɘའ̀눂 " i.e. compressed RC4 encrypted
valueStored = lib.LZString.compressToUTF16(lib.RC4.encrypt(JSON.stringify(data), lib.utils.encryptionSecret).toString());
valueStored = lib.LZString.compressToUTF16(
lib.RC4.encrypt(JSON.stringify(data), lib.utils.encryptionSecret).toString()
);

expect(mockLS.storage[key]).to.exist;
expect(mockLS.storage[key]).to.be.a('string');
Expand All @@ -384,4 +391,42 @@ describe('Encryption / Decryption Tests ->', () => {
lib.removeAll();
});
});

describe('AES encryption and compression and multiple storages', () => {
it('should have two parallel storage running at the same time', () => {
let data1 = [1, 2, 3];
let data2 = [3, 4, 5];
let secret1 = 'secret1';
let secret2 = 'secret2';
let realm1 = 'realm1';
let realm2 = 'realm2';
let key1 = 'key-1';
let key2 = 'key-2';

let lib1 = new SecureLS({
encodingType: 'RC4', isCompression: true, encryptionSecret: secret1, encryptionNamespace: realm1
});
let lib2 = new SecureLS({
encodingType: 'RC4', isCompression: true, encryptionSecret: secret2, encryptionNamespace: realm2
});

lib1.ls = mockStorage;
lib2.ls = mockStorage;

lib1.set(key1, data1);
lib2.set(key2, data2);

expect(lib1.get(key1)).to.eql(data1);
expect(lib2.get(key2)).to.eql(data2);

let error = null;

try {
lib1.get(key2);
} catch (e) {
error = e;
}
expect(error).to.not.eql(null);
});
});
});