-
Notifications
You must be signed in to change notification settings - Fork 0
/
web-storage.js
68 lines (44 loc) · 1.33 KB
/
web-storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
WebStorage v1.1.0
(c) 2016-2017 by - Anas KVC. All rights reserved.
https://exprotocol.com/
*/
var Storage = function(encrptionKey) {
this.encrptionKey = encrptionKey || null;
}
Storage.prototype.encrypt = function(data) {
return CryptoJS.AES.encrypt(data, this.encrptionKey).toString();
}
Storage.prototype.decrypt = function(data) {
return CryptoJS.AES.decrypt(data, this.encrptionKey).toString(CryptoJS.enc.Utf8);
}
Storage.prototype.create = function(name, data) {
if (localStorage.getItem(name) === null) {
data = JSON.stringify(data) || "";
if(this.encrptionKey!= null)
data = this.encrypt(data);
localStorage.setItem(name, data);
}
}
Storage.prototype.drop = function(name)
{
if (localStorage.getItem(name) != null)
localStorage.removeItem(name)
}
Storage.prototype.get = function(name) {
var data = localStorage.getItem(name);
if (data != null)
{
if(this.encrptionKey!= null)
data = this.decrypt(data);
return JSON.parse(data);
}
}
Storage.prototype.update = function(name, data) {
if (localStorage.getItem(name) != null) {
data = JSON.stringify(data) || "";
if(this.encrptionKey!= null)
data = this.encrypt(data);
localStorage.setItem(name, data);
}
}