secure-ls2 2.1.9 → 2.1.10

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.
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * secure-ls2 - v2.1.9
2
+ * secure-ls2 - v2.1.10
3
3
  * URL - https://github.com/softvar/secure-ls
4
4
  *
5
5
  * The MIT License (MIT)
@@ -41,13 +41,20 @@ __webpack_require__.r(__webpack_exports__);
41
41
  /* harmony export */ __webpack_require__.d(__webpack_exports__, {
42
42
  /* harmony export */ "default": () => (__WEBPACK_DEFAULT_EXPORT__)
43
43
  /* harmony export */ });
44
+ const BASE64_KEY_STR = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
45
+ const BASE64_PATTERN = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
44
46
  const Base64 = {
45
- _keyStr: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
47
+ _keyStr: BASE64_KEY_STR,
46
48
  encode: function (e) {
47
- let t = '';
49
+ const keyStr = this._keyStr;
50
+ const output = [];
48
51
  let n, r, i, s, o, u, a;
49
52
  let f = 0;
53
+ let outputIndex = 0;
50
54
  e = Base64._utf8Encode(e);
55
+ if (keyStr === BASE64_KEY_STR && typeof btoa === 'function') {
56
+ return btoa(e);
57
+ }
51
58
  while (f < e.length) {
52
59
  n = e.charCodeAt(f++);
53
60
  r = e.charCodeAt(f++);
@@ -61,76 +68,77 @@ const Base64 = {
61
68
  } else if (isNaN(i)) {
62
69
  a = 64;
63
70
  }
64
- t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a);
71
+ output[outputIndex++] = keyStr.charAt(s) + keyStr.charAt(o) + keyStr.charAt(u) + keyStr.charAt(a);
65
72
  }
66
- return t;
73
+ return output.join('');
67
74
  },
68
75
  decode: function (e) {
69
- let t = '';
76
+ const keyStr = this._keyStr;
77
+ const output = [];
70
78
  let n, r, i;
71
79
  let s, o, u, a;
72
80
  let f = 0;
81
+ let outputIndex = 0;
73
82
  e = e.replace(/[^A-Za-z0-9+/=]/g, '');
83
+ if (keyStr === BASE64_KEY_STR && typeof atob === 'function' && BASE64_PATTERN.test(e)) {
84
+ return Base64._utf8Decode(atob(e));
85
+ }
74
86
  while (f < e.length) {
75
- s = this._keyStr.indexOf(e.charAt(f++));
76
- o = this._keyStr.indexOf(e.charAt(f++));
77
- u = this._keyStr.indexOf(e.charAt(f++));
78
- a = this._keyStr.indexOf(e.charAt(f++));
87
+ s = keyStr.indexOf(e.charAt(f++));
88
+ o = keyStr.indexOf(e.charAt(f++));
89
+ u = keyStr.indexOf(e.charAt(f++));
90
+ a = keyStr.indexOf(e.charAt(f++));
79
91
  n = s << 2 | o >> 4;
80
92
  r = (o & 15) << 4 | u >> 2;
81
93
  i = (u & 3) << 6 | a;
82
- t = t + String.fromCharCode(n);
83
94
  if (u !== 64) {
84
- t = t + String.fromCharCode(r);
85
- }
86
- if (a !== 64) {
87
- t = t + String.fromCharCode(i);
95
+ output[outputIndex++] = a !== 64 ? String.fromCharCode(n, r, i) : String.fromCharCode(n, r);
96
+ } else {
97
+ output[outputIndex++] = String.fromCharCode(n);
88
98
  }
89
99
  }
90
- t = Base64._utf8Decode(t);
91
- return t;
100
+ return Base64._utf8Decode(output.join(''));
92
101
  },
93
102
  _utf8Encode: function (e) {
94
103
  e = e.replace(/\r\n/g, '\n');
95
- let t = '';
104
+ const output = [];
105
+ let outputIndex = 0;
96
106
  for (let n = 0; n < e.length; n++) {
97
107
  let r = e.charCodeAt(n);
98
108
  if (r < 128) {
99
- t += String.fromCharCode(r);
109
+ output[outputIndex++] = e.charAt(n);
100
110
  } else if (r > 127 && r < 2048) {
101
- t += String.fromCharCode(r >> 6 | 192);
102
- t += String.fromCharCode(r & 63 | 128);
111
+ output[outputIndex++] = String.fromCharCode(r >> 6 | 192, r & 63 | 128);
103
112
  } else {
104
- t += String.fromCharCode(r >> 12 | 224);
105
- t += String.fromCharCode(r >> 6 & 63 | 128);
106
- t += String.fromCharCode(r & 63 | 128);
113
+ output[outputIndex++] = String.fromCharCode(r >> 12 | 224, r >> 6 & 63 | 128, r & 63 | 128);
107
114
  }
108
115
  }
109
- return t;
116
+ return output.join('');
110
117
  },
111
118
  _utf8Decode: function (e) {
112
- let t = '';
119
+ const output = [];
113
120
  let n = 0;
114
121
  let r, c2, c3;
122
+ let outputIndex = 0;
115
123
 
116
124
  // r = c2 = 0;
117
125
  while (n < e.length) {
118
126
  r = e.charCodeAt(n);
119
127
  if (r < 128) {
120
- t += String.fromCharCode(r);
128
+ output[outputIndex++] = e.charAt(n);
121
129
  n++;
122
130
  } else if (r > 191 && r < 224) {
123
131
  c2 = e.charCodeAt(n + 1);
124
- t += String.fromCharCode((r & 31) << 6 | c2 & 63);
132
+ output[outputIndex++] = String.fromCharCode((r & 31) << 6 | c2 & 63);
125
133
  n += 2;
126
134
  } else {
127
135
  c2 = e.charCodeAt(n + 1);
128
136
  c3 = e.charCodeAt(n + 2);
129
- t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
137
+ output[outputIndex++] = String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
130
138
  n += 3;
131
139
  }
132
140
  }
133
- return t;
141
+ return output.join('');
134
142
  }
135
143
  };
136
144
  /* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (Base64);
@@ -217,7 +225,7 @@ class SecureLS {
217
225
  this.init();
218
226
  }
219
227
  init() {
220
- let metaData = this.getMetaData();
228
+ const metaData = this.getMetaData();
221
229
  this._isBase64 = this._isBase64EncryptionType();
222
230
  this._isAES = this._isAESEncryptionType();
223
231
  this._isDES = this._isDESEncryptionType();
@@ -246,22 +254,23 @@ class SecureLS {
246
254
  _isDataCompressionEnabled() {
247
255
  return this.config.isCompression;
248
256
  }
257
+ _isDataEncryptionEnabled() {
258
+ return this._isAES || this._isDES || this._isRabbit || this._isRC4;
259
+ }
249
260
  getEncryptionSecret(key) {
250
- let metaData = this.getMetaData();
251
- let obj = _utils__WEBPACK_IMPORTED_MODULE_2__["default"].getObjectFromKey(metaData.keys, key);
252
- if (!obj) {
261
+ if (!this._isDataEncryptionEnabled()) {
253
262
  return;
254
263
  }
255
- if (this._isAES || this._isDES || this._isRabbit || this._isRC4) {
256
- if (typeof this.config.encryptionSecret === 'undefined') {
257
- this.encryptionSecret = obj.s;
258
- if (!this.encryptionSecret) {
259
- this.encryptionSecret = _utils__WEBPACK_IMPORTED_MODULE_2__["default"].generateSecretKey();
260
- this.setMetaData();
261
- }
262
- } else {
263
- this.encryptionSecret = this.config.encryptionSecret || obj.s || '';
264
+ const metaData = this.getMetaData();
265
+ const obj = _utils__WEBPACK_IMPORTED_MODULE_2__["default"].getObjectFromKey(metaData.keys, key);
266
+ if (typeof this.config.encryptionSecret === 'undefined') {
267
+ this.encryptionSecret = obj.s;
268
+ if (!this.encryptionSecret) {
269
+ this.encryptionSecret = _utils__WEBPACK_IMPORTED_MODULE_2__["default"].generateSecretKey();
270
+ this.setMetaData();
264
271
  }
272
+ } else {
273
+ this.encryptionSecret = this.config.encryptionSecret || obj.s || '';
265
274
  }
266
275
  }
267
276
  getEncryptionType() {
@@ -275,7 +284,7 @@ class SecureLS {
275
284
  this.storage.setItem(key, data);
276
285
  }
277
286
  setMetaData() {
278
- let dataToStore = this.processData({
287
+ const dataToStore = this.processData({
279
288
  keys: this.allKeys
280
289
  }, true);
281
290
 
@@ -326,7 +335,7 @@ class SecureLS {
326
335
 
327
336
  // PUBLIC APIs
328
337
  getAllKeys() {
329
- let data = this.getMetaData();
338
+ const data = this.getMetaData();
330
339
  return _utils__WEBPACK_IMPORTED_MODULE_2__["default"].extractKeyNames(data) || [];
331
340
  }
332
341
  get(key, isAllKeysData) {
@@ -404,8 +413,8 @@ class SecureLS {
404
413
  this.storage.removeItem(key);
405
414
  }
406
415
  removeAll() {
407
- let keys = this.getAllKeys();
408
- for (let i = 0; i < keys.length; i++) {
416
+ const keys = this.getAllKeys();
417
+ for (let i = 0, length = keys.length; i < length; i++) {
409
418
  this.storage.removeItem(keys[i]);
410
419
  }
411
420
  this.storage.removeItem(this.metaKey);
@@ -596,7 +605,8 @@ const utils = {
596
605
  }) => k);
597
606
  },
598
607
  isKeyPresent: (allKeys = [], key) => {
599
- return allKeys.some(item => String(item.k) === String(key));
608
+ const keyName = String(key);
609
+ return allKeys.some(item => String(item.k) === keyName);
600
610
  },
601
611
  removeFromKeysList: (allKeys = [], key) => {
602
612
  const index = allKeys.findIndex(item => item.k === key);