Skip to content

Commit 5bffc51

Browse files
authored
Merge pull request #23 from konsultaner/master
add possibility to use two instances in parallel
2 parents b318562 + 2ef4862 commit 5bffc51

12 files changed

+148
-33
lines changed

README.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,22 @@ var ls = new SecureLS();
102102
`Contructor` accepts a configurable `Object` with all three keys being optional.
103103

104104

105-
| Config Keys | default | accepts |
106-
| --------------------- | -------------- | ----------------------------------------- |
107-
| **encodingType** | Base64 | `base64`/`aes`/`des`/`rabbit`/`rc4`/`''` |
108-
| **isCompression** | `true` | `true`/`false` |
109-
| **encryptionSecret** | PBKDF2 value | String |
110-
111-
**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.
112-
105+
| Config Keys | default | accepts |
106+
| ------------------------ | -------------- | ----------------------------------------- |
107+
| **encodingType** | Base64 | `base64`/`aes`/`des`/`rabbit`/`rc4`/`''` |
108+
| **isCompression** | `true` | `true`/`false` |
109+
| **encryptionSecret** | PBKDF2 value | String |
110+
| **encryptionNamespace** | null | String |
111+
112+
**Note:** `encryptionSecret` will only be used for the Encryption and Decryption of data
113+
with `AES`, `DES`, `RC4`, `RABBIT`, and the library will discard it if no encoding / Base64
114+
encoding method is choosen.
115+
116+
`encryptionNamespace` is used to make multiple instances with different `encryptionSecret`
117+
and/or different `encryptionSecret` possible.
118+
119+
var ls1 = new SecureLS({encodingType: 'des', encryptionSecret: 'my-secret-key-1'});
120+
var ls2 = new SecureLS({encodingType: 'aes', encryptionSecret: 'my-secret-key-2'});
113121

114122
**Examples:**
115123

dist/secure-ls.d.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
export interface SecureLS {
2-
new(config?: { isCompression: boolean, encodingType: string, encryptionSecret: string }): SecureLS;
1+
export = SecureLS;
2+
3+
import * as LZString from 'lz-string';
4+
import {CipherHelper, Encoder} from 'crypto-js';
5+
6+
declare class SecureLS {
7+
constructor(config?: { isCompression?: boolean, encodingType?: string, encryptionSecret?: string , encryptionNamespace?: string });
38
getEncryptionSecret(): string;
49
get(key: string, isAllKeysData?: boolean): any;
510
getDataFromLocalStorage(key: string): string | null;
@@ -13,4 +18,24 @@ export interface SecureLS {
1318
processData(data: any | string, isAllKeysData: boolean): string;
1419
setMetaData(): void;
1520
getMetaData(): { keys: string[] };
21+
22+
_name: 'secure-ls';
23+
Base64: SecureLS.Base64;
24+
LZString: LZString.LZStringStatic;
25+
AES: CipherHelper;
26+
DES: CipherHelper;
27+
RABBIT: CipherHelper;
28+
RC4: CipherHelper;
29+
enc: {
30+
Latin1: Encoder;
31+
_Utf8: Encoder;
32+
};
1633
}
34+
35+
declare namespace SecureLS{
36+
interface Base64 {
37+
_keyStr: string;
38+
encode(e: string);
39+
decode(e: string);
40+
}
41+
}

dist/secure-ls.js

Lines changed: 12 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/secure-ls.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/secure-ls.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/secure-ls.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/aes-compressed-realm.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var data = {data: [{age: 1}, {age: '2'}]};
2+
var aesCRealm1 = new SecureLS({encodingType: 'aes', encryptionSecret: 'secret1', encryptionNamespace: 'realm1'});
3+
var key1 = 'aes__compressed_1';
4+
var ae = aesCRealm1.AES.encrypt(JSON.stringify(data), '');
5+
var bde = aesCRealm1.AES.decrypt(ae.toString(), '');
6+
var de = bde.toString(aesCRealm1.enc._Utf8);
7+
var aesCRealm2 = new SecureLS({encodingType: 'aes', encryptionSecret: 'secret2', encryptionNamespace: 'realm2'});
8+
var key2 = 'aes__compressed_2';
9+
var ae2 = aesCRealm2.AES.encrypt(JSON.stringify(data), '');
10+
var bde2 = aesCRealm2.AES.decrypt(ae2.toString(), '');
11+
var de2 = bde2.toString(aesCRealm2.enc._Utf8);
12+
13+
aesCRealm1.set(key1, data);
14+
console.log('AES Compressed Realm1');
15+
console.log(localStorage.getItem(key1));
16+
console.log(aesCRealm1.get(key1));
17+
console.log('____________________________________');
18+
19+
aesCRealm2.set(key2, data);
20+
console.log('AES Compressed Realm2');
21+
console.log(localStorage.getItem(key2));
22+
console.log(aesCRealm2.get(key2));
23+
console.log('____________________________________');

example/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<script type="text/javascript" src="only-compressed.js"></script>
2525
<script type="text/javascript" src="base64-compressed.js"></script>
2626
<script type="text/javascript" src="aes-compressed.js"></script>
27+
<script type="text/javascript" src="aes-compressed-realm.js"></script>
2728
<script type="text/javascript" src="aes-uncompressed.js"></script>
2829
<script type="text/javascript" src="des-compressed.js"></script>
2930
<script type="text/javascript" src="des-uncompressed.js"></script>

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "secure-ls",
3-
"version": "1.2.4",
3+
"version": "1.2.5",
44
"description": "Secure localStorage data with high level of encryption and data compression",
55
"main": "./dist/secure-ls.js",
66
"typings": "./dist/secure-ls.d.ts",
@@ -14,6 +14,8 @@
1414
"coveralls": "cat ./coverage/lcov.info | node node_modules/.bin/coveralls"
1515
},
1616
"devDependencies": {
17+
"@types/crypto-js": "^3.1.43",
18+
"@types/lz-string": "^1.3.32",
1719
"babel": "6.3.13",
1820
"babel-core": "6.1.18",
1921
"babel-eslint": "5.0.0",

src/index.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,23 @@ export default class SecureLS {
2525

2626
this.config = {
2727
isCompression: true,
28-
encodingType: constants.EncrytionTypes.BASE64
28+
encodingType: constants.EncrytionTypes.BASE64,
29+
encryptionSecret: config.encryptionSecret,
30+
encryptionNamespace: config.encryptionNamespace
2931
};
3032
this.config.isCompression = typeof config.isCompression !== 'undefined' ?
3133
config.isCompression :
3234
true;
3335
this.config.encodingType = (typeof config.encodingType !== 'undefined' || config.encodingType === '') ?
3436
config.encodingType.toLowerCase() :
3537
constants.EncrytionTypes.BASE64;
36-
this.config.encryptionSecret = config.encryptionSecret;
3738

3839
this.ls = localStorage;
3940
this.init();
4041
};
4142

4243
init() {
43-
let metaData = this.getMetaData() || {};
44+
let metaData = this.getMetaData();
4445

4546
this.WarningEnum = this.constants.WarningEnum;
4647
this.WarningTypes = this.constants.WarningTypes;
@@ -88,7 +89,7 @@ export default class SecureLS {
8889
}
8990

9091
getEncryptionSecret(key) {
91-
let metaData = this.getMetaData() || {};
92+
let metaData = this.getMetaData();
9293
let obj = this.utils.getObjectFromKey(metaData.keys, key);
9394

9495
if (!obj) {
@@ -289,11 +290,15 @@ export default class SecureLS {
289290
}, true);
290291

291292
// Store the data to localStorage
292-
this.setDataToLocalStorage(this.utils.metaKey, dataToStore);
293+
this.setDataToLocalStorage(this.getMetaKey(), dataToStore);
293294
};
294295

295296
getMetaData() {
296-
return this.get(this.utils.metaKey, true);
297+
return this.get(this.getMetaKey(), true) || {};
297298
};
298299

300+
getMetaKey() {
301+
return this.utils.metaKey + (this.config.encryptionNamespace ? '__' + this.config.encryptionNamespace : '');
302+
}
303+
299304
};

0 commit comments

Comments
 (0)