hexo-blog-encrypt 4.1.0 → 4.2.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/ReadMe.md +43 -338
- package/ReadMe.zh.md +43 -317
- package/lib/hbe.blink.html +9 -8
- package/lib/hbe.bundle.js +1 -1
- package/lib/hbe.bundle.js.map +4 -4
- package/lib/hbe.card.html +24 -0
- package/lib/hbe.default.html +9 -8
- package/lib/hbe.flip.html +9 -8
- package/lib/hbe.minimal.html +24 -0
- package/lib/hbe.shrink.html +8 -8
- package/lib/hbe.style.css +549 -849
- package/lib/hbe.surge.html +14 -11
- package/lib/hbe.terminal.html +24 -0
- package/lib/hbe.up.html +9 -8
- package/lib/hbe.wave.html +13 -11
- package/lib/hbe.xray.html +14 -12
- package/package.json +9 -2
- package/src/server/config.js +46 -24
- package/src/server/crypto.js +49 -57
- package/src/server/format.js +77 -0
- package/src/server/generator.js +26 -65
- package/src/server/index.js +9 -11
package/lib/hbe.surge.html
CHANGED
|
@@ -11,17 +11,20 @@
|
|
|
11
11
|
</script>
|
|
12
12
|
<div class="hbe hbe-content">
|
|
13
13
|
<form id="hbeForm" class="hbe hbe-form">
|
|
14
|
-
<div class="hbe hbe-input hbe-input-surge">
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
<
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
14
|
+
<div class="hbe hbe-input hbe-input-surge hbe-classic">
|
|
15
|
+
<input class="hbe hbe-input-field hbe-input-field-surge" type="password" id="hbePass">
|
|
16
|
+
<label class="hbe hbe-input-label hbe-input-label-surge" for="hbePass">
|
|
17
|
+
<span class="hbe hbe-input-label-content hbe-input-label-content-surge">{{hbeMessage}}</span>
|
|
18
|
+
<span class="hbe-roll" aria-hidden="true">{{hbeMessage}}</span>
|
|
19
|
+
</label>
|
|
20
|
+
<span class="hbe-track" aria-hidden="true">
|
|
21
|
+
<svg aria-hidden="true" focusable="false" class="hbe hbe-graphic hbe-graphic-surge" width="300%" height="100%" viewBox="0 0 1200 60" preserveAspectRatio="none">
|
|
22
|
+
<path d="M1200,9c0,0-305.005,0-401.001,0C733,9,675.327,4.969,598,4.969C514.994,4.969,449.336,9,400.333,9C299.666,9,0,9,0,9v43c0,0,299.666,0,400.333,0c49.002,0,114.66,3.484,197.667,3.484c77.327,0,135-3.484,200.999-3.484C894.995,52,1200,52,1200,52V9z"></path>
|
|
23
|
+
</svg>
|
|
24
|
+
</span>
|
|
25
|
+
</div>
|
|
26
|
+
<button class="hbe hbe-button{{hbeButtonClass}}" type="submit"><span class="hbe hbe-button-text">{{hbeButtonText}}</span></button>
|
|
27
|
+
<div class="hbe hbe-error" role="alert"></div>
|
|
25
28
|
</form>
|
|
26
29
|
</div>
|
|
27
30
|
</div>
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
<div class="hbe hbe-container" id="hexo-blog-encrypt"
|
|
2
|
+
data-hbe-format="{{hbeFormat}}"
|
|
3
|
+
data-wpm="{{hbeWrongPassMessage}}"
|
|
4
|
+
data-whm="{{hbeWrongHashMessage}}"
|
|
5
|
+
data-salt="{{hbeSalt}}"
|
|
6
|
+
data-nonce="{{hbeNonce}}"
|
|
7
|
+
data-kdf-iterations="{{hbeKdfIterations}}"
|
|
8
|
+
data-auto-save="{{hbeAutoSave}}">
|
|
9
|
+
<script id="hbeData" type="hbeData">
|
|
10
|
+
{{hbeEncryptedData}}
|
|
11
|
+
</script>
|
|
12
|
+
<div class="hbe hbe-content">
|
|
13
|
+
<form id="hbeForm" class="hbe hbe-form">
|
|
14
|
+
<div class="hbe hbe-input hbe-input-terminal">
|
|
15
|
+
<input class="hbe hbe-input-field hbe-input-field-terminal" type="password" id="hbePass">
|
|
16
|
+
<label class="hbe hbe-input-label hbe-input-label-terminal" for="hbePass">
|
|
17
|
+
<span class="hbe hbe-input-label-content hbe-input-label-content-terminal">{{hbeMessage}}</span>
|
|
18
|
+
</label>
|
|
19
|
+
</div>
|
|
20
|
+
<button class="hbe hbe-button{{hbeButtonClass}}" type="submit"><span class="hbe hbe-button-text">{{hbeButtonText}}</span></button>
|
|
21
|
+
<div class="hbe hbe-error" role="alert"></div>
|
|
22
|
+
</form>
|
|
23
|
+
</div>
|
|
24
|
+
</div>
|
package/lib/hbe.up.html
CHANGED
|
@@ -11,14 +11,15 @@
|
|
|
11
11
|
</script>
|
|
12
12
|
<div class="hbe hbe-content">
|
|
13
13
|
<form id="hbeForm" class="hbe hbe-form">
|
|
14
|
-
<div class="hbe hbe-input hbe-input-up">
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
<
|
|
14
|
+
<div class="hbe hbe-input hbe-input-up hbe-classic">
|
|
15
|
+
<input class="hbe hbe-input-field hbe-input-field-up" type="password" id="hbePass">
|
|
16
|
+
<label class="hbe hbe-input-label hbe-input-label-up" for="hbePass">
|
|
17
|
+
<span class="hbe hbe-input-label-content hbe-input-label-content-up">{{hbeMessage}}</span>
|
|
18
|
+
</label>
|
|
19
|
+
<span class="hbe-track" aria-hidden="true"></span>
|
|
20
|
+
</div>
|
|
21
|
+
<button class="hbe hbe-button{{hbeButtonClass}}" type="submit"><span class="hbe hbe-button-text">{{hbeButtonText}}</span></button>
|
|
22
|
+
<div class="hbe hbe-error" role="alert"></div>
|
|
22
23
|
</form>
|
|
23
24
|
</div>
|
|
24
25
|
</div>
|
package/lib/hbe.wave.html
CHANGED
|
@@ -11,17 +11,19 @@
|
|
|
11
11
|
</script>
|
|
12
12
|
<div class="hbe hbe-content">
|
|
13
13
|
<form id="hbeForm" class="hbe hbe-form">
|
|
14
|
-
<div class="hbe hbe-input hbe-input-wave">
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
14
|
+
<div class="hbe hbe-input hbe-input-wave hbe-classic">
|
|
15
|
+
<input class="hbe hbe-input-field hbe-input-field-wave" type="password" id="hbePass">
|
|
16
|
+
<label class="hbe hbe-input-label hbe-input-label-wave" for="hbePass">
|
|
17
|
+
<span class="hbe hbe-input-label-content hbe-input-label-content-wave">{{hbeMessage}}</span>
|
|
18
|
+
</label>
|
|
19
|
+
<span class="hbe-track" aria-hidden="true">
|
|
20
|
+
<svg aria-hidden="true" focusable="false" class="hbe hbe-graphic hbe-graphic-wave" width="300%" height="100%" viewBox="0 0 1200 60" preserveAspectRatio="none">
|
|
21
|
+
<path d="M0,56.5c0,0,298.666,0,399.333,0C448.336,56.5,513.994,46,597,46c77.327,0,135,10.5,200.999,10.5c95.996,0,402.001,0,402.001,0"></path>
|
|
22
|
+
</svg>
|
|
23
|
+
</span>
|
|
24
|
+
</div>
|
|
25
|
+
<button class="hbe hbe-button{{hbeButtonClass}}" type="submit"><span class="hbe hbe-button-text">{{hbeButtonText}}</span></button>
|
|
26
|
+
<div class="hbe hbe-error" role="alert"></div>
|
|
25
27
|
</form>
|
|
26
28
|
</div>
|
|
27
29
|
</div>
|
package/lib/hbe.xray.html
CHANGED
|
@@ -11,18 +11,20 @@
|
|
|
11
11
|
</script>
|
|
12
12
|
<div class="hbe hbe-content">
|
|
13
13
|
<form id="hbeForm" class="hbe hbe-form">
|
|
14
|
-
<div class="hbe hbe-input hbe-input-xray">
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
14
|
+
<div class="hbe hbe-input hbe-input-xray hbe-classic">
|
|
15
|
+
<input class="hbe hbe-input-field hbe-input-field-xray" type="password" id="hbePass">
|
|
16
|
+
<label class="hbe hbe-input-label hbe-input-label-xray" for="hbePass">
|
|
17
|
+
<span class="hbe hbe-input-label-content hbe-input-label-content-xray">{{hbeMessage}}</span>
|
|
18
|
+
</label>
|
|
19
|
+
<span class="hbe-track" aria-hidden="true">
|
|
20
|
+
<svg aria-hidden="true" focusable="false" class="hbe hbe-graphic hbe-graphic-xray" width="300%" height="100%" viewBox="0 0 1200 60" preserveAspectRatio="none">
|
|
21
|
+
<path d="M0,56.5c0,0,298.666,0,399.333,0C448.336,56.5,513.994,46,597,46c77.327,0,135,10.5,200.999,10.5c95.996,0,402.001,0,402.001,0"></path>
|
|
22
|
+
<path d="M0,2.5c0,0,298.666,0,399.333,0C448.336,2.5,513.994,13,597,13c77.327,0,135-10.5,200.999-10.5c95.996,0,402.001,0,402.001,0"></path>
|
|
23
|
+
</svg>
|
|
24
|
+
</span>
|
|
25
|
+
</div>
|
|
26
|
+
<button class="hbe hbe-button{{hbeButtonClass}}" type="submit"><span class="hbe hbe-button-text">{{hbeButtonText}}</span></button>
|
|
27
|
+
<div class="hbe hbe-error" role="alert"></div>
|
|
26
28
|
</form>
|
|
27
29
|
</div>
|
|
28
30
|
</div>
|
package/package.json
CHANGED
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"test:docs": "node --test tests/docs.test.js tests/skills.test.js",
|
|
42
42
|
"lint": "eslint ."
|
|
43
43
|
},
|
|
44
|
-
"version": "4.
|
|
44
|
+
"version": "4.2.0",
|
|
45
45
|
"engines": {
|
|
46
46
|
"node": ">=18"
|
|
47
47
|
},
|
|
@@ -56,6 +56,13 @@
|
|
|
56
56
|
"eslint": "^10.12.0",
|
|
57
57
|
"globals": "^17.13.0",
|
|
58
58
|
"hexo": "^7",
|
|
59
|
-
"hexo-renderer-marked": "^7"
|
|
59
|
+
"hexo-renderer-marked": "^7",
|
|
60
|
+
"js-yaml": "^4.1.0"
|
|
61
|
+
},
|
|
62
|
+
"overrides": {
|
|
63
|
+
"test-exclude@8.0.0": {
|
|
64
|
+
"glob": "14.0.6",
|
|
65
|
+
"minimatch": "11.0.10"
|
|
66
|
+
}
|
|
60
67
|
}
|
|
61
68
|
}
|
package/src/server/config.js
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const { checkKdf } = require('./format');
|
|
4
4
|
|
|
5
|
-
const
|
|
6
|
-
const
|
|
5
|
+
const SCRYPT = 'scrypt';
|
|
6
|
+
const SHA256 = 'pbkdf2-sha256';
|
|
7
|
+
const SHA512 = 'pbkdf2-sha512';
|
|
8
|
+
const presets = Object.freeze({
|
|
9
|
+
[SCRYPT]: Object.freeze({ algorithm: SCRYPT, N: 32768, r: 8, p: 3 }),
|
|
10
|
+
[SHA256]: Object.freeze({ algorithm: SHA256, iterations: 600000 }),
|
|
11
|
+
[SHA512]: Object.freeze({ algorithm: SHA512, iterations: 220000 }),
|
|
12
|
+
});
|
|
7
13
|
|
|
8
14
|
const DEFAULTS = Object.freeze({
|
|
9
15
|
abstract: 'Here\'s something encrypted, password is required to continue reading.',
|
|
@@ -20,9 +26,7 @@ const DEFAULTS = Object.freeze({
|
|
|
20
26
|
show: true,
|
|
21
27
|
text: 'Decrypt',
|
|
22
28
|
}),
|
|
23
|
-
kdf:
|
|
24
|
-
iterations: DEFAULT_ITERATIONS, // single server-side default (#260)
|
|
25
|
-
}),
|
|
29
|
+
kdf: presets[SCRYPT],
|
|
26
30
|
});
|
|
27
31
|
|
|
28
32
|
// Localized user-facing defaults (#259). English lives in DEFAULTS; other
|
|
@@ -89,6 +93,33 @@ function shallowPickKnown(source, knownKeys) {
|
|
|
89
93
|
return out;
|
|
90
94
|
}
|
|
91
95
|
|
|
96
|
+
// A layer can inherit costs only from the same algorithm. Bare iterations
|
|
97
|
+
// preserve the PBKDF2 hash, or select SHA-256 when switching from scrypt.
|
|
98
|
+
function kdfLayer(base, layer) {
|
|
99
|
+
if (layer == null) return base;
|
|
100
|
+
if (typeof layer !== 'object' || Array.isArray(layer)) throw new Error('kdf must be an object');
|
|
101
|
+
const iterations = 'iterations' in layer;
|
|
102
|
+
const cost = ['N', 'r', 'p'].some(key => key in layer);
|
|
103
|
+
let algorithm = base.algorithm;
|
|
104
|
+
if ('algorithm' in layer) algorithm = layer.algorithm;
|
|
105
|
+
else if (iterations) algorithm = base.algorithm === SHA512 ? SHA512 : SHA256;
|
|
106
|
+
else if (cost) algorithm = SCRYPT;
|
|
107
|
+
if (!Object.hasOwn(presets, algorithm) || (algorithm === SCRYPT ? iterations : cost)) {
|
|
108
|
+
throw new Error('conflicting or unknown KDF');
|
|
109
|
+
}
|
|
110
|
+
const preset = presets[algorithm];
|
|
111
|
+
return checkKdf({
|
|
112
|
+
...(algorithm === base.algorithm ? base : preset),
|
|
113
|
+
...shallowPickKnown(layer, Object.keys(preset)),
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// normalizeBooleans returns a fresh object; keep KDF layers out of deepMerge.
|
|
118
|
+
function withoutKdf(picked) {
|
|
119
|
+
delete picked.kdf;
|
|
120
|
+
return picked;
|
|
121
|
+
}
|
|
122
|
+
|
|
92
123
|
// Keys allowed to flow from the front-matter `post` object into `resolve()`'s
|
|
93
124
|
// deep merge. Deliberately EXCLUDES `tags`: Hexo materializes `post.tags` as a
|
|
94
125
|
// Warehouse Query containing functions/methods, which `structuredClone` cannot
|
|
@@ -197,13 +228,15 @@ function resolve(hexoConfig, postData, logger) {
|
|
|
197
228
|
const post = postData || {};
|
|
198
229
|
|
|
199
230
|
// Layer 1: defaults → hexo encrypt block (for top-level shared keys, NOT password/theme overrides per-post).
|
|
200
|
-
let merged = deepMerge(localizedDefaults(hexoConfig, post),
|
|
231
|
+
let merged = deepMerge(localizedDefaults(hexoConfig, post),
|
|
232
|
+
withoutKdf(normalizeBooleans(shallowPickKnown(hexoEncrypt, KNOWN_KEYS), 'encrypt.', log)));
|
|
201
233
|
|
|
202
234
|
// Layer 2: post front-matter wins. Use POST_KNOWN_KEYS (which excludes
|
|
203
235
|
// `tags`) to avoid pulling Hexo's Warehouse Query for `post.tags` into
|
|
204
236
|
// the deep-merge — `structuredClone` would throw DataCloneError on the
|
|
205
237
|
// function-bearing internals.
|
|
206
|
-
merged = deepMerge(merged,
|
|
238
|
+
merged = deepMerge(merged,
|
|
239
|
+
withoutKdf(normalizeBooleans(shallowPickKnown(post, POST_KNOWN_KEYS), 'front-matter ', log)));
|
|
207
240
|
|
|
208
241
|
// Deprecation: template → theme.
|
|
209
242
|
if (merged.template !== undefined && merged.template !== null) {
|
|
@@ -222,20 +255,11 @@ function resolve(hexoConfig, postData, logger) {
|
|
|
222
255
|
merged.password = resolvePasswordRef(merged.password, post.title);
|
|
223
256
|
if (merged.password === '') return null;
|
|
224
257
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
if (
|
|
229
|
-
|
|
230
|
-
`hexo-blog-encrypt: kdf.iterations must be an integer ≥ ${FLOOR.toLocaleString('en-US').replace(/,/g, '_')} ` +
|
|
231
|
-
`(got ${merged.kdf.iterations}). 100_000 is the absolute floor; ${RECOMMENDED.toLocaleString('en-US').replace(/,/g, '_')}+ is recommended (OWASP 2023).`
|
|
232
|
-
);
|
|
233
|
-
}
|
|
234
|
-
if (merged.kdf.iterations < RECOMMENDED) {
|
|
235
|
-
log.warn(
|
|
236
|
-
`hexo-blog-encrypt: kdf.iterations=${merged.kdf.iterations} is below the OWASP-recommended 600_000. ` +
|
|
237
|
-
'Consider raising it for stronger brute-force resistance.'
|
|
238
|
-
);
|
|
258
|
+
merged.kdf = { ...kdfLayer(kdfLayer(DEFAULTS.kdf, hexoEncrypt.kdf), post.kdf) };
|
|
259
|
+
const k = merged.kdf;
|
|
260
|
+
const preset = presets[k.algorithm];
|
|
261
|
+
if (k.algorithm === SCRYPT ? k.r < 8 || k.N * k.r * k.p < 655360 : k.iterations < preset.iterations) {
|
|
262
|
+
log.warn(`KDF cost below recommended ${preset.iterations || 'scrypt profile'}`);
|
|
239
263
|
}
|
|
240
264
|
|
|
241
265
|
// wrong_hash_message handling under GCM:
|
|
@@ -259,6 +283,4 @@ function resolve(hexoConfig, postData, logger) {
|
|
|
259
283
|
module.exports = {
|
|
260
284
|
resolve,
|
|
261
285
|
DEFAULTS,
|
|
262
|
-
FLOOR,
|
|
263
|
-
RECOMMENDED,
|
|
264
286
|
};
|
package/src/server/crypto.js
CHANGED
|
@@ -2,79 +2,71 @@
|
|
|
2
2
|
|
|
3
3
|
const crypto = require('node:crypto');
|
|
4
4
|
const { promisify } = require('node:util');
|
|
5
|
+
const { frameLength, checkKdf, headerFor } = require('./format');
|
|
6
|
+
const { DEFAULTS } = require('./config');
|
|
5
7
|
|
|
6
8
|
const pbkdf2 = promisify(crypto.pbkdf2);
|
|
9
|
+
const scryptAsync = promisify(crypto.scrypt);
|
|
7
10
|
|
|
8
|
-
const DEFAULT_ITERATIONS = 250000;
|
|
9
11
|
const SALT_BYTES = 32;
|
|
10
12
|
const NONCE_BYTES = 12;
|
|
11
13
|
const KEY_BYTES = 32;
|
|
12
|
-
const TAG_BYTES = 16;
|
|
13
|
-
const HASH = 'sha256';
|
|
14
|
-
const CIPHER = 'aes-256-gcm';
|
|
15
14
|
|
|
16
|
-
function
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return pbkdf2(passwordBuf, salt, iterations, KEY_BYTES, HASH);
|
|
15
|
+
function makeFrame(text) {
|
|
16
|
+
const n = Buffer.byteLength(text, 'utf8');
|
|
17
|
+
const frame = Buffer.alloc(frameLength(n));
|
|
18
|
+
frame.writeUInt32BE(n, 0);
|
|
19
|
+
frame.write(text, 4, n, 'utf8');
|
|
20
|
+
return frame;
|
|
23
21
|
}
|
|
24
22
|
|
|
25
|
-
async function
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (!Buffer.isBuffer(opts.salt) || opts.salt.length !== SALT_BYTES) {
|
|
32
|
-
throw new Error('salt must be a 32-byte Buffer');
|
|
23
|
+
async function deriveIKM(password, salt, kdf) {
|
|
24
|
+
checkKdf(kdf);
|
|
25
|
+
const pw = Buffer.from(String(password), 'utf8');
|
|
26
|
+
try {
|
|
27
|
+
if (kdf.algorithm !== 'scrypt') {
|
|
28
|
+
return await pbkdf2(pw, salt, kdf.iterations, KEY_BYTES, kdf.algorithm.slice(7));
|
|
33
29
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
salt
|
|
30
|
+
const { N, r, p } = kdf;
|
|
31
|
+
const maxmem = Math.max(32 * 2 ** 20, 256 * r * (N + p + 2));
|
|
32
|
+
return await scryptAsync(pw, salt, KEY_BYTES, { N, r, p, maxmem });
|
|
33
|
+
} finally {
|
|
34
|
+
pw.fill(0);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function seal(frame, ikm, header, salt, nonce) {
|
|
39
|
+
const aad = Buffer.concat([header, salt, nonce]);
|
|
40
|
+
const expand = label => Buffer.from(crypto.hkdfSync('sha256', ikm, aad, `hexo-blog-encrypt:v5:${label}`, KEY_BYTES));
|
|
41
|
+
const key = expand('enc');
|
|
42
|
+
try {
|
|
43
|
+
const commitment = expand('cmt');
|
|
44
|
+
const cipher = crypto.createCipheriv('aes-256-gcm', key, nonce);
|
|
45
|
+
cipher.setAAD(Buffer.concat([aad, commitment]));
|
|
46
|
+
return Buffer.concat([header, commitment, cipher.update(frame), cipher.final(), cipher.getAuthTag()]);
|
|
47
|
+
} finally {
|
|
48
|
+
key.fill(0);
|
|
37
49
|
}
|
|
38
|
-
const nonce = crypto.randomBytes(NONCE_BYTES);
|
|
39
|
-
const dk = await deriveKey(password, salt, iterations);
|
|
40
|
-
const cipher = crypto.createCipheriv(CIPHER, dk, nonce);
|
|
41
|
-
const enc = Buffer.concat([
|
|
42
|
-
cipher.update(Buffer.from(String(plaintext), 'utf8')),
|
|
43
|
-
cipher.final(),
|
|
44
|
-
]);
|
|
45
|
-
const tag = cipher.getAuthTag();
|
|
46
|
-
const ciphertext = Buffer.concat([enc, tag]);
|
|
47
|
-
return { salt, nonce, ciphertext };
|
|
48
50
|
}
|
|
49
51
|
|
|
50
|
-
async function
|
|
51
|
-
const iterations = (opts && Number.isInteger(opts.iterations) && opts.iterations > 0)
|
|
52
|
-
? opts.iterations
|
|
53
|
-
: DEFAULT_ITERATIONS;
|
|
52
|
+
async function encrypt(plaintext, password, { kdf = DEFAULTS.kdf, salt = crypto.randomBytes(SALT_BYTES) } = {}) {
|
|
54
53
|
if (!Buffer.isBuffer(salt) || salt.length !== SALT_BYTES) {
|
|
55
54
|
throw new Error('salt must be a 32-byte Buffer');
|
|
56
55
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
56
|
+
salt = Buffer.from(salt);
|
|
57
|
+
const header = headerFor(kdf);
|
|
58
|
+
const nonce = crypto.randomBytes(NONCE_BYTES);
|
|
59
|
+
const frame = makeFrame(String(plaintext));
|
|
60
|
+
try {
|
|
61
|
+
const ikm = await deriveIKM(password, salt, kdf);
|
|
62
|
+
try {
|
|
63
|
+
return { salt, nonce, ciphertext: seal(frame, ikm, header, salt, nonce) };
|
|
64
|
+
} finally {
|
|
65
|
+
ikm.fill(0);
|
|
66
|
+
}
|
|
67
|
+
} finally {
|
|
68
|
+
frame.fill(0);
|
|
62
69
|
}
|
|
63
|
-
const dk = await deriveKey(password, salt, iterations);
|
|
64
|
-
const enc = ciphertext.slice(0, ciphertext.length - TAG_BYTES);
|
|
65
|
-
const tag = ciphertext.slice(ciphertext.length - TAG_BYTES);
|
|
66
|
-
const decipher = crypto.createDecipheriv(CIPHER, dk, nonce);
|
|
67
|
-
decipher.setAuthTag(tag);
|
|
68
|
-
const out = Buffer.concat([decipher.update(enc), decipher.final()]);
|
|
69
|
-
return out.toString('utf8');
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
-
module.exports = {
|
|
73
|
-
encrypt,
|
|
74
|
-
decrypt,
|
|
75
|
-
DEFAULT_ITERATIONS,
|
|
76
|
-
SALT_BYTES,
|
|
77
|
-
NONCE_BYTES,
|
|
78
|
-
KEY_BYTES,
|
|
79
|
-
TAG_BYTES,
|
|
80
|
-
};
|
|
72
|
+
module.exports = { encrypt, seal, makeFrame, deriveIKM };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Array indices are the on-wire KDF IDs; zero is reserved.
|
|
4
|
+
const algorithms = ['', 'pbkdf2-sha256', 'pbkdf2-sha512', 'scrypt'];
|
|
5
|
+
const inRange = (x, min, max) => Number.isInteger(x) && x >= min && x <= max;
|
|
6
|
+
|
|
7
|
+
function need(ok, message) {
|
|
8
|
+
if (!ok) throw new Error(message);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function checkKdf(k) {
|
|
12
|
+
const id = algorithms.indexOf(k.algorithm);
|
|
13
|
+
need(id > 0, 'unknown KDF');
|
|
14
|
+
if (id < 3) {
|
|
15
|
+
need(inRange(k.iterations, 100000, 10000000), 'kdf.iterations must be an integer 100000–10000000');
|
|
16
|
+
} else {
|
|
17
|
+
const { N, r, p } = k;
|
|
18
|
+
const logN = Math.log2(N);
|
|
19
|
+
need(inRange(N, 8192, 262144) && Number.isInteger(logN) &&
|
|
20
|
+
inRange(r, 1, 32) && inRange(p, 1, 16) && logN < 16 * r &&
|
|
21
|
+
128 * r * N <= 128 * 2 ** 20 && N * r * p <= 2 ** 20, 'invalid scrypt cost');
|
|
22
|
+
}
|
|
23
|
+
return k;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function headerFor(k) {
|
|
27
|
+
checkKdf(k);
|
|
28
|
+
const id = algorithms.indexOf(k.algorithm);
|
|
29
|
+
const h = Uint8Array.of(72, 66, 69, 53, 5, id, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
30
|
+
const view = new DataView(h.buffer);
|
|
31
|
+
if (id === 3) {
|
|
32
|
+
h[8] = Math.log2(k.N);
|
|
33
|
+
view.setUint16(9, k.r);
|
|
34
|
+
view.setUint16(11, k.p);
|
|
35
|
+
} else {
|
|
36
|
+
view.setUint32(8, k.iterations);
|
|
37
|
+
}
|
|
38
|
+
return h;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function readHeader(h) {
|
|
42
|
+
need(h.length === 16, 'header length');
|
|
43
|
+
const view = new DataView(h.buffer, h.byteOffset, 16);
|
|
44
|
+
const algorithm = algorithms[h[5]];
|
|
45
|
+
let k;
|
|
46
|
+
if (h[5] === 3) {
|
|
47
|
+
need(inRange(h[8], 13, 18), 'scrypt log2N');
|
|
48
|
+
k = { algorithm, N: 2 ** h[8], r: view.getUint16(9), p: view.getUint16(11) };
|
|
49
|
+
} else {
|
|
50
|
+
k = { algorithm, iterations: view.getUint32(8) };
|
|
51
|
+
}
|
|
52
|
+
// Re-encoding validates costs as well as all fixed and reserved bytes.
|
|
53
|
+
need(headerFor(k).every((byte, i) => byte === h[i]), 'noncanonical header');
|
|
54
|
+
return k;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function frameLength(n) {
|
|
58
|
+
need(inRange(n, 0, 2 ** 24), 'plaintext length');
|
|
59
|
+
const length = n + 4;
|
|
60
|
+
if (length <= 256) return 256;
|
|
61
|
+
|
|
62
|
+
// Padmé: round up after keeping as many mantissa bits as the exponent's bit length.
|
|
63
|
+
const exponent = 31 - Math.clz32(length);
|
|
64
|
+
const bits = 32 - Math.clz32(exponent);
|
|
65
|
+
const mask = (1 << (exponent - bits)) - 1;
|
|
66
|
+
return (length + mask) & ~mask;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function unframe(f) {
|
|
70
|
+
const n = new DataView(f.buffer, f.byteOffset, f.byteLength).getUint32(0);
|
|
71
|
+
const canonical = frameLength(n);
|
|
72
|
+
need(n <= f.length - 4 && canonical === f.length, 'frame length');
|
|
73
|
+
need(!f.subarray(4 + n).some(byte => byte !== 0), 'padding');
|
|
74
|
+
return new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(f.subarray(4, 4 + n));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
module.exports = { checkKdf, headerFor, readHeader, frameLength, unframe };
|
package/src/server/generator.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const fs = require('node:fs');
|
|
4
4
|
const crypto = require('node:crypto');
|
|
5
|
+
const { Readable } = require('node:stream');
|
|
5
6
|
|
|
6
7
|
const HASH_BYTES = 5;
|
|
7
8
|
|
|
@@ -21,28 +22,21 @@ function linkSourcemap(bytes, mapName) {
|
|
|
21
22
|
return Buffer.from(text.replace(SOURCEMAP_COMMENT_RE, replacement), 'utf8');
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
* @param {string} opts.bundlePath Absolute path to the browser bundle JS.
|
|
40
|
-
* @param {string} opts.cssPath Absolute path to the plugin stylesheet.
|
|
41
|
-
* @param {string} [opts.sourcemapPath] Absolute path to the bundle sourcemap.
|
|
42
|
-
* Optional; if absent or file missing,
|
|
43
|
-
* no `.map` route is emitted.
|
|
44
|
-
* @returns {() => Array<{path: string, data: Function}>}
|
|
45
|
-
*/
|
|
25
|
+
const integrity = bytes => 'sha384-' + crypto.createHash('sha384').update(bytes).digest('base64');
|
|
26
|
+
|
|
27
|
+
function reader(file) {
|
|
28
|
+
let cache;
|
|
29
|
+
return () => {
|
|
30
|
+
const { mtimeMs, size } = fs.statSync(file);
|
|
31
|
+
if (!cache || cache.mtimeMs !== mtimeMs || cache.size !== size) {
|
|
32
|
+
const bytes = fs.readFileSync(file);
|
|
33
|
+
cache = { mtimeMs, size, bytes, hash: hex10(bytes) };
|
|
34
|
+
}
|
|
35
|
+
return cache;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Generated routes are ordered CSS, JS, then optional map; data() owns its generation's snapshot.
|
|
46
40
|
function createGenerator(opts) {
|
|
47
41
|
if (!opts || typeof opts.bundlePath !== 'string' || !opts.bundlePath) {
|
|
48
42
|
throw new Error('createGenerator: bundlePath is required');
|
|
@@ -57,55 +51,22 @@ function createGenerator(opts) {
|
|
|
57
51
|
? opts.sourcemapPath
|
|
58
52
|
: null;
|
|
59
53
|
|
|
60
|
-
|
|
61
|
-
// bundle file's mtime/size changes. See the in-function comment below
|
|
62
|
-
// for why this is correct.
|
|
63
|
-
let cache = null;
|
|
64
|
-
|
|
54
|
+
const bundle = reader(bundlePath), css = reader(cssPath);
|
|
65
55
|
return function generate() {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
// `hexo server` watch mode, an `npm run build` mid-session updates the
|
|
69
|
-
// mtime, invalidating the cache exactly once. This closes the
|
|
70
|
-
// hash-mismatch window between the filter (which inlines the script
|
|
71
|
-
// src) and the generator (which emits the bytes) — both now agree on
|
|
72
|
-
// the same content hash within one mtime tick.
|
|
73
|
-
const stat = fs.statSync(bundlePath);
|
|
74
|
-
if (cache === null || cache.mtimeMs !== stat.mtimeMs || cache.size !== stat.size) {
|
|
75
|
-
const bundleBytes = fs.readFileSync(bundlePath);
|
|
76
|
-
cache = {
|
|
77
|
-
mtimeMs: stat.mtimeMs,
|
|
78
|
-
size: stat.size,
|
|
79
|
-
bytes: bundleBytes,
|
|
80
|
-
hash: hex10(bundleBytes),
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
// The hash is computed over the on-disk bundle bytes; the filter and the
|
|
84
|
-
// generator share this instance, so both always agree on the route name.
|
|
85
|
-
const { bytes: rawBundleBytes, hash } = cache;
|
|
86
|
-
const jsRoutePath = `lib/hbe.${hash}.js`;
|
|
56
|
+
const { bytes: rawBundleBytes, hash } = bundle();
|
|
57
|
+
const { bytes: cssBytes, hash: cssHash } = css();
|
|
87
58
|
const hasMap = Boolean(sourcemapPath && fs.existsSync(sourcemapPath));
|
|
88
|
-
const
|
|
89
|
-
|
|
59
|
+
const digit = (parseInt(hash[9], 16) & ~1) | (hasMap ? 1 : 0);
|
|
60
|
+
const name = `hbe.${hash.slice(0, 9)}${digit.toString(16)}.js`;
|
|
61
|
+
const jsBytes = linkSourcemap(rawBundleBytes, hasMap ? name + '.map' : null);
|
|
90
62
|
const routes = [
|
|
91
|
-
{
|
|
92
|
-
|
|
93
|
-
data: () => fs.createReadStream(cssPath),
|
|
94
|
-
},
|
|
95
|
-
{
|
|
96
|
-
path: jsRoutePath,
|
|
97
|
-
data: () => bundleBytes,
|
|
98
|
-
},
|
|
63
|
+
{ path: `css/hbe.${cssHash}.css`, integrity: integrity(cssBytes), data: () => Readable.from([cssBytes]) },
|
|
64
|
+
{ path: 'lib/' + name, integrity: integrity(jsBytes), data: () => jsBytes },
|
|
99
65
|
];
|
|
100
|
-
|
|
101
66
|
if (hasMap) {
|
|
102
|
-
const
|
|
103
|
-
routes.push({
|
|
104
|
-
path: `lib/hbe.${hash}.js.map`,
|
|
105
|
-
data: () => mapBytes,
|
|
106
|
-
});
|
|
67
|
+
const bytes = fs.readFileSync(sourcemapPath);
|
|
68
|
+
routes.push({ path: 'lib/' + name + '.map', integrity: integrity(bytes), data: () => bytes });
|
|
107
69
|
}
|
|
108
|
-
|
|
109
70
|
return routes;
|
|
110
71
|
};
|
|
111
72
|
}
|