-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.js
193 lines (172 loc) · 5.01 KB
/
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* @typedef {import('./cryptic.js').CrypticMethods} CrypticMethods
* @typedef {import('./cryptic.js').BufferToString} BufferToString
* @typedef {import('./cryptic.js').StringToBuffer} StringToBuffer
* @typedef {import('./cryptic.js').ToHex} ToHex
* @typedef {import('./cryptic.js').ToBytes} ToBytes
* @typedef {import('./cryptic.js').BufferToHex} BufferToHex
* @typedef {import('./cryptic.js').HexToBuffer} HexToBuffer
* @typedef {import('./cryptic.js').CrypticInstance} CrypticInstance
* @typedef {import('./cryptic.js').EncryptStr} EncryptString
* @typedef {import('./cryptic.js').BrowserSupport} BrowserSupport
* @typedef {import('./cryptic.js').Cryptic} Cryptic
*/
/**
* @typedef CrypticStorage
* @prop {BufferToString} bufferToString
* @prop {StringToBuffer} stringToBuffer
* @prop {ToHex} toHex
* @prop {ToBytes} toBytes
* @prop {BufferToHex} bufferToHex
* @prop {HexToBuffer} hexToBuffer
* @prop {EncryptedStorage} getEncryptedStorage
* @prop {EncryptedStorageFromPassword} getEncryptedStorageFromPassword
* @prop {EncryptedStorageFromCrypto} getEncryptedStorageFromCrypto
* @prop {CrypticInstance} create
* @prop {EncryptString} encryptString - Deprecated: renamed to `create` since version 0.1.0
* @prop {BrowserSupport} isBrowserSupported
*/
/**
* @async
* @callback EncryptedStorage
*
* @param {Storage} storage
* @param {String | CrypticMethods} arg1
* @param {String} arg2
*
* @returns {Promise<EncryptedStorageFromCrypto | EncryptedStorageFromPassword>}
*/
/**
* Gets encrypted storage with async getItem and setItem
*
* @async
* @callback EncryptedStorageFromCrypto
*
* @param {Storage} storage Browser storage - localStorage, sessionStorage
* @param {CrypticMethods} cryptoWrapper Crypto
*/
/**
* @async
* @callback EncryptedStorageFromPassword
*
* @param {Storage} storage
* @param {String} password
* @param {String} salt
*
* @returns {Promise<EncryptedStorageFromCrypto>}
*/
/** @type {CrypticStorage} */
// @ts-ignore
var CrypticStorage = ('object' === typeof module && exports) || {};
(function (Window, CrypticStorage) {
'use strict';
/** @type {Cryptic} */
// @ts-ignore
let { Cryptic } = Window;
CrypticStorage.getEncryptedStorageFromCrypto = async function (
storage,
cryptoWrapper,
) {
let isSupported;
/**
*
* @param {String} [key]
* @returns {ArrayBufferLike}
*/
const getIV = (key) => {
let ivk = storage.getItem(key);
if (ivk) {
ivk = ivk.split(':')[1];
return Cryptic.hexToBuffer(ivk);
}
return cryptoWrapper.getInitVector();
};
const unmodifiedFunctions = {
clear() {
storage.clear();
},
get length() {
return storage.length;
},
key(i) {
return storage.key(i);
},
};
const setBrowserSupport = async () => {
if (typeof isSupported === 'undefined') {
isSupported = await Cryptic.isBrowserSupported();
}
};
await setBrowserSupport();
return {
...storage,
async setItem(key, value) {
await setBrowserSupport();
if (isSupported) {
try {
const iv = getIV(key);
const encrypted = await cryptoWrapper.encrypt(value, iv);
storage.setItem(
key,
`${encrypted}:${Cryptic.bufferToHex(iv)}`,
);
} catch (error) {
console.error(`Cannot set encrypted value for ${key}`, error);
throw error;
}
} else {
storage.setItem(key, value); // legacy mode, no encryption
}
},
async getItem(key) {
await setBrowserSupport();
if (isSupported) {
try {
const [data, iv] = storage.getItem(key)?.split(':');
return await cryptoWrapper.decrypt(data, iv);
} catch (error) {
console.error(`Cannot get encrypted item for ${key}.`, error);
return null;
}
}
return storage.getItem(key); // legacy mode, no encryption
},
async removeItem(key) {
storage.removeItem(key);
},
...unmodifiedFunctions,
};
};
CrypticStorage.getEncryptedStorageFromPassword = async function (
storage,
password,
salt,
) {
return await CrypticStorage.getEncryptedStorageFromCrypto(
storage,
Cryptic.encryptString(password, salt),
);
};
CrypticStorage.getEncryptedStorage = async function (storage, ...args) {
const [arg1, arg2] = args;
if (typeof arg1 === 'object') {
// it is crypto object
return await CrypticStorage.getEncryptedStorageFromCrypto(storage, arg1);
}
if (typeof arg1 === 'string' && typeof arg2 === 'string') {
return await CrypticStorage.getEncryptedStorageFromPassword(
storage,
arg1,
arg2,
);
}
};
// @ts-ignore
Window.CrypticStorage = CrypticStorage;
})(
/** @type {Window} */ (globalThis?.window || globalThis || {}),
CrypticStorage
);
if ('object' === typeof module) {
module.exports = CrypticStorage;
}