-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsecure-storage.js
More file actions
85 lines (61 loc) · 1.51 KB
/
secure-storage.js
File metadata and controls
85 lines (61 loc) · 1.51 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var Generator = require('generate-js');
var SecureStorage = Generator.generate(
function SecureStorage(storage, options) {
var _ = this;
_.storage = storage;
if (options) {
_.hash = options.hash;
_.encrypt = options.encrypt;
_.decrypt = options.decrypt;
}
}
);
function through(data) {
return data;
}
SecureStorage.definePrototype({
hash: through,
encrypt: through,
decrypt: through,
}, {
writable: true
});
SecureStorage.definePrototype({
getItem: function getItem(key) {
var _ = this;
key = _.hash(key);
var value = _.storage.getItem(key);
if (typeof value !== 'string') {
return value;
}
value = _.decrypt(value);
return JSON.parse(value);
},
setItem: function setItem(key, value) {
var _ = this;
key = _.hash(key);
value = JSON.stringify(value);
value = _.encrypt(value);
return _.storage.setItem(key, value);
},
removeItem: function removeItem(key) {
var _ = this;
key = _.hash(key);
return _.storage.removeItem(key);
},
clear: function clear() {
var _ = this;
return _.storage.clear();
},
key: function key(id) {
var _ = this;
return _.storage.key(id);
},
length: {
get: function getLength() {
var _ = this;
return _.storage.length;
}
}
});
module.exports = SecureStorage;