hexo-blog-encrypt 4.0.2 → 4.1.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 CHANGED
@@ -1,149 +1,103 @@
1
1
  # hexo-blog-encrypt
2
2
 
3
+ > Password-protect individual Hexo posts. Content is encrypted at build time
4
+ > with AES-256-GCM and decrypted in the reader's browser with the Web Crypto
5
+ > API. No server, no plaintext in the generated HTML.
6
+
7
+ **[▶ Live demo](https://d0n9x1n.github.io/hexo-blog-encrypt/)**: every demo password is `hello`.
8
+
3
9
  [![GitHub release](https://img.shields.io/github/v/release/D0n9X1n/hexo-blog-encrypt?include_prereleases&logo=github&label=release&color=brightgreen)](https://github.com/D0n9X1n/hexo-blog-encrypt/releases)
4
10
  [![npm](https://img.shields.io/npm/v/hexo-blog-encrypt?logo=npm&color=brightgreen)](https://www.npmjs.com/package/hexo-blog-encrypt)
5
11
  [![npm downloads](https://img.shields.io/npm/dm/hexo-blog-encrypt?logo=npm&label=downloads)](https://www.npmjs.com/package/hexo-blog-encrypt)
6
12
  [![License](https://img.shields.io/npm/l/hexo-blog-encrypt?color=brightgreen)](./LICENSE)
7
13
  [![Tests](https://github.com/D0n9X1n/hexo-blog-encrypt/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/D0n9X1n/hexo-blog-encrypt/actions/workflows/test.yml)
14
+ [![Coverage](https://img.shields.io/badge/server%20coverage-100%25-brightgreen)](./docs/DEVELOPMENT.md)
8
15
  [![Live Demo](https://img.shields.io/badge/demo-online-brightgreen?logo=github)](https://d0n9x1n.github.io/hexo-blog-encrypt/)
9
-
10
- [中文说明](./ReadMe.zh.md)
11
-
12
- ## What's this
13
-
14
- - ~~First of all, the **BEST** post encryption plugin in the universe for hexo.(But what about the other plugins?)~~
15
-
16
- - It is for those who write a post, but don't want everyone to read it. Thus, password is required in certain pages to access these encrypted posts.
17
-
18
- - Encryption is simple on wordpress, emlog or other blog systems, except hexo. :(
19
-
20
- - So it's "hexo-blog-encrypt"'s time.
21
-
22
- ## Features
23
-
24
- - Once you enter the correct password, the encrypted post is decrypted client-side. By default the password is **not** persisted; opt in with `autoSave: true` (per-post or in `_config.yml`) to have a derived key cached in `localStorage` so a subsequent reload of the same page auto-decrypts. If there are scripts in the post, they will be executed once the post is decrypted.
25
-
26
- - Support preset tag-specified password.
27
-
28
- - All functions are provided by the native APIs. We use [Crypto](https://nodejs.org/dist/latest-v12.x/docs/api/crypto.html) in Node.js, and use [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API) in Browsers.
29
-
30
- - [PBKDF2](https://tools.ietf.org/html/rfc2898) with [SHA-256](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf) is used to derive keys, and we use [AES-256-GCM](https://csrc.nist.gov/publications/detail/sp/800-38d/final) for both encryption and authenticated decryption — the GCM auth tag detects any modification of the ciphertext (replacing the v3 separate HMAC step).
31
-
32
- - Promise is widely used to make sure our main procedures are asynchronous, so that there is little chance for the process to be blocked, and the experience will be more fluent.
33
-
34
- - Template theme supported, you can use [`default`, `blink`, `flip`, `shrink`, `surge`, `up`, `wave`, `xray`] to set up your template theme, and [CHECK ONLINE](https://d0n9x1n.github.io/hexo-blog-encrypt/tags/theme-demo/).
35
-
36
- - Outdated browsers may not work well. In such case, please upgrade your browser.
37
-
38
- ## Online demo
39
-
40
- - See [Demo Page](https://d0n9x1n.github.io/hexo-blog-encrypt/), **all passwords are `hello`**.
41
-
42
- ## Browser support
43
-
44
- The runtime uses Web Crypto (`crypto.subtle`), which requires a **secure
45
- context** (HTTPS or `http://localhost`). Verified:
46
-
47
- | Browser | Minimum | Notes |
48
- | --- | --- | --- |
49
- | Chromium / Edge | 92+ | Tested in CI via Playwright |
50
- | Firefox | 90+ | |
51
- | Safari (macOS / iOS) | 14+ | Manual smoke at release time |
52
-
53
- If your site is served over plain HTTP from a non-localhost host,
54
- decryption will silently fail (`crypto.subtle` is `undefined`). Always
55
- use HTTPS in production.
56
-
57
- ## Why upgrade from v3?
58
-
59
- v4 isn't a cosmetic refresh — it closes real security gaps and fixes UX
60
- problems v3 had. The headline reasons:
61
-
62
- - **Stronger crypto.** **AES-256-GCM** with a built-in authentication
63
- tag replaces v3's AES-CBC + separate HMAC. One primitive, one auth
64
- path, hardware-accelerated, and immune to a class of CBC
65
- padding-oracle and HMAC-comparison bugs.
66
- - **Per-post salt + nonce.** v3 used the same salt for every post, so
67
- the same password derived the same key everywhere. v4 derives an
68
- independent key per post — an attacker can't amortize one password's
69
- PBKDF2 work across other posts that share the password.
70
- - **Stronger KDF defaults.** v3 used **1,024** PBKDF2-SHA256 iterations
71
- — far below current recommendations and easily amortized across many
72
- guesses on modern hardware. v4 defaults to **250,000** (≈ 240× more
73
- work per guess) and recommends 600,000+ (OWASP 2023). Tunable per
74
- post via `kdf.iterations`.
75
- - **Privacy by default.** v3 always cached decrypted posts in
76
- `localStorage`, so anyone with subsequent device access could
77
- re-read them. v4 defaults to `autoSave: false`; you opt in per post
78
- or globally.
79
- - **Inline error messages.** v3 fired a `window.alert()` dialog on a
80
- wrong password — disruptive, especially on mobile. v4 shows the
81
- error inline next to the password field (`role="alert"`).
82
- - **Optional click-to-decrypt button.** Discoverable for visitors who
83
- don't realise Enter submits the form. Label is configurable.
84
- - **Smaller bundle.** Drops the bundled CryptoJS dependency in
85
- favour of the browser's native Web Crypto API. The shipped browser
86
- bundle is ≈ 6 KB minified.
87
- - **Tested.** Server suite at 100 % line / branch / function coverage
88
- + Playwright e2e across all 8 themes. v3 had effectively zero
89
- automated tests; v4 is regression-guarded on every push.
90
-
91
- If you care about offline password guessing against your generated
92
- HTML, or about shared-device exposure of cached keys — upgrade.
93
-
94
- The deeper rationale per change lives on the wiki:
95
- [Migration v3 → v4](https://github.com/D0n9X1n/hexo-blog-encrypt/wiki/Migration-v3-to-v4).
96
-
97
- ## Upgrading from v3
98
-
99
- v4 changed the wire format (AES-GCM, per-post salt + nonce). Existing
100
- encrypted output cannot be read by v4 and vice versa, so:
101
-
102
- 1. Upgrade the plugin: `npm install --save hexo-blog-encrypt@^4`.
103
- 2. Rebuild from source: `hexo clean && hexo generate`.
104
- 3. Purge your CDN if you use one (the post HTML changes byte-for-byte
105
- on every build by design — fresh nonce per encryption — but the
106
- runtime asset URL `lib/hbe.<hash>.js` is content-hashed and only
107
- changes when the bundle changes).
108
- 4. Optional: drop `wrong_hash_message` from your config and front-matter
109
- (deprecated in v4, removed in v5).
110
-
111
- No source `.md` files are touched. See [CHANGELOG.md](./CHANGELOG.md)
112
- for the full breaking-changes list and the new `decryptButton` /
113
- `autoSave` / `kdf.iterations` options.
16
+ [![Node](https://img.shields.io/node/v/hexo-blog-encrypt?logo=node.js&color=brightgreen)](./package.json)
17
+ [![Hexo](https://img.shields.io/npm/dependency-version/hexo-blog-encrypt/peer/hexo?logo=hexo&label=hexo)](https://hexo.io/)
18
+ [![Dependencies](https://img.shields.io/badge/runtime%20dependencies-0-brightgreen)](./package.json)
19
+ [![npm provenance](https://img.shields.io/badge/npm-provenance-blue?logo=npm)](https://www.npmjs.com/package/hexo-blog-encrypt#provenance)
20
+ [![GitHub stars](https://img.shields.io/github/stars/D0n9X1n/hexo-blog-encrypt?style=flat&logo=github)](https://github.com/D0n9X1n/hexo-blog-encrypt/stargazers)
21
+
22
+ English · [中文说明](./ReadMe.zh.md) · [Wiki](https://github.com/D0n9X1n/hexo-blog-encrypt/wiki) · [Releases](https://github.com/D0n9X1n/hexo-blog-encrypt/releases)
23
+
24
+ ## Contents
25
+
26
+ - [Highlights](#highlights)
27
+ - [Live demo](#live-demo)
28
+ - [Install](#install)
29
+ - [Quick start](#quick-start)
30
+ - [Configuration](#configuration)
31
+ - [Theme integration](#theme-integration)
32
+ - [Browser support](#browser-support)
33
+ - [Why upgrade from v3?](#why-upgrade-from-v3)
34
+ - [Upgrading from v3](#upgrading-from-v3)
35
+ - [Contributing](#contributing)
36
+ - [License](#license)
37
+
38
+ ## Highlights
39
+
40
+ | | |
41
+ | --- | --- |
42
+ | **Authenticated encryption** | [PBKDF2](https://www.rfc-editor.org/rfc/rfc8018)-SHA256 key derivation and [AES-256-GCM](https://csrc.nist.gov/pubs/sp/800/38/d/final). The GCM tag rejects wrong passwords and tampered ciphertext. |
43
+ | **Native crypto only** | Node.js [`crypto`](https://nodejs.org/api/crypto.html) at build time, the browser [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API) at read time. No runtime dependencies. |
44
+ | **Per-post keys** | Each post has its own 32-byte salt and 12-byte nonce, so one cracked post does not speed up attacks on the others. |
45
+ | **Front-matter or tag passwords** | Set `password:` on a post, or map tags to passwords in `_config.yml`. |
46
+ | **Eight themes** | `default`, `blink`, `flip`, `shrink`, `surge`, `up`, `wave`, `xray`. Add your own by dropping in one HTML file. |
47
+ | **Scripts keep working** | Scripts inside a decrypted post run in document order. A `hexo-blog-decrypt` event fires when the post is revealed. |
48
+ | **Privacy by default** | Passwords and keys are not stored unless you opt in with `autoSave`. |
49
+ | **Tested** | 100% server line, branch and function coverage, plus Playwright end-to-end tests across every theme. |
50
+
51
+ ## Live demo
52
+
53
+ Open the **[demo site](https://d0n9x1n.github.io/hexo-blog-encrypt/)**. All passwords are `hello`.
54
+
55
+ - [Theme gallery](https://d0n9x1n.github.io/hexo-blog-encrypt/tags/theme-demo/)
56
+ - [Decrypt callback](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/callback/)
57
+ - [Script loading order and cached keys](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/script-order/)
114
58
 
115
59
  ## Install
116
60
 
117
- - `npm install --save hexo-blog-encrypt`
61
+ ```bash
62
+ npm install --save hexo-blog-encrypt
63
+ ```
118
64
 
119
- - or `yarn add hexo-blog-encrypt` (require [Yarn](https://yarnpkg.com/en/))
65
+ Requires Node.js 18 or later and Hexo 5 or later. The package is published to
66
+ npm with [provenance](https://docs.npmjs.com/generating-provenance-statements)
67
+ and mirrored to GitHub Packages as `@d0n9x1n/hexo-blog-encrypt`.
120
68
 
121
69
  ## Quick start
122
70
 
123
- - Add the "password" value to your post's front matter like:
71
+ Add a `password` to a post's front matter:
124
72
 
125
73
  ```markdown
126
-
127
74
  ---
128
75
  title: Hello World
129
76
  date: 2016-03-30 21:18:02
130
77
  password: hello
131
78
  ---
79
+ ```
80
+
81
+ Then rebuild and preview:
132
82
 
83
+ ```bash
84
+ hexo clean && hexo generate && hexo server
133
85
  ```
134
86
 
135
- - Then use `hexo clean && hexo g && hexo s` to see your encrypted post locally.
87
+ The post body is replaced by a password form. Index pages show the `abstract`
88
+ text instead of the content.
136
89
 
137
- ## Password Priority
90
+ ## Configuration
138
91
 
139
- post's front matter > encrypt tags
92
+ Settings are resolved in this order:
140
93
 
141
- ## Advanced settings
94
+ **post front matter > `_config.yml` `encrypt:` block > built-in defaults**
142
95
 
143
- ### in post's front matter
96
+ A front-matter `password` takes precedence over a tag password.
144
97
 
145
- ```markdown
98
+ ### Front matter
146
99
 
100
+ ```markdown
147
101
  ---
148
102
  title: Hello World
149
103
  tags:
@@ -153,211 +107,268 @@ password: mikemessi
153
107
  abstract: Here's something encrypted, password is required to continue reading.
154
108
  message: Hey, password is required here.
155
109
  wrong_pass_message: Oh, this is an invalid password. Check and try again, please.
156
- # Deprecated under v4 — AES-GCM unifies wrong-password and tampered-ciphertext
157
- # failures, so this option is now an alias of `wrong_pass_message`. Set
158
- # `wrong_pass_message` instead.
159
- wrong_hash_message: Oh, these decrypted content cannot be verified, but you can still have a look.
110
+ theme: xray
160
111
  ---
161
-
162
112
  ```
163
113
 
164
- ### In `_config.yml`
165
-
166
- #### Example
114
+ ### Site-wide `_config.yml`
167
115
 
168
116
  ```yaml
169
- # Security
170
117
  encrypt: # hexo-blog-encrypt
171
118
  abstract: Here's something encrypted, password is required to continue reading.
172
119
  message: Hey, password is required here.
120
+ wrong_pass_message: Oh, this is an invalid password. Check and try again, please.
121
+ theme: default
122
+ silent: false
123
+
124
+ # Encrypt every post that carries one of these tags.
173
125
  tags:
174
126
  - {name: encryptAsDiary, password: passwordA}
175
127
  - {name: encryptAsTips, password: passwordB}
176
- wrong_pass_message: Oh, this is an invalid password. Check and try again, please.
177
- # Deprecated under v4 (alias of `wrong_pass_message`). See note above.
178
- wrong_hash_message: Oh, these decrypted content cannot be verified, but you can still have a look.
179
128
 
180
- # ── v4 keys (all optional, safe defaults) ────────────────────────────
181
- # Render a visible "Decrypt" button next to the password field.
182
- # Themes that already include a button via `<%- decrypt_button %>`
183
- # ignore this option. Default: true.
129
+ # Show a visible "Decrypt" button next to the password field.
130
+ # Default: true.
184
131
  decryptButton:
185
132
  show: true
186
133
  text: Decrypt
187
134
 
188
135
  # Cache the derived AES key in localStorage so reloads skip the password
189
- # prompt. OFF by default — opt in per post via front-matter
190
- # `autoSave: true`, or globally here. Cache key is namespaced under
191
- # `hbe.v4.<post-permalink-hash>`.
136
+ # prompt. Default: false. Enable globally here or per post in front matter.
137
+ # Entries are stored under `hbe.v4.<page path>`.
192
138
  autoSave: false
193
139
 
194
- # Keep the PBKDF2 salt stable across clean static builds for the same
195
- # post permalink. Default: false. This is useful with autoSave on
196
- # Cloudflare Pages, Vercel, Netlify, GitHub Actions, and similar
197
- # platforms that rebuild generated HTML from a clean environment on
198
- # every deploy.
140
+ # Keep the PBKDF2 salt stable across clean builds for the same post
141
+ # permalink. Default: false. Useful with autoSave on Cloudflare Pages,
142
+ # Vercel, Netlify, GitHub Actions, and other platforms that rebuild
143
+ # generated HTML from a clean environment on every deploy.
199
144
  #
200
- # Only the salt is stable. The AES-GCM nonce remains random for every
145
+ # Only the salt is stable. The AES-GCM nonce stays random for every
201
146
  # encryption to avoid nonce reuse. If the post permalink changes, the
202
- # stable salt changes too, and existing localStorage cache for that page
203
- # is invalidated.
147
+ # stable salt changes too, and the page's existing localStorage cache is
148
+ # invalidated.
204
149
  stableSalt: false
205
150
 
206
- # PBKDF2 iteration count. Floor: 100_000. Recommended ≥ 600_000
207
- # (OWASP 2023 for PBKDF2-SHA256). Lower-than-recommended values log a
208
- # warning at build time. Default: 250_000.
151
+ # PBKDF2 iteration count. Floor: 100_000. Recommended: 600_000 or more
152
+ # (OWASP 2023 for PBKDF2-SHA256). Values below the recommendation log a
153
+ # build warning. Default: 250_000.
209
154
  kdf:
210
155
  iterations: 250000
211
-
212
156
  ```
213
157
 
214
158
  When `autoSave` reads a cached key, it compares the cached salt with the
215
- current page salt. A nonce change alone does not delete the cache up
216
- front; the browser tries the cached key with the current page nonce and
217
- ciphertext. If AES-GCM authentication fails, the cache entry is cleared
218
- and the visitor is asked for the password again.
159
+ current page salt. A nonce change alone does not delete the cache up front;
160
+ the browser tries the cached key with the current page nonce and ciphertext.
161
+ If AES-GCM authentication fails, the cache entry is cleared and the visitor is
162
+ asked for the password again.
163
+
164
+ **Saved keys and their exposure.** A saved key lives in `localStorage` under
165
+ `hbe.v4.<page path>` and unlocks that post on this device until it is removed.
166
+ Any script running on the same origin (your theme, analytics, comment
167
+ widgets, or an injected script) can read it, so enable `autoSave` only when
168
+ you trust every script on the site. After a post is revealed with a saved
169
+ key, a **Forget saved key** button above it deletes the entry. Turning
170
+ `autoSave` off for a post deletes that post's saved key on the next visit,
171
+ and leftover v3 `hexo-blog-encrypt:#…` entries are removed automatically.
172
+
173
+ ### Option reference
174
+
175
+ | Option | Default | Description |
176
+ | --- | --- | --- |
177
+ | `password` | none | Encrypts the post. `""` disables encryption, including tag encryption. A matching tag entry without a password fails the build. |
178
+ | `password: { env: NAME }` | none | Reads the password from environment variable `NAME` at build time (front matter, `encrypt.password` or `encrypt.tags[].password`). A missing or empty variable fails the build; the value is never logged or rendered. This keeps passwords out of config files, not out of plaintext Markdown you commit publicly. |
179
+ | `abstract` | `Here's something encrypted…` | Excerpt shown on index and archive pages. |
180
+ | `message` | `Hey, password is required here.` | Label of the password field. |
181
+ | `wrong_pass_message` | `Oh, this is an invalid password…` | Inline error after a failed attempt. |
182
+ | `theme` | `default` | One of the eight bundled themes. Unknown names fall back to `default` with a warning. |
183
+ | `tags` | `[]` | `_config.yml` only. `{name, password}` pairs for tag encryption. |
184
+ | `decryptButton.show` / `.text` | `true` / `Decrypt` | Visible submit button and its label. |
185
+ | `autoSave` | `false` | Cache the derived key in `localStorage`. |
186
+ | `stableSalt` | `false` | Derive the salt from the permalink so cached keys survive clean rebuilds. |
187
+ | `kdf.iterations` | `250000` | PBKDF2 iterations. Must be an integer of at least 100,000. |
188
+ | `silent` | `false` | Suppress build-time info logs. Warnings and errors still print. |
189
+ | `wrong_hash_message` | `wrong_pass_message` | Deprecated alias. AES-GCM reports wrong passwords and tampering the same way. |
190
+ | `template` | none | Deprecated alias of `theme`. |
219
191
 
220
- #### To disable tag encryption
192
+ Boolean options (`autoSave`, `stableSalt`, `silent`, `decryptButton.show`) must be `true` or `false`. Quoted `"true"`/`"false"` are accepted with a warning; any other value fails the build.
221
193
 
222
- Just set the `password` property in front matter to `""`.
194
+ The default `abstract`, `message`, `wrong_pass_message` and `decryptButton.text` follow the post's `lang`/`language`, else the site `language`. `zh`, `zh-CN` and `zh-Hans` use Simplified Chinese; everything else uses English. Explicit values always win.
223
195
 
224
- Example:
196
+ ### Disable tag encryption for one post
225
197
 
226
- ```
198
+ Set an empty password in that post's front matter:
199
+
200
+ ```markdown
227
201
  ---
228
- title: Callback Test
229
- date: 2019-12-21 11:54:07
202
+ title: Public post with an encrypted tag
230
203
  tags:
231
204
  - A Tag should be encrypted
232
205
  password: ""
233
206
  ---
234
-
235
- Use a "" to disable tag encryption.
236
207
  ```
237
208
 
238
- ### Config priority
209
+ ## Theme integration
239
210
 
240
- post's front matter > `_config.yml` (in the root directory) > default
211
+ ### After-decrypt event
241
212
 
242
- ### About Callback
243
- In some blogs, some elements may not be displayed normally after decryption. This is a known issue. The current solution is to check the code in your blog to learn which functions are called when the onload event occurs.
244
- Then write these code at the end of your post. For example:
213
+ The plugin dispatches `hexo-blog-decrypt` once on `window` after each post is
214
+ revealed. `event.detail.mode` is `manual` (password entered) or `cached`
215
+ (saved key), and `event.detail.element` is the revealed container (useful when
216
+ an index page shows several encrypted posts). Listen for it to re-run anything
217
+ that scans the post body:
245
218
 
219
+ ```js
220
+ window.addEventListener('hexo-blog-decrypt', function (event) {
221
+ // Reinitialize your theme's table of contents, syntax highlighting, etc.
222
+ console.log('Decrypted:', event.detail.mode, event.detail.element);
223
+ });
246
224
  ```
247
- ---
248
- title: Callback Test
249
- date: 2019-12-21 11:54:07
250
- tags:
251
- - Encrypted
252
- ---
253
225
 
254
- This is a blog to test Callback functions. You just need to add code at the end of your post as follows:
226
+ ### PJAX and SPA themes
255
227
 
256
- It will be called after the blog is decrypted.
228
+ The bundle initializes every encrypted post on the page once. It re-scans
229
+ automatically on `pjax:complete`, `pjax:success`, `swup:contentReplaced` and
230
+ `turbo:load` (dispatched on `document`). For other routers, call the idempotent
231
+ initializer after swapping content:
257
232
 
258
- <script>
259
- // add script tag and code at the end of your post
260
- alert("Hello World");
261
- </script>
233
+ ```js
234
+ window.hexoBlogEncrypt.init(); // whole document
235
+ window.hexoBlogEncrypt.init(container); // only a subtree
262
236
  ```
263
237
 
264
- Demo: [Callback Example](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/callback/).
238
+ ### Scripts inside encrypted posts
265
239
 
266
- ### After Decrypt Event
267
- Thanks to @[f-dong](https://github.com/f-dong), we now will trigger a event named `hexo-blog-decrypt`, so you can add a call back to listen to that event.
240
+ Scripts in a post run after it is decrypted. Since **4.0.3**, ordinary external
241
+ scripts load in document order before later inline scripts run, so
242
+ library-dependent code such as DPlayer initializes correctly. The decrypt event
243
+ waits until restored external scripts load, fail, or reach a **15-second
244
+ per-script deadline**. A timeout does not cancel a script; it can still execute
245
+ later. Existing `onload` and `onerror` handlers are preserved. Explicit `async`
246
+ scripts still load independently, and restored `defer` scripts do not recreate a
247
+ document parsing phase. Inline `type="module"` scripts and asynchronous work
248
+ inside scripts keep native scheduling and may finish after the decrypt event.
268
249
 
269
- ```
270
- // trigger event
271
- var event = new Event('hexo-blog-decrypt');
272
- window.dispatchEvent(event);
250
+ ```markdown
251
+ <script>
252
+ // Runs after this post is decrypted.
253
+ console.log('Hello from an encrypted post');
254
+ </script>
273
255
  ```
274
256
 
275
- ### Encrypt TOC
257
+ ### Table of contents
276
258
 
277
- If you has a post with TOC, you should change the code of your template. Take the default theme 'landscape' as an example:
259
+ **Never render an encrypted post's `post.origin` into generated HTML.** It
260
+ holds plaintext for legacy Hexo integrations. A TOC hidden with
261
+ `display:none` still publishes its headings. Search, feed and sitemap
262
+ integrations must also exclude plaintext fields from encrypted posts.
278
263
 
279
- + You should find the `article.ejs` file located at `hexo/themes/landscape/layout/_partial/article.ejs`.
280
- + Find the code like <% post.content %>, which is usually at line 30.
281
- + Replace the <% post.content %> with the following code block:
264
+ Render a server-side TOC only for unencrypted posts:
282
265
 
283
- ```
284
- <% if(post.toc == true){ %>
285
- <div id="toc-div" class="toc-article" <% if (post.encrypt == true) { %>style="display:none" <% } %>>
286
- <strong class="toc-title">Index</strong>
287
- <% if (post.encrypt == true) { %>
288
- <%- toc(post.origin, {list_number: true}) %>
289
- <% } else { %>
290
- <%- toc(post.content, {list_number: true}) %>
291
- <% } %>
292
- </div>
266
+ ```ejs
267
+ <% if (post.toc === true && !post.encrypt) { %>
268
+ <div class="toc-article"><%- toc(post.content, {list_number: true}) %></div>
293
269
  <% } %>
294
270
  <%- post.content %>
295
271
  ```
296
272
 
297
- ### Disable logging
298
- If you want to disable the logging, you can add a silent property in `_config.yml` and set it to true.
273
+ For encrypted posts, build the TOC in the browser inside the
274
+ `hexo-blog-decrypt` handler from the revealed `#hexo-blog-encrypt` content.
299
275
 
300
- ```yaml
301
- # Security
302
- encrypt: # hexo-blog-encrypt
303
- silent: true
304
- ```
276
+ ### Encryption themes
305
277
 
306
- This would disable the logging like `INFO hexo-blog-encrypt: encrypting "{Blog Name}" based on Tag: "EncryptedTag".`.
278
+ Pick a theme per post with `theme:` in front matter, or for the whole site in
279
+ `_config.yml`. Preview each one:
307
280
 
308
- ### Encrypt Theme
309
- Previously, we use `template` to let users modify their own themes. Turn out that it's not a simple way. So, we are introducing this feature here.
281
+ [default](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/default/) ·
282
+ [blink](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/blink/) ·
283
+ [flip](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/flip/) ·
284
+ [shrink](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/shrink/) ·
285
+ [surge](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/surge/) ·
286
+ [up](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/up/) ·
287
+ [wave](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/wave/) ·
288
+ [xray](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/xray/)
310
289
 
311
- You can simply use `theme` in `_config.yml` or in header like:
290
+ To add a theme, see [docs/THEMES.md](./docs/THEMES.md).
312
291
 
313
- #### In post's front matter
292
+ ## Browser support
314
293
 
315
- ```markdown
316
- ---
317
- title: Theme test
318
- date: 2019-12-21 11:54:07
319
- tags:
320
- - A Tag should be encrypted
321
- theme: xray
322
- password: "hello"
323
- ---
324
- ```
294
+ Decryption uses Web Crypto (`crypto.subtle`), which requires a **secure
295
+ context**: HTTPS or `http://localhost`.
325
296
 
326
- #### In `_config.yml`
297
+ | Browser | Minimum | Tested |
298
+ | --- | --- | --- |
299
+ | Chromium / Edge | 92+ | Automated: Playwright Chromium in CI, Hexo 7 and 8 |
300
+ | Firefox | 90+ | Automated: Playwright Firefox in CI |
301
+ | Safari (macOS) | 14+ | Automated: Playwright WebKit in CI (WebKit engine, not Safari itself) |
302
+ | Safari (iOS) | 14+ | Manual only |
327
303
 
328
- This would be a default one.
304
+ Minimum versions come from Web Crypto and language feature support; CI runs
305
+ the current Playwright builds, not these minimums.
329
306
 
330
- ```yaml
331
- # Security
332
- encrypt: # hexo-blog-encrypt
333
- abstract: Here's something encrypted, password is required to continue reading.
334
- message: Hey, password is required here.
335
- tags:
336
- - {name: encryptAsDiary, password: passwordA}
337
- - {name: encryptAsTips, password: passwordB}
338
- theme: xray
339
- wrong_pass_message: Oh, this is an invalid password. Check and try again, please.
340
- # Deprecated under v4 (alias of `wrong_pass_message`); see "Advanced settings".
341
- wrong_hash_message: Oh, these decrypted content cannot be verified, but you can still have a look.
307
+ On plain HTTP from a non-localhost host, `crypto.subtle` is `undefined`, so
308
+ decryption cannot run; the post shows an "open this page over HTTPS" message
309
+ instead of the wrong-password error. Always serve production sites over HTTPS.
342
310
 
343
- ```
311
+ ## Why upgrade from v3?
312
+
313
+ v4 closes real security gaps and fixes v3's UX problems:
314
+
315
+ - **Stronger crypto.** **AES-256-GCM** with a built-in authentication tag
316
+ replaces v3's AES-CBC plus a separate HMAC: one primitive, one
317
+ authentication path, and no CBC padding-oracle or HMAC-comparison bugs.
318
+ - **Per-post salt and nonce.** v3 used the same salt for every post, so one
319
+ password derived the same key everywhere. v4 derives an independent key per
320
+ post, so an attacker cannot reuse PBKDF2 work across posts that share a
321
+ password.
322
+ - **Stronger KDF defaults.** v3 used **1,024** PBKDF2-SHA256 iterations. v4
323
+ defaults to **250,000** (about 240 times more work per guess) and recommends
324
+ 600,000 or more (OWASP 2023). Tune it per post with `kdf.iterations`.
325
+ - **Privacy by default.** v3 always cached decrypted posts in `localStorage`,
326
+ so anyone with later access to the device could read them again. v4 defaults
327
+ to `autoSave: false`; you opt in per post or globally.
328
+ - **Inline errors.** v3 showed a `window.alert()` dialog on a wrong password.
329
+ v4 shows the error next to the field (`role="alert"`).
330
+ - **Optional decrypt button** for readers who don't know Enter submits the form.
331
+ - **Smaller bundle.** CryptoJS is gone; the browser bundle is about 6 KB
332
+ minified.
333
+ - **Tested.** 100% server coverage plus Playwright tests across all eight
334
+ themes, on every push.
335
+
336
+ The rationale for each change is on the wiki:
337
+ [Migration v3 → v4](https://github.com/D0n9X1n/hexo-blog-encrypt/wiki/Migration-v3-to-v4).
338
+
339
+ ## Upgrading from v3
340
+
341
+ v4 changed the encrypted format (AES-GCM, per-post salt and nonce). v4 cannot
342
+ read v3 output and v3 cannot read v4 output.
343
+
344
+ 1. Upgrade the plugin: `npm install --save hexo-blog-encrypt@^4`.
345
+ 2. Rebuild from source: `hexo clean && hexo generate`.
346
+ 3. Purge your CDN if you use one. Post HTML changes on every build because each
347
+ encryption uses a fresh nonce. The runtime asset `lib/hbe.<hash>.js` is
348
+ content-hashed and changes only when the bundle changes.
349
+ 4. Optional: remove `wrong_hash_message` from your config and front matter. It
350
+ is deprecated in v4 and will be removed in v5.
351
+
352
+ Your source `.md` files are not touched. See the
353
+ [v4.0.0 release notes](https://github.com/D0n9X1n/hexo-blog-encrypt/releases/tag/v4.0.0)
354
+ and [Releases](https://github.com/D0n9X1n/hexo-blog-encrypt/releases) for the
355
+ full list of breaking changes.
344
356
 
345
- Check them online, and PICK one:
357
+ ## Contributing
346
358
 
347
- + [default](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/default/)
348
- + [blink](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/blink/)
349
- + [shrink](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/shrink/)
350
- + [flip](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/flip/)
351
- + [up](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/up/)
352
- + [surge](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/surge/)
353
- + [wave](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/wave/)
354
- + [xray](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/xray/)
359
+ Issues and pull requests are welcome. Please read
360
+ [CONTRIBUTING.md](./.github/CONTRIBUTING.md) and include reproduction steps,
361
+ logs and your configuration when reporting a bug.
355
362
 
363
+ - [Architecture](./docs/ARCHITECTURE.md)
364
+ - [Development](./docs/DEVELOPMENT.md)
365
+ - [Releasing](./docs/RELEASING.md)
366
+ - [Themes](./docs/THEMES.md)
356
367
 
357
368
  ## License
358
369
 
359
- See [LICENSE](./LICENSE) file.
370
+ [MIT](./LICENSE)
360
371
 
361
372
  ## Thanks
362
373
 
363
- Collaborator - [xiazeyu](https://github.com/xiazeyu)
374
+ Collaborator: [xiazeyu](https://github.com/xiazeyu)