hexo-blog-encrypt 3.1.9 → 4.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.
package/lib/hbe.js DELETED
@@ -1,297 +0,0 @@
1
- (() => {
2
- 'use strict';
3
-
4
- const cryptoObj = window.crypto || window.msCrypto;
5
- const storage = window.localStorage;
6
-
7
- const storageName = 'hexo-blog-encrypt:#' + window.location.pathname;
8
- const keySalt = textToArray('hexo-blog-encrypt的作者们都是大帅比!');
9
- const ivSalt = textToArray('hexo-blog-encrypt是地表最强Hexo加密插件!');
10
-
11
- // As we can't detect the wrong password with AES-CBC,
12
- // so adding an empty div and check it when decrption.
13
- const knownPrefix = "<hbe-prefix></hbe-prefix>";
14
-
15
- const mainElement = document.getElementById('hexo-blog-encrypt');
16
- const wrongPassMessage = mainElement.dataset['wpm'];
17
- const wrongHashMessage = mainElement.dataset['whm'];
18
- const dataElement = mainElement.getElementsByTagName('script')['hbeData'];
19
- const encryptedData = dataElement.innerText;
20
- const HmacDigist = dataElement.dataset['hmacdigest'];
21
-
22
- function hexToArray(s) {
23
- return new Uint8Array(s.match(/[\da-f]{2}/gi).map((h => {
24
- return parseInt(h, 16);
25
- })));
26
- }
27
-
28
- function textToArray(s) {
29
- var i = s.length;
30
- var n = 0;
31
- var ba = new Array()
32
-
33
- for (var j = 0; j < i;) {
34
- var c = s.codePointAt(j);
35
- if (c < 128) {
36
- ba[n++] = c;
37
- j++;
38
- } else if ((c > 127) && (c < 2048)) {
39
- ba[n++] = (c >> 6) | 192;
40
- ba[n++] = (c & 63) | 128;
41
- j++;
42
- } else if ((c > 2047) && (c < 65536)) {
43
- ba[n++] = (c >> 12) | 224;
44
- ba[n++] = ((c >> 6) & 63) | 128;
45
- ba[n++] = (c & 63) | 128;
46
- j++;
47
- } else {
48
- ba[n++] = (c >> 18) | 240;
49
- ba[n++] = ((c >> 12) & 63) | 128;
50
- ba[n++] = ((c >> 6) & 63) | 128;
51
- ba[n++] = (c & 63) | 128;
52
- j += 2;
53
- }
54
- }
55
- return new Uint8Array(ba);
56
- }
57
-
58
- function arrayBufferToHex(arrayBuffer) {
59
- if (typeof arrayBuffer !== 'object' || arrayBuffer === null || typeof arrayBuffer.byteLength !== 'number') {
60
- throw new TypeError('Expected input to be an ArrayBuffer')
61
- }
62
-
63
- var view = new Uint8Array(arrayBuffer)
64
- var result = ''
65
- var value
66
-
67
- for (var i = 0; i < view.length; i++) {
68
- value = view[i].toString(16)
69
- result += (value.length === 1 ? '0' + value : value)
70
- }
71
-
72
- return result
73
- }
74
-
75
- async function getExecutableScript(oldElem) {
76
- let out = document.createElement('script');
77
- const attList = ['type', 'text', 'src', 'crossorigin', 'defer', 'referrerpolicy'];
78
- attList.forEach((att) => {
79
- if (oldElem[att])
80
- out[att] = oldElem[att];
81
- })
82
-
83
- return out;
84
- }
85
-
86
- async function convertHTMLToElement(content) {
87
- let out = document.createElement('div');
88
- out.innerHTML = content;
89
- out.querySelectorAll('script').forEach(async (elem) => {
90
- elem.replaceWith(await getExecutableScript(elem));
91
- });
92
-
93
- return out;
94
- }
95
-
96
- function getKeyMaterial(password) {
97
- let encoder = new TextEncoder();
98
- return cryptoObj.subtle.importKey(
99
- 'raw',
100
- encoder.encode(password),
101
- {
102
- 'name': 'PBKDF2',
103
- },
104
- false,
105
- [
106
- 'deriveKey',
107
- 'deriveBits',
108
- ]
109
- );
110
- }
111
-
112
- function getHmacKey(keyMaterial) {
113
- return cryptoObj.subtle.deriveKey({
114
- 'name': 'PBKDF2',
115
- 'hash': 'SHA-256',
116
- 'salt': keySalt.buffer,
117
- 'iterations': 1024
118
- }, keyMaterial, {
119
- 'name': 'HMAC',
120
- 'hash': 'SHA-256',
121
- 'length': 256,
122
- }, true, [
123
- 'verify',
124
- ]);
125
- }
126
-
127
- function getDecryptKey(keyMaterial) {
128
- return cryptoObj.subtle.deriveKey({
129
- 'name': 'PBKDF2',
130
- 'hash': 'SHA-256',
131
- 'salt': keySalt.buffer,
132
- 'iterations': 1024,
133
- }, keyMaterial, {
134
- 'name': 'AES-CBC',
135
- 'length': 256,
136
- }, true, [
137
- 'decrypt',
138
- ]);
139
- }
140
-
141
- function getIv(keyMaterial) {
142
- return cryptoObj.subtle.deriveBits({
143
- 'name': 'PBKDF2',
144
- 'hash': 'SHA-256',
145
- 'salt': ivSalt.buffer,
146
- 'iterations': 512,
147
- }, keyMaterial, 16 * 8);
148
- }
149
-
150
- async function verifyContent(key, content) {
151
- const encoder = new TextEncoder();
152
- const encoded = encoder.encode(content);
153
-
154
- let signature = hexToArray(HmacDigist);
155
-
156
- const result = await cryptoObj.subtle.verify({
157
- 'name': 'HMAC',
158
- 'hash': 'SHA-256',
159
- }, key, signature, encoded);
160
- console.log(`Verification result: ${result}`);
161
- if (!result) {
162
- alert(wrongHashMessage);
163
- console.log(`${wrongHashMessage}, got `, signature, ` but proved wrong.`);
164
- }
165
- return result;
166
- }
167
-
168
- async function decrypt(decryptKey, iv, hmacKey) {
169
- let typedArray = hexToArray(encryptedData);
170
-
171
- const result = await cryptoObj.subtle.decrypt({
172
- 'name': 'AES-CBC',
173
- 'iv': iv,
174
- }, decryptKey, typedArray.buffer).then(async (result) => {
175
- const decoder = new TextDecoder();
176
- const decoded = decoder.decode(result);
177
-
178
- // check the prefix, if not then we can sure here is wrong password.
179
- if (!decoded.startsWith(knownPrefix)) {
180
- throw "Decode successfully but not start with KnownPrefix.";
181
- }
182
-
183
- const hideButton = document.createElement('button');
184
- hideButton.textContent = 'Encrypt again';
185
- hideButton.type = 'button';
186
- hideButton.classList.add("hbe-button");
187
- hideButton.addEventListener('click', () => {
188
- window.localStorage.removeItem(storageName);
189
- window.location.reload();
190
- });
191
-
192
- document.getElementById('hexo-blog-encrypt').style.display = 'inline';
193
- document.getElementById('hexo-blog-encrypt').innerHTML = '';
194
- document.getElementById('hexo-blog-encrypt').appendChild(await convertHTMLToElement(decoded));
195
- document.getElementById('hexo-blog-encrypt').appendChild(hideButton);
196
-
197
- // support html5 lazyload functionality.
198
- document.querySelectorAll('img').forEach((elem) => {
199
- if (elem.getAttribute("data-src") && !elem.src) {
200
- elem.src = elem.getAttribute('data-src');
201
- }
202
- });
203
-
204
- // support theme-next refresh
205
- window.NexT && NexT.boot && typeof NexT.boot.refresh === 'function' && NexT.boot.refresh();
206
-
207
- // TOC part
208
- var tocDiv = document.getElementById("toc-div");
209
- if (tocDiv) {
210
- tocDiv.style.display = 'inline';
211
- }
212
-
213
- var tocDivs = document.getElementsByClassName('toc-div-class');
214
- if (tocDivs && tocDivs.length > 0) {
215
- for (var idx = 0; idx < tocDivs.length; idx++) {
216
- tocDivs[idx].style.display = 'inline';
217
- }
218
- }
219
-
220
- // trigger event
221
- var event = new Event('hexo-blog-decrypt');
222
- window.dispatchEvent(event);
223
-
224
- return await verifyContent(hmacKey, decoded);
225
- }).catch((e) => {
226
- alert(wrongPassMessage);
227
- console.log(e);
228
- return false;
229
- });
230
-
231
- return result;
232
-
233
- }
234
-
235
- function hbeLoader() {
236
-
237
- const oldStorageData = JSON.parse(storage.getItem(storageName));
238
-
239
- if (oldStorageData) {
240
- console.log(`Password got from localStorage(${storageName}): `, oldStorageData);
241
-
242
- const sIv = hexToArray(oldStorageData.iv).buffer;
243
- const sDk = oldStorageData.dk;
244
- const sHmk = oldStorageData.hmk;
245
-
246
- cryptoObj.subtle.importKey('jwk', sDk, {
247
- 'name': 'AES-CBC',
248
- 'length': 256,
249
- }, true, [
250
- 'decrypt',
251
- ]).then((dkCK) => {
252
- cryptoObj.subtle.importKey('jwk', sHmk, {
253
- 'name': 'HMAC',
254
- 'hash': 'SHA-256',
255
- 'length': 256,
256
- }, true, [
257
- 'verify',
258
- ]).then((hmkCK) => {
259
- decrypt(dkCK, sIv, hmkCK).then((result) => {
260
- if (!result) {
261
- storage.removeItem(storageName);
262
- }
263
- });
264
- });
265
- });
266
- }
267
-
268
- mainElement.addEventListener('keydown', async (event) => {
269
- if (event.isComposing || event.keyCode === 13) {
270
- const password = document.getElementById('hbePass').value;
271
- const keyMaterial = await getKeyMaterial(password);
272
- const hmacKey = await getHmacKey(keyMaterial);
273
- const decryptKey = await getDecryptKey(keyMaterial);
274
- const iv = await getIv(keyMaterial);
275
-
276
- decrypt(decryptKey, iv, hmacKey).then((result) => {
277
- console.log(`Decrypt result: ${result}`);
278
- if (result) {
279
- cryptoObj.subtle.exportKey('jwk', decryptKey).then((dk) => {
280
- cryptoObj.subtle.exportKey('jwk', hmacKey).then((hmk) => {
281
- const newStorageData = {
282
- 'dk': dk,
283
- 'iv': arrayBufferToHex(iv),
284
- 'hmk': hmk,
285
- };
286
- storage.setItem(storageName, JSON.stringify(newStorageData));
287
- });
288
- });
289
- }
290
- });
291
- }
292
- });
293
- }
294
-
295
- hbeLoader();
296
-
297
- })();