secure-ls2 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/.eslintrc +186 -0
  2. package/.github/FUNDING.yml +6 -0
  3. package/.github/workflows/test.yml +65 -0
  4. package/.husky/commit-msg +1 -0
  5. package/.husky/pre-commit +1 -0
  6. package/.husky/pre-push +3 -0
  7. package/.prettierrc +8 -0
  8. package/CODE_OF_CONDUCT.md +76 -0
  9. package/CONTRIBUTING.md +46 -0
  10. package/LICENSE +21 -0
  11. package/README.md +350 -0
  12. package/babel.config.js +3 -0
  13. package/ci-package.json +47 -0
  14. package/commitlint.config.js +12 -0
  15. package/dist/secure-ls.js +5500 -0
  16. package/dist/secure-ls.js.map +1 -0
  17. package/dist/secure-ls.min.js +33 -0
  18. package/dist/secure-ls.min.js.map +1 -0
  19. package/dist/secure-ls2.js +5500 -0
  20. package/dist/secure-ls2.js.map +1 -0
  21. package/dist/secure-ls2.min.js +33 -0
  22. package/dist/secure-ls2.min.js.map +1 -0
  23. package/eslint.config.mjs +12 -0
  24. package/example/aes-compressed-realm.js +28 -0
  25. package/example/aes-compressed.js +12 -0
  26. package/example/aes-uncompressed.js +12 -0
  27. package/example/base64-compressed.js +12 -0
  28. package/example/custom-storage.js +29 -0
  29. package/example/des-compressed.js +12 -0
  30. package/example/des-uncompressed.js +12 -0
  31. package/example/index.html +42 -0
  32. package/example/only-base64.js +12 -0
  33. package/example/only-compressed.js +12 -0
  34. package/example/rabbit-compressed.js +12 -0
  35. package/example/rabbit-uncompressed.js +12 -0
  36. package/example/rc4-compressed.js +12 -0
  37. package/example/rc4-uncompressed.js +12 -0
  38. package/example/sessionStorage.js +13 -0
  39. package/example/standard.js +13 -0
  40. package/example/vendor/screenlog.min.js +5 -0
  41. package/jest.config.js +10 -0
  42. package/package.json +78 -0
  43. package/screenshot.png +0 -0
  44. package/types/secure-ls.d.ts +58 -0
package/README.md ADDED
@@ -0,0 +1,350 @@
1
+ # secure-ls
2
+ wuc656 自用
3
+ Secure localStorage/sessionStorage data with high level of encryption and data compression.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/secure-ls2?style=for-the-badge&color=grey&logo=npm)](https://www.npmjs.com/package/secure-ls2) [![npm](https://img.shields.io/npm/dt/secure-ls2.svg?style=for-the-badge&color=blue)](https://www.npmjs.com/package/secure-ls2)
6
+
7
+ [![CI](https://img.shields.io/github/actions/workflow/status/softvar/secure-ls/test.yml?style=for-the-badge&logo=github)](https://github.com/softvar/secure-ls/actions?query=workflow%3ACI)
8
+ [![codecov](https://img.shields.io/codecov/c/github/softvar/secure-ls?token=HxMmlpGn3k&style=for-the-badge&logo=codecov)](https://codecov.io/gh/softvar/secure-ls)
9
+
10
+ **[LIVE DEMO](http://softvar.github.io/secure-ls#live-demo)**
11
+
12
+ ## Features
13
+
14
+ - Secure data with various types of encryption including `AES`, `DES`, `Rabbit` and `RC4`. (defaults to `Base64` encoding).
15
+ - Compress data before storing it to `localStorage` to save extra bytes (defaults to `true`).
16
+ - Advanced API wrapper over `localStorage` API, providing other basic utilities.
17
+ - Save data in multiple keys inside `localStorage` and `secure-ls` will always remember it's creation.
18
+
19
+ ## Installation
20
+
21
+ Via NPM
22
+
23
+ ```bash
24
+ npm install secure-ls2
25
+ ```
26
+
27
+ Via yarn
28
+
29
+ ```bash
30
+ yarn add secure-ls
31
+ ```
32
+
33
+ ## Libraries used
34
+
35
+ - **Encryption / Decryption** using [The Cipher Algorithms](https://code.google.com/archive/p/crypto-js)
36
+
37
+ It requires secret-key for encrypting and decrypting data securely. If custom secret-key is provided as mentioned below in APIs, then the library will pick that otherwise it will generate yet another very `secure` unique password key using [PBKDF2](https://code.google.com/archive/p/crypto-js/#PBKDF2), which will be further used for future API requests.
38
+
39
+ `PBKDF2` is a password-based key derivation function. In many applications of cryptography, user security is ultimately dependent on a password, and because a password usually can't be used directly as a cryptographic key, some processing is required.
40
+
41
+ A salt provides a large set of keys for any given password, and an iteration count increases the cost of producing keys from a password, thereby also increasing the difficulty of attack.
42
+
43
+ Eg: `55e8f5585789191d350329b9ebcf2b11` and `db51d35aad96610683d5a40a70b20c39`.
44
+
45
+ For the generation of such strings, `secretPhrase` is being used and can be found in code easily but that won't make it unsecure, `PBKDF2`'s layer on top of that will handle security.
46
+
47
+ - **Compresion / Decompression** using [lz-string](https://github.com/pieroxy/lz-string)
48
+
49
+ ## Usage
50
+
51
+ - Example 1: With `default` settings i.e. `Base64` Encoding and Data Compression
52
+
53
+ ```javascript
54
+ const ls = new SecureLS();
55
+
56
+ ls.set('key1', { data: 'test' }); // set key1
57
+ ls.get('key1'); // print data
58
+ // {data: 'test'}
59
+ ```
60
+
61
+ - Example 2: With `AES` Encryption and Data Compression
62
+
63
+ ```javascript
64
+ const ls = new SecureLS({ encodingType: 'aes' });
65
+
66
+ ls.set('key1', { data: 'test' }); // set key1
67
+ ls.get('key1'); // print data
68
+ // {data: 'test'}
69
+
70
+ ls.set('key2', [1, 2, 3]); // set another key
71
+ ls.getAllKeys(); // get all keys
72
+ // ["key1", "key2"]
73
+ ls.removeAll(); // remove all keys
74
+ ```
75
+
76
+ - Example 3: With `RC4` Encryption but no Data Compression
77
+
78
+ ```javascript
79
+ const ls = new SecureLS({ encodingType: 'rc4', isCompression: false });
80
+
81
+ ls.set('key1', { data: 'test' }); // set key1
82
+ ls.get('key1'); // print data
83
+ // {data: 'test'}
84
+
85
+ ls.set('key2', [1, 2, 3]); // set another key
86
+ ls.getAllKeys(); // get all keys
87
+ // ["key1", "key2"]
88
+ ls.removeAll(); // remove all keys
89
+ ```
90
+
91
+ - Example 3: With `DES` Encryption, no Data Compression and custom secret key
92
+
93
+ ```javascript
94
+ const ls = new SecureLS({ encodingType: 'des', isCompression: false, encryptionSecret: 'my-secret-key' });
95
+
96
+ ls.set('key1', { data: 'test' }); // set key1
97
+ ls.get('key1'); // print data
98
+ // {data: 'test'}
99
+
100
+ ls.set('key2', [1, 2, 3]); // set another key
101
+ ls.getAllKeys(); // get all keys
102
+ // ["key1", "key2"]
103
+ ls.removeAll(); // remove all keys
104
+ ```
105
+
106
+ ## API Documentation
107
+
108
+ #### Create an instance / reference before using.
109
+
110
+ ```javascript
111
+ const ls = new SecureLS();
112
+ ```
113
+
114
+ `Contructor` accepts a configurable `Object` with all three keys being optional.
115
+
116
+ | Config Keys | default | accepts |
117
+ | ----------------------- | ----------------------- | ------------------------------------------------------------------------------ |
118
+ | **encodingType** | Base64 | `base64`/`aes`/`des`/`rabbit`/`rc4`/`''` |
119
+ | **isCompression** | `true` | `true`/`false` |
120
+ | **encryptionSecret** | PBKDF2 value | String |
121
+ | **encryptionNamespace** | null | String |
122
+ | **storage** | localStorage | sessionStorage/localStorage or any storage object having same methods as ls/ss |
123
+ | **metaKey** | `_secure__ls__metadata` | String |
124
+
125
+ **Note:** `encryptionSecret` will only be used for the Encryption and Decryption of data
126
+ with `AES`, `DES`, `RC4`, `RABBIT`, and the library will discard it if no encoding / Base64
127
+ encoding method is choosen.
128
+
129
+ `encryptionNamespace` is used to make multiple instances with different `encryptionSecret`
130
+ and/or different `encryptionSecret` possible.
131
+
132
+ ```javascript
133
+ const ls1 = new SecureLS({ encodingType: 'des', encryptionSecret: 'my-secret-key-1' });
134
+
135
+ const ls2 = new SecureLS({ encodingType: 'aes', encryptionSecret: 'my-secret-key-2' });
136
+ ```
137
+
138
+ **Examples:**
139
+
140
+ - No config or empty Object i.e. Default **`Base64 Encoding`** and **`Data compression`**
141
+
142
+ ```javascript
143
+ const ls = new SecureLS();
144
+ // or
145
+ const ls = new SecureLS({});
146
+ ```
147
+
148
+ - No encoding No data compression i.e. **`Normal`** way of storing data
149
+
150
+ ```javascript
151
+ const ls = new SecureLS({ encodingType: '', isCompression: false });
152
+ ```
153
+
154
+ - **`Base64`** encoding but **`no`** data compression
155
+
156
+ ```javascript
157
+ const ls = new SecureLS({ isCompression: false });
158
+ ```
159
+
160
+ - **`AES`** encryption and **`data compression`**
161
+
162
+ ```javascript
163
+ const ls = new SecureLS({ encodingType: 'aes' });
164
+ ```
165
+
166
+ - **`RC4`** encryption and **`no`** data compression
167
+
168
+ ```javascript
169
+ const ls = new SecureLS({ encodingType: 'rc4', isCompression: false });
170
+ ```
171
+
172
+ - **`RABBIT`** encryption, **`no`** data compression and `custom` encryptionSecret
173
+
174
+ ```javascript
175
+ const ls = new SecureLS({ encodingType: 'rc4', isCompression: false, encryptionSecret: 's3cr3tPa$$w0rd@123' });
176
+ ```
177
+
178
+ - Any other **storage** apart from localStorage
179
+
180
+ - Using `sessionStorage`
181
+
182
+ ```javascript
183
+ const ls = new SecureLS({ encodingType: 'aes', isCompression: true, storage: sessionStorage });
184
+ ```
185
+
186
+ - Using any storage having same methods as `localStorage/sessionStorage`
187
+
188
+ ```javascript
189
+ window.customSecureLsStore = {};
190
+ const storage = {
191
+ setItem: (key, value) => {
192
+ window.customSecureLsStore[key] = value || '';
193
+ },
194
+ getItem: (key) => {
195
+ return window.customSecureLsStore[key] || null;
196
+ },
197
+ removeItem: (key) => {
198
+ delete window.customSecureLsStore[key];
199
+ },
200
+ clear: () => {
201
+ window.customSecureLsStore = {};
202
+ },
203
+ };
204
+
205
+ const ls = new SecureLS({ encodingType: 'aes', isCompression: true, storage: storage });
206
+ ```
207
+
208
+ #### Methods
209
+
210
+ - **`set`**
211
+
212
+ Saves `data` in specifed `key` in localStorage. If the key is not provided, the library will warn. Following types of JavaScript objects are supported:
213
+
214
+ - Array
215
+ - ArrayBuffer
216
+ - Blob
217
+ - Float32Array
218
+ - Float64Array
219
+ - Int8Array
220
+ - Int16Array
221
+ - Int32Array
222
+ - Number
223
+ - Object
224
+ - Uint8Array
225
+ - Uint8ClampedArray
226
+ - Uint16Array
227
+ - Uint32Array
228
+ - String
229
+
230
+ | Parameter | Description |
231
+ | --------- | -------------------- |
232
+ | key | key to store data in |
233
+ | data | data to be stored |
234
+
235
+ ```javascript
236
+ ls.set('key-name', { test: 'secure-ls' });
237
+ ```
238
+
239
+ - **`get`**
240
+
241
+ Gets `data` back from specified `key` from the localStorage library. If the key is not provided, the library will warn.
242
+
243
+ | Parameter | Description |
244
+ | --------- | --------------------------- |
245
+ | key | key in which data is stored |
246
+
247
+ ```javascript
248
+ ls.get('key-name');
249
+ ```
250
+
251
+ - **`remove`**
252
+
253
+ Removes the value of a key from the localStorage. If the `meta key`, which stores the list of keys, is tried to be removed even if there are other keys which were created by `secure-ls` library, the library will warn for the action.
254
+
255
+ | Parameter | Description |
256
+ | --------- | ---------------------------------- |
257
+ | key | remove key in which data is stored |
258
+
259
+ ```javascript
260
+ ls.remove('key-name');
261
+ ```
262
+
263
+ - **`removeAll`**
264
+
265
+ Removes all the keys that were created by the `secure-ls` library, even the `meta key`.
266
+
267
+ ```javascript
268
+ ls.removeAll();
269
+ ```
270
+
271
+ - **`clear`**
272
+
273
+ Removes all the keys ever created for that particular domain. Remember localStorage works differently for `http` and `https` protocol;
274
+
275
+ ```javascript
276
+ ls.clear();
277
+ ```
278
+
279
+ - **`getAllKeys`**
280
+
281
+ Gets the list of keys that were created using the `secure-ls` library. Helpful when data needs to be retrieved for all the keys or when keys name are not known(dynamically created keys).
282
+
283
+ `getAllKeys()`
284
+
285
+ ```javascript
286
+ ls.getAllKeys();
287
+ ```
288
+
289
+ ## Screenshot
290
+
291
+ <img src="screenshot.png" height="800">
292
+
293
+ ## Scripts
294
+
295
+ - `npm run build` - produces production version of the library under the `dist` folder
296
+ - `npm run build-dev` - produces development version of the library and runs a watcher
297
+ - `npm run test` - well ... it runs the tests :)
298
+
299
+ ## Contributing
300
+
301
+ 1. Fork the repo on GitHub.
302
+ 2. Clone the repo on machine.
303
+ 3. Execute `npm install` and `npm run dev`.
304
+ 4. Create a new branch `<fix-typo>` and do your work.
305
+ 5. Run `npm run build` to build dist files and `npm run test` to ensure all test cases are passing.
306
+ 6. Commit your changes to the branch.
307
+ 7. Submit a Pull request.
308
+
309
+ ## Development Stack
310
+
311
+ - Webpack based `src` compilation & bundling and `dist` generation.
312
+ - ES6 as a source of writing code.
313
+ - Exports in a [umd](https://github.com/umdjs/umd) format so the library works everywhere.
314
+ - ES6 test setup with [Jest](https://jestjs.io/)
315
+ - Linting with [ESLint](http://eslint.org/).
316
+
317
+ ## Process
318
+
319
+ ```
320
+ ES6 source files
321
+ |
322
+ |
323
+ webpack
324
+ |
325
+ +--- babel, eslint
326
+ |
327
+ ready to use
328
+ library
329
+ in umd format
330
+ ```
331
+
332
+ ## Credits
333
+
334
+ Many thanks to:
335
+
336
+ - [@brix](https://github.com/brix) for the awesome **[crypto-js](https://github.com/brix/crypto-js)** library for encrypting and decrypting data securely.
337
+
338
+ - [@pieroxy](https://github.com/pieroxy) for the **[lz-string](https://github.com/pieroxy/lz-string)** js library for data compression / decompression.
339
+
340
+ ## Copyright and license
341
+
342
+ > The [MIT license](https://opensource.org/licenses/MIT) (MIT)
343
+ >
344
+ > Copyright (c) 2015-2024 Varun Malhotra
345
+ >
346
+ > Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
347
+ >
348
+ > The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
349
+ >
350
+ > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,3 @@
1
+ module.exports = {
2
+ presets: [['@babel/preset-env', { targets: { node: 'current' } }]],
3
+ };
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "secure-ls",
3
+ "description": "Secure localStorage/sessionStorage data with high level of encryption and data compression",
4
+ "main": "./dist/secure-ls.js",
5
+ "browser": "./dist/secure-ls.js",
6
+ "typings": "./types/secure-ls.d.ts",
7
+ "scripts": {
8
+ "build": "yarn build-dev && yarn build-prod",
9
+ "build-dev": "webpack --mode=development",
10
+ "build-prod": "webpack --mode=production",
11
+ "build:patch": "yarn build-dev --env type=patch npm version patch",
12
+ "build:minor": "yarn build-dev --env type=minor npm version minor",
13
+ "build:major": "yarn build-dev --env type=major npm version major",
14
+ "lint": "eslint src/*.js --fix",
15
+ "prettier": "prettier -w **/*.js *.md",
16
+ "test:dev": "jest --watch --runInBand --debug --colors --errorOnDeprecated",
17
+ "test:prod": "jest --runInBand --colors --errorOnDeprecated",
18
+ "test:coverage": "jest --coverage --coverageDirectory=coverage && cat ./coverage/lcov.info"
19
+ },
20
+ "dependencies": {
21
+ "crypto-js": "^4.2.0",
22
+ "lz-string": "^1.5.0"
23
+ },
24
+ "devDependencies": {
25
+ "@babel/core": "^7.24.5",
26
+ "@babel/preset-env": "^7.24.5",
27
+ "babel-jest": "^25.x.x",
28
+ "jest": "^25.x.x",
29
+ "jest-environment-jsdom": "^25.x.x",
30
+ "semver": "^7.6.2"
31
+ },
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "https://github.com/softvar/secure-ls.git"
35
+ },
36
+ "keywords": ["secure-ls", "localStorage", "encryption", "compression", "webpack", "es6", "umd", "commonjs"],
37
+ "author": "Varun Malhotra",
38
+ "license": "MIT",
39
+ "bugs": {
40
+ "url": "https://github.com/softvar/secure-ls/issues"
41
+ },
42
+ "homepage": "https://github.com/softvar/secure-ls",
43
+ "engines": {
44
+ "node": ">=8.0"
45
+ },
46
+ "engineStrict": true
47
+ }
@@ -0,0 +1,12 @@
1
+ module.exports = {
2
+ extends: ['@commitlint/config-conventional'],
3
+ rules: {
4
+ // TODO Add Scope Enum Here
5
+ // 'scope-enum': [2, 'always', ['yourscope', 'yourscope']],
6
+ 'type-enum': [
7
+ 2,
8
+ 'always',
9
+ ['feat', 'fix', 'docs', 'chore', 'style', 'refactor', 'ci', 'test', 'revert', 'perf', 'vercel'],
10
+ ],
11
+ },
12
+ };