hexo-blog-encrypt 4.0.3 → 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 +252 -256
- package/ReadMe.zh.md +218 -237
- package/lib/hbe.blink.html +1 -1
- package/lib/hbe.bundle.js +1 -1
- package/lib/hbe.bundle.js.map +4 -4
- package/lib/hbe.default.html +1 -1
- package/lib/hbe.flip.html +1 -1
- package/lib/hbe.shrink.html +1 -1
- package/lib/hbe.style.css +50 -20
- package/lib/hbe.surge.html +1 -1
- package/lib/hbe.up.html +1 -1
- package/lib/hbe.wave.html +1 -1
- package/lib/hbe.xray.html +1 -1
- package/package.json +11 -9
- package/src/server/config.js +104 -4
- package/src/server/crypto.js +10 -5
- package/src/server/generator.js +18 -2
- package/src/server/index.js +12 -5
- package/src/server/template.js +2 -1
package/ReadMe.zh.md
CHANGED
|
@@ -1,143 +1,101 @@
|
|
|
1
1
|
# hexo-blog-encrypt
|
|
2
2
|
|
|
3
|
+
> 为单篇 Hexo 文章设置密码. 构建时使用 AES-256-GCM 加密, 读者在浏览器中通过
|
|
4
|
+
> Web Crypto API 解密. 无需服务端, 生成的 HTML 中不含明文.
|
|
5
|
+
|
|
6
|
+
**[▶ 在线演示](https://d0n9x1n.github.io/hexo-blog-encrypt/)**: 所有演示密码都是 `hello`.
|
|
7
|
+
|
|
3
8
|
[](https://github.com/D0n9X1n/hexo-blog-encrypt/releases)
|
|
4
9
|
[](https://www.npmjs.com/package/hexo-blog-encrypt)
|
|
5
10
|
[](https://www.npmjs.com/package/hexo-blog-encrypt)
|
|
6
11
|
[](./LICENSE)
|
|
7
12
|
[](https://github.com/D0n9X1n/hexo-blog-encrypt/actions/workflows/test.yml)
|
|
13
|
+
[](./docs/DEVELOPMENT.md)
|
|
8
14
|
[](https://d0n9x1n.github.io/hexo-blog-encrypt/)
|
|
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
|
-
|
|
15
|
+
[](./package.json)
|
|
16
|
+
[](https://hexo.io/)
|
|
17
|
+
[](./package.json)
|
|
18
|
+
[](https://www.npmjs.com/package/hexo-blog-encrypt#provenance)
|
|
19
|
+
[](https://github.com/D0n9X1n/hexo-blog-encrypt/stargazers)
|
|
20
|
+
|
|
21
|
+
[English](./ReadMe.md) · 中文说明 · [Wiki](https://github.com/D0n9X1n/hexo-blog-encrypt/wiki) · [Releases](https://github.com/D0n9X1n/hexo-blog-encrypt/releases)
|
|
22
|
+
|
|
23
|
+
## 目录
|
|
24
|
+
|
|
25
|
+
- [主要特性](#主要特性)
|
|
26
|
+
- [在线演示](#在线演示)
|
|
27
|
+
- [安装](#安装)
|
|
28
|
+
- [快速开始](#快速开始)
|
|
29
|
+
- [配置](#配置)
|
|
30
|
+
- [主题集成](#主题集成)
|
|
31
|
+
- [浏览器支持](#浏览器支持)
|
|
32
|
+
- [为什么要从 v3 升级](#为什么要从-v3-升级)
|
|
33
|
+
- [从 v3 升级](#从-v3-升级)
|
|
34
|
+
- [参与贡献](#参与贡献)
|
|
35
|
+
- [许可](#许可)
|
|
36
|
+
|
|
37
|
+
## 主要特性
|
|
38
|
+
|
|
39
|
+
| | |
|
|
40
|
+
| --- | --- |
|
|
41
|
+
| **认证加密** | [PBKDF2](https://www.rfc-editor.org/rfc/rfc8018)-SHA256 派生密钥, [AES-256-GCM](https://csrc.nist.gov/pubs/sp/800/38/d/final) 加密. GCM 认证标签会拒绝错误密码和被篡改的密文. |
|
|
42
|
+
| **只用原生密码学 API** | 构建时使用 Node.js [`crypto`](https://nodejs.org/api/crypto.html), 阅读时使用浏览器 [Web Crypto API](https://developer.mozilla.org/zh-CN/docs/Web/API/Web_Crypto_API). 没有运行时依赖. |
|
|
43
|
+
| **每篇文章独立密钥** | 每篇文章都有独立的 32 字节 salt 和 12 字节 nonce, 破解一篇不会加速对其它文章的攻击. |
|
|
44
|
+
| **文章密码或标签密码** | 在文章信息头写 `password:`, 或在 `_config.yml` 中为标签配置密码. |
|
|
45
|
+
| **8 个主题** | `default`, `blink`, `flip`, `shrink`, `surge`, `up`, `wave`, `xray`. 放入一个 HTML 文件即可新增主题. |
|
|
46
|
+
| **脚本照常运行** | 解密后文章中的脚本按文档顺序执行. 文章显示后触发 `hexo-blog-decrypt` 事件. |
|
|
47
|
+
| **默认保护隐私** | 除非用 `autoSave` 开启, 否则不保存密码或密钥. |
|
|
48
|
+
| **完整测试** | 服务端 100% 行 / 分支 / 函数覆盖, 并用 Playwright 对每个主题做端到端测试. |
|
|
43
49
|
|
|
44
50
|
## 在线演示
|
|
45
51
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
## 浏览器支持
|
|
49
|
-
|
|
50
|
-
运行时使用 Web Crypto (`crypto.subtle`), 因此需要 **安全上下文**
|
|
51
|
-
(HTTPS 或 `http://localhost`). 已验证:
|
|
52
|
+
打开 **[演示站点](https://d0n9x1n.github.io/hexo-blog-encrypt/)**, 所有密码都是 `hello`.
|
|
52
53
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
| Firefox | 90+ | |
|
|
57
|
-
| Safari (macOS / iOS) | 14+ | 发版前手动冒烟 |
|
|
58
|
-
|
|
59
|
-
如果站点是通过非 localhost 的纯 HTTP 提供, 解密会静默失败
|
|
60
|
-
(`crypto.subtle` 为 `undefined`). 生产环境请始终使用 HTTPS.
|
|
61
|
-
|
|
62
|
-
## 为什么要从 v3 升级
|
|
63
|
-
|
|
64
|
-
v4 不是表面的刷新, 而是真正修复了 v3 的安全短板与 UX 痛点. 主要原因:
|
|
65
|
-
|
|
66
|
-
- **更强的密码学.** **AES-256-GCM** 自带的 auth tag 取代了 v3 的
|
|
67
|
-
AES-CBC + 独立 HMAC. 单一原语、单一鉴权路径、硬件加速, 并且免疫一类
|
|
68
|
-
CBC padding-oracle 与 HMAC 比较 bug.
|
|
69
|
-
- **每篇独立 salt + nonce.** v3 所有文章共用同一个 salt — 同样的密码
|
|
70
|
-
到处派生出同样的密钥. v4 每篇独立派生密钥 — 攻击者无法把对一个密码
|
|
71
|
-
的 PBKDF2 计算结果, 复用到其它使用相同密码的文章上.
|
|
72
|
-
- **更强的 KDF 默认值.** v3 默认仅 **1,024** 轮 PBKDF2-SHA256 — 远低于
|
|
73
|
-
现代推荐值, 在现代硬件上很容易被批量爆破均摊. v4 默认 **250,000**
|
|
74
|
-
轮 (≈ 240× 单次工作量), 推荐 ≥ 600,000 (OWASP 2023). 可通过
|
|
75
|
-
`kdf.iterations` 按文章覆盖.
|
|
76
|
-
- **隐私默认关闭.** v3 总是把解密后的密钥缓存到 `localStorage`, 共享
|
|
77
|
-
设备的下一个用户可以直接重看. v4 默认 `autoSave: false`, 你按需开启
|
|
78
|
-
(按文章或全局).
|
|
79
|
-
- **行内错误提示.** v3 用 `window.alert()` 弹窗提示密码错 — 移动端
|
|
80
|
-
尤其打断流程. v4 直接把错误显示在密码框旁边 (`role="alert"`).
|
|
81
|
-
- **可选的"解密"按钮.** 让不知道按 Enter 的访客也能用. 文案可配置.
|
|
82
|
-
- **更小.** 抛弃了打包的 CryptoJS 依赖, 改用浏览器原生 Web Crypto
|
|
83
|
-
API. 浏览器端 bundle ≈ 6 KB (minified).
|
|
84
|
-
- **有测试.** 服务端套件 100% 行 / 分支 / 函数覆盖 + Playwright e2e
|
|
85
|
-
覆盖全部 8 个主题. v3 几乎没有自动化测试; v4 每次推送都跑回归.
|
|
86
|
-
|
|
87
|
-
如果你在意有人能拉到你生成的 HTML 后做离线密码爆破, 或者在意共用
|
|
88
|
-
设备上缓存密钥被偷看 — 请升级.
|
|
89
|
-
|
|
90
|
-
每条变化更深入的"为什么", 见 wiki:
|
|
91
|
-
[Migration v3 → v4](https://github.com/D0n9X1n/hexo-blog-encrypt/wiki/Migration-v3-to-v4).
|
|
92
|
-
|
|
93
|
-
## 从 v3 升级
|
|
94
|
-
|
|
95
|
-
v4 修改了密文格式 (AES-GCM, 每篇文章独立 salt + 每次加密独立 nonce).
|
|
96
|
-
v3 加密的产物在 v4 下无法解密, 反之亦然. 升级步骤:
|
|
97
|
-
|
|
98
|
-
1. 升级插件: `npm install --save hexo-blog-encrypt@^4`.
|
|
99
|
-
2. 重新生成: `hexo clean && hexo generate`.
|
|
100
|
-
3. 如果使用 CDN, 请清空缓存 (按设计, 每次构建文章 HTML 都会变化 —
|
|
101
|
-
每次加密都使用新的 nonce — 但运行时资源 `lib/hbe.<hash>.js`
|
|
102
|
-
是按内容 hash 命名的, 只有 bundle 改变时 URL 才会变).
|
|
103
|
-
4. 可选: 从 `_config.yml` 与 front-matter 中删除 `wrong_hash_message`
|
|
104
|
-
(v4 起废弃, v5 中删除).
|
|
105
|
-
|
|
106
|
-
不会触碰任何源 `.md` 文件. 完整的 break 列表与新增的 `decryptButton` /
|
|
107
|
-
`autoSave` / `kdf.iterations` 等配置请见 [CHANGELOG.md](./CHANGELOG.md).
|
|
54
|
+
- [主题展示](https://d0n9x1n.github.io/hexo-blog-encrypt/tags/theme-demo/)
|
|
55
|
+
- [解密回调](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/callback/)
|
|
56
|
+
- [脚本加载顺序与缓存密钥](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/script-order/)
|
|
108
57
|
|
|
109
58
|
## 安装
|
|
110
59
|
|
|
111
|
-
|
|
60
|
+
```bash
|
|
61
|
+
npm install --save hexo-blog-encrypt
|
|
62
|
+
```
|
|
112
63
|
|
|
113
|
-
|
|
64
|
+
需要 Node.js 18 及以上, Hexo 5 及以上. npm 包带有
|
|
65
|
+
[provenance](https://docs.npmjs.com/generating-provenance-statements) 证明,
|
|
66
|
+
并镜像发布到 GitHub Packages (`@d0n9x1n/hexo-blog-encrypt`).
|
|
114
67
|
|
|
115
|
-
##
|
|
68
|
+
## 快速开始
|
|
116
69
|
|
|
117
|
-
|
|
70
|
+
在文章信息头中加入 `password`:
|
|
118
71
|
|
|
119
72
|
```markdown
|
|
120
|
-
|
|
121
73
|
---
|
|
122
74
|
title: Hello World
|
|
123
75
|
date: 2016-03-30 21:18:02
|
|
124
76
|
password: hello
|
|
125
77
|
---
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
然后重新生成并预览:
|
|
126
81
|
|
|
82
|
+
```bash
|
|
83
|
+
hexo clean && hexo generate && hexo server
|
|
127
84
|
```
|
|
128
85
|
|
|
129
|
-
|
|
86
|
+
文章正文会被替换为密码输入框. 首页等列表页显示 `abstract` 文本, 而不是正文.
|
|
130
87
|
|
|
131
|
-
##
|
|
88
|
+
## 配置
|
|
132
89
|
|
|
133
|
-
|
|
90
|
+
配置按以下顺序生效:
|
|
134
91
|
|
|
135
|
-
|
|
92
|
+
**文章信息头 > `_config.yml` 中的 `encrypt:` > 内置默认值**
|
|
93
|
+
|
|
94
|
+
文章信息头中的 `password` 优先于标签密码.
|
|
136
95
|
|
|
137
96
|
### 文章信息头
|
|
138
97
|
|
|
139
98
|
```markdown
|
|
140
|
-
|
|
141
99
|
---
|
|
142
100
|
title: Hello World
|
|
143
101
|
tags:
|
|
@@ -147,47 +105,39 @@ password: mikemessi
|
|
|
147
105
|
abstract: 有东西被加密了, 请输入密码查看.
|
|
148
106
|
message: 您好, 这里需要密码.
|
|
149
107
|
wrong_pass_message: 抱歉, 这个密码看着不太对, 请再试试.
|
|
150
|
-
|
|
151
|
-
# 该字段现在等同于 `wrong_pass_message`. 请改设 `wrong_pass_message`.
|
|
152
|
-
wrong_hash_message: 抱歉, 这个文章不能被校验, 不过您还是能看看解密后的内容.
|
|
108
|
+
theme: xray
|
|
153
109
|
---
|
|
154
|
-
|
|
155
110
|
```
|
|
156
111
|
|
|
157
|
-
### `_config.yml`
|
|
158
|
-
|
|
159
|
-
#### 示例
|
|
112
|
+
### 全站 `_config.yml`
|
|
160
113
|
|
|
161
114
|
```yaml
|
|
162
|
-
|
|
163
|
-
# Security
|
|
164
115
|
encrypt: # hexo-blog-encrypt
|
|
165
116
|
abstract: 有东西被加密了, 请输入密码查看.
|
|
166
117
|
message: 您好, 这里需要密码.
|
|
167
|
-
tags:
|
|
168
|
-
- {name: tagName, password: 密码A}
|
|
169
|
-
- {name: tagName, password: 密码B}
|
|
170
118
|
wrong_pass_message: 抱歉, 这个密码看着不太对, 请再试试.
|
|
171
|
-
|
|
172
|
-
|
|
119
|
+
theme: default
|
|
120
|
+
silent: false
|
|
173
121
|
|
|
174
|
-
#
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
122
|
+
# 加密所有带有以下标签的文章.
|
|
123
|
+
tags:
|
|
124
|
+
- {name: 作为日记加密, password: 密码A}
|
|
125
|
+
- {name: 作为提示加密, password: 密码B}
|
|
126
|
+
|
|
127
|
+
# 在密码框旁显示 "解密" 按钮. 默认: true.
|
|
178
128
|
decryptButton:
|
|
179
129
|
show: true
|
|
180
130
|
text: 解密
|
|
181
131
|
|
|
182
|
-
#
|
|
183
|
-
#
|
|
184
|
-
#
|
|
132
|
+
# 将派生出的 AES 密钥缓存到 localStorage, 刷新页面时跳过密码输入.
|
|
133
|
+
# 默认: false. 可在这里全局开启, 也可在文章信息头中单独开启.
|
|
134
|
+
# 缓存键为 `hbe.v4.<页面路径>`.
|
|
185
135
|
autoSave: false
|
|
186
136
|
|
|
187
|
-
# 让同一篇文章的 PBKDF2 salt 在 clean build
|
|
188
|
-
#
|
|
137
|
+
# 让同一篇文章的 PBKDF2 salt 在 clean build 之间保持稳定, 文章身份来自
|
|
138
|
+
# post permalink. 默认: false. 配合 autoSave 时, 适用于
|
|
189
139
|
# Cloudflare Pages、Vercel、Netlify、GitHub Actions 等每次部署都会从
|
|
190
|
-
# 干净环境重新生成 HTML
|
|
140
|
+
# 干净环境重新生成 HTML 的平台.
|
|
191
141
|
#
|
|
192
142
|
# 只稳定 salt; AES-GCM nonce 每次加密仍然随机, 避免 nonce reuse.
|
|
193
143
|
# 如果文章 permalink 改变, stable salt 也会改变, 该页面已有的
|
|
@@ -199,89 +149,105 @@ encrypt: # hexo-blog-encrypt
|
|
|
199
149
|
# 默认: 250_000.
|
|
200
150
|
kdf:
|
|
201
151
|
iterations: 250000
|
|
202
|
-
|
|
203
152
|
```
|
|
204
153
|
|
|
205
154
|
`autoSave` 读取缓存密钥时会比较缓存中的 salt 与当前页面 salt. 仅 nonce
|
|
206
155
|
变化不会提前删除缓存; 浏览器会使用缓存密钥配合当前页面的 nonce 与密文尝试
|
|
207
156
|
解密. 如果 AES-GCM 认证失败, 该缓存条目会被清除, 访客需要重新输入密码.
|
|
208
157
|
|
|
209
|
-
|
|
158
|
+
**已保存的密钥及其暴露面.** 保存的密钥位于 `localStorage` 的
|
|
159
|
+
`hbe.v4.<页面路径>`, 在被删除前可在本设备上解锁该文章. 同源运行的任何脚本
|
|
160
|
+
(主题、统计、评论组件或被注入的脚本) 都能读取它, 因此只有在信任站点上所有脚本时
|
|
161
|
+
才开启 `autoSave`. 文章通过已保存的密钥显示后, 上方的 **忘记已保存的密钥**
|
|
162
|
+
按钮可删除该条目. 对某篇文章关闭 `autoSave` 后, 下次访问时会删除该文章已保存的
|
|
163
|
+
密钥; v3 遗留的 `hexo-blog-encrypt:#…` 条目会被自动清除.
|
|
210
164
|
|
|
211
|
-
|
|
165
|
+
### 配置项一览
|
|
212
166
|
|
|
213
|
-
|
|
167
|
+
| 配置项 | 默认值 | 说明 |
|
|
168
|
+
| --- | --- | --- |
|
|
169
|
+
| `password` | 无 | 加密该文章. `""` 表示不加密, 包括不使用标签加密. 匹配到的标签条目若没有密码, 构建会失败. |
|
|
170
|
+
| `password: { env: NAME }` | 无 | 构建时从环境变量 `NAME` 读取密码 (可用于文章信息头、`encrypt.password` 或 `encrypt.tags[].password`). 变量缺失或为空时构建失败; 密码值不会被打印或渲染. 这只能让密码不出现在配置文件中, 不能保护公开提交的明文 Markdown. |
|
|
171
|
+
| `abstract` | `Here's something encrypted…` | 首页和归档页显示的摘要. |
|
|
172
|
+
| `message` | `Hey, password is required here.` | 密码输入框的提示文字. |
|
|
173
|
+
| `wrong_pass_message` | `Oh, this is an invalid password…` | 密码错误时的行内提示. |
|
|
174
|
+
| `theme` | `default` | 8 个内置主题之一. 未知名称会回退到 `default` 并打印 warning. |
|
|
175
|
+
| `tags` | `[]` | 仅限 `_config.yml`. 标签加密使用的 `{name, password}` 列表. |
|
|
176
|
+
| `decryptButton.show` / `.text` | `true` / `Decrypt` | 是否显示解密按钮及其文字. |
|
|
177
|
+
| `autoSave` | `false` | 将派生密钥缓存到 `localStorage`. |
|
|
178
|
+
| `stableSalt` | `false` | 由 permalink 派生 salt, 让缓存密钥在 clean build 后仍然有效. |
|
|
179
|
+
| `kdf.iterations` | `250000` | PBKDF2 迭代次数, 必须是不小于 100,000 的整数. |
|
|
180
|
+
| `silent` | `false` | 隐藏构建时的 info 日志. warning 和 error 仍会输出. |
|
|
181
|
+
| `wrong_hash_message` | `wrong_pass_message` | 已废弃的别名. AES-GCM 对错误密码和篡改给出同样的结果. |
|
|
182
|
+
| `template` | 无 | `theme` 的已废弃别名. |
|
|
214
183
|
|
|
215
|
-
|
|
184
|
+
布尔配置项 (`autoSave`、`stableSalt`、`silent`、`decryptButton.show`) 必须是 `true` 或 `false`. 带引号的 `"true"`/`"false"` 会被接受并打印 warning; 其他值会使构建失败.
|
|
185
|
+
|
|
186
|
+
`abstract`、`message`、`wrong_pass_message` 和 `decryptButton.text` 的默认值跟随文章的 `lang`/`language`, 否则跟随站点 `language`. `zh`、`zh-CN` 和 `zh-Hans` 使用简体中文, 其他语言使用英文. 显式配置始终优先.
|
|
187
|
+
|
|
188
|
+
### 对单篇文章禁用标签加密
|
|
189
|
+
|
|
190
|
+
在该文章的信息头中设置空密码:
|
|
191
|
+
|
|
192
|
+
```markdown
|
|
216
193
|
---
|
|
217
|
-
title:
|
|
218
|
-
date: 2019-12-21 11:54:07
|
|
194
|
+
title: 带有加密标签的公开文章
|
|
219
195
|
tags:
|
|
220
196
|
- A Tag should be encrypted
|
|
221
197
|
password: ""
|
|
222
198
|
---
|
|
223
|
-
|
|
224
|
-
Use a "" to diable tag encryption.
|
|
225
199
|
```
|
|
226
200
|
|
|
227
|
-
|
|
201
|
+
## 主题集成
|
|
228
202
|
|
|
229
|
-
|
|
203
|
+
### 解密后事件
|
|
230
204
|
|
|
231
|
-
|
|
232
|
-
|
|
205
|
+
每篇文章显示后, 插件会在 `window` 上触发一次 `hexo-blog-decrypt`.
|
|
206
|
+
`event.detail.mode` 为 `manual` (输入密码) 或 `cached` (已缓存的密钥),
|
|
207
|
+
`event.detail.element` 是显示出的容器 (首页同时展示多篇加密文章时可用来区分).
|
|
208
|
+
监听它来重新执行需要扫描正文的代码:
|
|
233
209
|
|
|
210
|
+
```js
|
|
211
|
+
window.addEventListener('hexo-blog-decrypt', function (event) {
|
|
212
|
+
// 重新初始化主题的目录、代码高亮等.
|
|
213
|
+
console.log('Decrypted:', event.detail.mode, event.detail.element);
|
|
214
|
+
});
|
|
234
215
|
```
|
|
235
|
-
---
|
|
236
|
-
title: Callback Test
|
|
237
|
-
date: 2019-12-21 11:54:07
|
|
238
|
-
tags:
|
|
239
|
-
- Encrypted
|
|
240
|
-
---
|
|
241
216
|
|
|
242
|
-
|
|
217
|
+
### PJAX 与单页应用主题
|
|
243
218
|
|
|
244
|
-
|
|
219
|
+
脚本会初始化页面上的每一篇加密文章, 且每篇只初始化一次. 在 `document` 上触发
|
|
220
|
+
`pjax:complete`, `pjax:success`, `swup:contentReplaced` 或 `turbo:load` 时会自动
|
|
221
|
+
重新扫描. 其他路由方案可在替换内容后调用幂等的初始化函数:
|
|
245
222
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
</script>
|
|
223
|
+
```js
|
|
224
|
+
window.hexoBlogEncrypt.init(); // 整个文档
|
|
225
|
+
window.hexoBlogEncrypt.init(container); // 仅某个子树
|
|
250
226
|
```
|
|
251
227
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
### 解密后的触发事件
|
|
228
|
+
### 加密文章中的脚本
|
|
255
229
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
230
|
+
文章中的脚本会在解密后执行. 自 **4.0.3** 起, 普通外部脚本会按文档顺序加载,
|
|
231
|
+
再执行其后的内联脚本, 因此 DPlayer 等依赖库的代码可以正确初始化. 解密事件会等待
|
|
232
|
+
恢复的外部脚本加载完成、加载失败或达到 **每个脚本 15 秒** 的期限. 超时不会取消
|
|
233
|
+
脚本, 它之后仍可能执行. 原有的 `onload` / `onerror` 处理函数会被保留. 显式
|
|
234
|
+
`async` 的脚本仍独立加载, 恢复的 `defer` 脚本不会重建文档解析阶段. 内联
|
|
235
|
+
`type="module"` 脚本及脚本中的异步任务保留原生调度, 可能在解密事件之后才完成.
|
|
259
236
|
|
|
260
|
-
```
|
|
261
|
-
|
|
262
|
-
//
|
|
263
|
-
console.log('
|
|
264
|
-
|
|
237
|
+
```markdown
|
|
238
|
+
<script>
|
|
239
|
+
// 文章解密后执行.
|
|
240
|
+
console.log('Hello from an encrypted post');
|
|
241
|
+
</script>
|
|
265
242
|
```
|
|
266
243
|
|
|
267
|
-
|
|
268
|
-
这解决了 DPlayer 等组件在其依赖库尚未加载时就初始化的问题。
|
|
269
|
-
外部脚本加载成功、失败或达到每个脚本 **15 秒** 的等待上限后,才发送解密事件;
|
|
270
|
-
原有 `onload` / `onerror` 处理函数保持不变。超时不等于取消,脚本可能稍后执行。
|
|
271
|
-
带有显式 `async` 的外部脚本仍独立加载;动态恢复的 `defer` 不会重新产生页面解析阶段。
|
|
272
|
-
内联 `type="module"` 脚本及脚本内部的异步任务保留浏览器原生时序,不保证在解密事件前完成。
|
|
273
|
-
依赖这些任务时,请使用它们自己的就绪信号。
|
|
274
|
-
|
|
275
|
-
[脚本加载顺序示例](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/script-order/)
|
|
276
|
-
(密码 `hello`)同时展示普通解密与缓存密钥解密。
|
|
277
|
-
|
|
278
|
-
### 对 TOC 进行加密
|
|
244
|
+
### 目录 (TOC)
|
|
279
245
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
246
|
+
**不要把加密文章的 `post.origin` 渲染到生成的 HTML 中.** 它保存的是为旧版 Hexo
|
|
247
|
+
集成保留的明文. 用 `display:none` 隐藏的 TOC 仍然会公开标题. 搜索、订阅和站点地图
|
|
248
|
+
插件也必须排除加密文章的明文字段.
|
|
283
249
|
|
|
284
|
-
|
|
250
|
+
只为未加密文章生成服务端 TOC:
|
|
285
251
|
|
|
286
252
|
```ejs
|
|
287
253
|
<% if (post.toc === true && !post.encrypt) { %>
|
|
@@ -290,83 +256,98 @@ window.addEventListener('hexo-blog-decrypt', function (event) {
|
|
|
290
256
|
<%- post.content %>
|
|
291
257
|
```
|
|
292
258
|
|
|
293
|
-
|
|
294
|
-
|
|
259
|
+
对加密文章, 在 `hexo-blog-decrypt` 回调中根据解密后的 `#hexo-blog-encrypt`
|
|
260
|
+
内容在浏览器端生成目录.
|
|
295
261
|
|
|
296
|
-
###
|
|
297
|
-
If you want to disable the logging, you can add a silent property in `_config.yml` and set it to true.
|
|
298
|
-
如果你想要禁止使用 Log, 你可以在 `_config.yml` 中增加一个 silent 属性, 并将其设置为 true.
|
|
262
|
+
### 加密主题
|
|
299
263
|
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
encrypt: # hexo-blog-encrypt
|
|
303
|
-
silent: true
|
|
304
|
-
```
|
|
264
|
+
通过文章信息头的 `theme:` 为单篇文章选择主题, 或在 `_config.yml` 中为全站设置.
|
|
265
|
+
在线预览:
|
|
305
266
|
|
|
306
|
-
|
|
267
|
+
[default](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/default/) ·
|
|
268
|
+
[blink](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/blink/) ·
|
|
269
|
+
[flip](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/flip/) ·
|
|
270
|
+
[shrink](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/shrink/) ·
|
|
271
|
+
[surge](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/surge/) ·
|
|
272
|
+
[up](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/up/) ·
|
|
273
|
+
[wave](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/wave/) ·
|
|
274
|
+
[xray](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/xray/)
|
|
307
275
|
|
|
308
|
-
|
|
276
|
+
新增主题请参阅 [docs/THEMES.md](./docs/THEMES.md).
|
|
309
277
|
|
|
310
|
-
|
|
278
|
+
## 浏览器支持
|
|
311
279
|
|
|
312
|
-
|
|
280
|
+
解密使用 Web Crypto (`crypto.subtle`), 需要 **安全上下文**: HTTPS 或
|
|
281
|
+
`http://localhost`.
|
|
313
282
|
|
|
314
|
-
|
|
283
|
+
| 浏览器 | 最低版本 | 测试方式 |
|
|
284
|
+
| --- | --- | --- |
|
|
285
|
+
| Chromium / Edge | 92+ | 自动: CI 中 Playwright Chromium, Hexo 7 与 8 |
|
|
286
|
+
| Firefox | 90+ | 自动: CI 中 Playwright Firefox |
|
|
287
|
+
| Safari (macOS) | 14+ | 自动: CI 中 Playwright WebKit (WebKit 引擎, 非 Safari 本身) |
|
|
288
|
+
| Safari (iOS) | 14+ | 仅手动测试 |
|
|
315
289
|
|
|
316
|
-
|
|
290
|
+
最低版本依据 Web Crypto 与语言特性支持; CI 运行的是当前的 Playwright
|
|
291
|
+
浏览器版本, 而非这些最低版本.
|
|
317
292
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
- 作为日记加密
|
|
322
|
-
date: 2016-03-30 21:12:21
|
|
323
|
-
password: mikemessi
|
|
324
|
-
abstract: 有东西被加密了, 请输入密码查看.
|
|
325
|
-
message: 您好, 这里需要密码.
|
|
326
|
-
theme: xray
|
|
327
|
-
wrong_pass_message: 抱歉, 这个密码看着不太对, 请再试试.
|
|
328
|
-
# v4 起已废弃 (等同于 `wrong_pass_message`), 详见 “高级设置”.
|
|
329
|
-
wrong_hash_message: 抱歉, 这个文章不能被校验, 不过您还是能看看解密后的内容.
|
|
330
|
-
---
|
|
293
|
+
在非 localhost 的纯 HTTP 站点上, `crypto.subtle` 为 `undefined`, 无法解密;
|
|
294
|
+
文章会提示 "请通过 HTTPS 打开此页面", 而不是显示密码错误.
|
|
295
|
+
生产环境请始终使用 HTTPS.
|
|
331
296
|
|
|
332
|
-
|
|
297
|
+
## 为什么要从 v3 升级
|
|
333
298
|
|
|
334
|
-
|
|
299
|
+
v4 修复了 v3 真实存在的安全短板与体验问题:
|
|
300
|
+
|
|
301
|
+
- **更强的密码学.** **AES-256-GCM** 自带认证标签, 取代了 v3 的 AES-CBC 加独立
|
|
302
|
+
HMAC: 单一原语、单一认证路径, 不存在 CBC padding-oracle 或 HMAC 比较漏洞.
|
|
303
|
+
- **每篇独立 salt 和 nonce.** v3 所有文章共用同一个 salt, 同一个密码到处派生出
|
|
304
|
+
同一个密钥. v4 每篇独立派生密钥, 攻击者无法在使用相同密码的文章间复用 PBKDF2
|
|
305
|
+
计算.
|
|
306
|
+
- **更强的 KDF 默认值.** v3 仅使用 **1,024** 轮 PBKDF2-SHA256. v4 默认
|
|
307
|
+
**250,000** 轮 (每次猜测约 240 倍工作量), 推荐 600,000 轮以上 (OWASP 2023).
|
|
308
|
+
可通过 `kdf.iterations` 按文章调整.
|
|
309
|
+
- **默认保护隐私.** v3 总是把解密内容缓存到 `localStorage`, 之后使用该设备的人
|
|
310
|
+
可以再次阅读. v4 默认 `autoSave: false`, 按文章或全局开启.
|
|
311
|
+
- **行内错误提示.** v3 在密码错误时弹出 `window.alert()`. v4 在输入框旁显示错误
|
|
312
|
+
(`role="alert"`).
|
|
313
|
+
- **可选的解密按钮**, 方便不知道按 Enter 提交的读者.
|
|
314
|
+
- **更小的体积.** 移除了 CryptoJS, 浏览器端 bundle 约 6 KB (minified).
|
|
315
|
+
- **完整测试.** 每次推送都运行 100% 服务端覆盖测试及覆盖全部 8 个主题的
|
|
316
|
+
Playwright 测试.
|
|
317
|
+
|
|
318
|
+
每项变化的详细原因见 wiki:
|
|
319
|
+
[Migration v3 → v4](https://github.com/D0n9X1n/hexo-blog-encrypt/wiki/Migration-v3-to-v4).
|
|
335
320
|
|
|
336
|
-
|
|
321
|
+
## 从 v3 升级
|
|
337
322
|
|
|
338
|
-
|
|
323
|
+
v4 修改了加密格式 (AES-GCM, 每篇独立 salt 和 nonce). v4 无法读取 v3 的产物,
|
|
324
|
+
反之亦然.
|
|
339
325
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
theme: xray
|
|
348
|
-
wrong_pass_message: 抱歉, 这个密码看着不太对, 请再试试.
|
|
349
|
-
# v4 起已废弃 (等同于 `wrong_pass_message`), 详见 “高级设置”.
|
|
350
|
-
wrong_hash_message: 抱歉, 这个文章不能被校验, 不过您还是能看看解密后的内容.
|
|
326
|
+
1. 升级插件: `npm install --save hexo-blog-encrypt@^4`.
|
|
327
|
+
2. 从源文件重新生成: `hexo clean && hexo generate`.
|
|
328
|
+
3. 如果使用 CDN, 请清空缓存. 每次加密都使用新的 nonce, 因此每次构建文章 HTML
|
|
329
|
+
都会变化. 运行时资源 `lib/hbe.<hash>.js` 按内容 hash 命名, 只有 bundle
|
|
330
|
+
改变时才会变化.
|
|
331
|
+
4. 可选: 从配置和文章信息头中删除 `wrong_hash_message`. 它在 v4 中已废弃,
|
|
332
|
+
将在 v5 中删除.
|
|
351
333
|
|
|
352
|
-
|
|
334
|
+
不会修改任何源 `.md` 文件. 完整的不兼容变更列表见 [v4.0.0 发布说明](https://github.com/D0n9X1n/hexo-blog-encrypt/releases/tag/v4.0.0) 和 [Releases](https://github.com/D0n9X1n/hexo-blog-encrypt/releases).
|
|
353
335
|
|
|
354
|
-
|
|
336
|
+
## 参与贡献
|
|
355
337
|
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
+ [flip](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/flip/)
|
|
360
|
-
+ [up](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/up/)
|
|
361
|
-
+ [surge](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/surge/)
|
|
362
|
-
+ [wave](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/wave/)
|
|
363
|
-
+ [xray](https://d0n9x1n.github.io/hexo-blog-encrypt/demo/theme/xray/)
|
|
338
|
+
欢迎提交 issue 和 pull request. 请先阅读
|
|
339
|
+
[CONTRIBUTING.md](./.github/CONTRIBUTING.md). 报告 bug 时请附上复现步骤、日志和
|
|
340
|
+
配置, 信息不完整的 issue 可能会被关闭.
|
|
364
341
|
|
|
342
|
+
- [架构](./docs/ARCHITECTURE.md)
|
|
343
|
+
- [开发](./docs/DEVELOPMENT.md)
|
|
344
|
+
- [发布](./docs/RELEASING.md)
|
|
345
|
+
- [主题](./docs/THEMES.md)
|
|
365
346
|
|
|
366
347
|
## 许可
|
|
367
348
|
|
|
368
|
-
|
|
349
|
+
[MIT](./LICENSE)
|
|
369
350
|
|
|
370
351
|
## 感谢
|
|
371
352
|
|
|
372
|
-
|
|
353
|
+
协作者: [xiazeyu](https://github.com/xiazeyu)
|
package/lib/hbe.blink.html
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
{{hbeEncryptedData}}
|
|
11
11
|
</script>
|
|
12
12
|
<div class="hbe hbe-content">
|
|
13
|
-
<form id="hbeForm" class="hbe hbe-form"
|
|
13
|
+
<form id="hbeForm" class="hbe hbe-form">
|
|
14
14
|
<div class="hbe hbe-input hbe-input-blink">
|
|
15
15
|
<input class="hbe hbe-input-field hbe-input-field-blink" type="password" id="hbePass">
|
|
16
16
|
<label class="hbe hbe-input-label hbe-input-label-blink" for="hbePass">
|
package/lib/hbe.bundle.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";(()=>{var p=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports);var m=p((ot,x)=>{"use strict";function D(e,t){if(!e)return;e.setAttribute("aria-busy",t?"true":"false");let n=e.querySelector(".hbe-button");n&&(n.disabled=!!t);let r=e.querySelector("#hbePass");r&&(r.disabled=!!t)}function B(e,t){let n=e.querySelector('[role="alert"]');if(!n){n=document.createElement("div"),n.setAttribute("role","alert"),n.className="hbe hbe-error";let r=e.querySelector("#hbeForm");r?r.parentNode.insertBefore(n,r.nextSibling):e.appendChild(n)}n.textContent=t}function k(e){let t=e.querySelector('[role="alert"]');t&&(t.textContent="")}function M(e){let t=e.querySelector("#hbePass");return t?t.value:""}function P(e,t){e&&e.addEventListener("submit",n=>{n.preventDefault(),t()})}x.exports={setBusy:D,showError:B,clearError:k,readPassword:M,attachSubmit:P}});var b=p((at,w)=>{"use strict";var O=new TextDecoder("utf-8",{fatal:!0}),K=new TextEncoder;function u(e){let t=e.length;if(t%2!==0)throw new Error("hexo-blog-encrypt: hex string has odd length");let n=new Uint8Array(t/2);for(let r=0;r<n.length;r++)n[r]=parseInt(e.substr(r*2,2),16);return n}async function g(e,t,n){let r=await crypto.subtle.importKey("raw",K.encode(e),{name:"PBKDF2"},!1,["deriveKey"]);return crypto.subtle.deriveKey({name:"PBKDF2",salt:t,iterations:n,hash:"SHA-256"},r,{name:"AES-GCM",length:256},!0,["decrypt"])}async function y(e,t,n){let r=await crypto.subtle.decrypt({name:"AES-GCM",iv:t,tagLength:128},e,n);return O.decode(r)}async function L({password:e,saltHex:t,nonceHex:n,ciphertextHex:r,iterations:o}){try{let a=u(t),c=u(n),i=u(r);if(i.length<16)return{ok:!1};let d=await g(e,a,o);return{ok:!0,plaintext:await y(d,c,i),key:d}}catch(a){return{ok:!1}}}async function R({key:e,nonceHex:t,ciphertextHex:n}){try{let r=u(t),o=u(n);return o.length<16?{ok:!1}:{ok:!0,plaintext:await y(e,r,o)}}catch(r){return{ok:!1}}}w.exports={tryDecryptWithPassword:L,tryDecryptWithKey:R,deriveKey:g,decryptWithKey:y,hexToBytes:u}});var T=p((ct,v)=>{"use strict";var q=/^(?:application|text)\/(?:x-)?(?:java|ecma)script$|^text\/(?:javascript1\.[0-5]|jscript|livescript)$/i;function S(e){let t=document.createElement("script");for(let n of e.attributes)t.setAttribute(n.name,n.value);return e.nonce&&(t.nonce=e.nonce),t.text=e.text,t}function E(e){let t=document.createElement("div");return t.innerHTML=e,t}function N(e){let t=e.getAttribute("type");if(t===null){let o=e.getAttribute("language");t=o?"text/"+o:""}let n=t.toLowerCase();if(n==="module")return"module";if(n==="importmap"||n==="speculationrules")return"declarative";let r=t.replace(/^[\t\n\f\r ]+|[\t\n\f\r ]+$/g,"");return t===""||q.test(r)?"classic":"data"}function G(e,t,n){return n?new Promise(r=>{let o=()=>{clearTimeout(a),t.removeEventListener("load",o),t.removeEventListener("error",o),r()},a=setTimeout(()=>{console.warn("hexo-blog-encrypt: timed out waiting for a decrypted script to load."),o()},15e3);t.addEventListener("load",o),t.addEventListener("error",o),e.parentNode.replaceChild(t,e)}):(e.parentNode.replaceChild(t,e),Promise.resolve())}async function F(e){let t=[],n="noModule"in document.createElement("script");for(let r of e.querySelectorAll("script")){if(!e.contains(r))continue;let o=N(r);if(o==="data"||o==="classic"&&r.noModule&&n||o==="module"&&!n)continue;let a=S(r),c=o!=="declarative"&&a.hasAttribute("src"),i=c&&a.hasAttribute("async");o==="classic"&&c&&!i&&(a.async=!1);let d=G(r,a,c);i?t.push(d):await d}await Promise.all(t)}async function W(e,t){let n=E(t);return n.id="hexo-blog-encrypt",n.classList.add("hbe","hbe-decrypted-content"),e.parentNode.replaceChild(n,e),await F(n),n}v.exports={getExecutableScript:S,convertHTMLToElement:E,swapInDecryptedDOM:W}});var I=p((st,_)=>{"use strict";var A="hbe.v4.";function l(e){return A+e}function Y(e){let t="";for(let n=0;n<e.length;n++)t+=String.fromCharCode(e[n]);return btoa(t)}function U(e){let t=atob(e),n=new Uint8Array(t.length);for(let r=0;r<t.length;r++)n[r]=t.charCodeAt(r);return n}async function V({pageKey:e,key:t,saltHex:n,nonceHex:r,autoSave:o}){if(o)try{let a=await crypto.subtle.exportKey("raw",t),c={version:4,dk:Y(new Uint8Array(a)),salt:n,nonce:r};localStorage.setItem(l(e),JSON.stringify(c))}catch(a){}}async function j({pageKey:e,expectedSaltHex:t}){let n;try{let r=localStorage.getItem(l(e));if(!r)return null;n=JSON.parse(r)}catch(r){return null}if(!n||n.version!==4||typeof n.dk!="string"||typeof n.salt!="string"||typeof n.nonce!="string"||"hmk"in n||"hmacDigest"in n){try{localStorage.removeItem(l(e))}catch(r){}return null}if(n.salt!==t){try{localStorage.removeItem(l(e))}catch(r){}return null}try{let r=U(n.dk);return await crypto.subtle.importKey("raw",r,{name:"AES-GCM"},!0,["decrypt"])}catch(r){try{localStorage.removeItem(l(e))}catch(o){}return null}}function J(e){try{localStorage.removeItem(l(e))}catch(t){}}_.exports={save:V,load:j,clear:J,STORAGE_KEY_PREFIX:A,SCHEMA_VERSION:4}});var s=m(),C=b(),X=T(),f=I(),$="4";function z(e){let t=e.dataset,n=e.querySelector("script#hbeData");return{format:t.hbeFormat,wpm:t.wpm||"Wrong password.",whm:t.whm||t.wpm||"Wrong password.",saltHex:t.salt,nonceHex:t.nonce,iterations:parseInt(t.kdfIterations,10)||25e4,autoSave:t.autoSave==="true",ciphertextHex:n?n.textContent.trim():""}}function Q(e){try{window.dispatchEvent(new CustomEvent("hexo-blog-decrypt",{detail:{mode:e}}))}catch(t){try{window.dispatchEvent(new Event("hexo-blog-decrypt"))}catch(n){}}}function h(){return location.pathname+location.search}async function Z(e,t,n){await X.swapInDecryptedDOM(e,t),Q(n)}async function tt(e,t,n){if(!t.autoSave)return!1;let r=await f.load({pageKey:h(),expectedSaltHex:t.saltHex});if(!r)return!1;let o=await C.tryDecryptWithKey({key:r,nonceHex:t.nonceHex,ciphertextHex:t.ciphertextHex});return o.ok?(await n(o.plaintext,"cached"),!0):(f.clear(h()),!1)}async function et(e,t,n,r){s.clearError(e),s.setBusy(n,!0);let o=s.readPassword(n);if(!o){s.setBusy(n,!1),s.showError(e,t.wpm);return}let a;try{a=await C.tryDecryptWithPassword({password:o,saltHex:t.saltHex,nonceHex:t.nonceHex,ciphertextHex:t.ciphertextHex,iterations:t.iterations})}finally{s.setBusy(n,!1)}if(!a.ok){s.showError(e,t.wpm);return}t.autoSave&&await f.save({pageKey:h(),key:a.key,saltHex:t.saltHex,nonceHex:t.nonceHex,autoSave:!0}),await r(a.plaintext,"manual")}async function H(){let e=document.getElementById("hexo-blog-encrypt");if(!e)return;let t=z(e);if(t.format!==$){s.showError(e,"hexo-blog-encrypt: this page was built with an incompatible plugin version. Rebuild the site after upgrading the plugin.");return}let n=!1,r=async(c,i)=>{n||(n=!0,await Z(e,c,i))},o=e.querySelector("#hbeForm");if(s.attachSubmit(o,()=>et(e,t,o,r)),!await tt(e,t,r)&&!n){let c=e.querySelector("#hbePass");c&&c.focus()}}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",H):H();})();
|
|
1
|
+
"use strict";(()=>{var y=(e,t)=>()=>{try{return t||e((t={exports:{}}).exports,t),t.exports}catch(n){throw t=0,n}};var E=y((Ee,x)=>{"use strict";function w(e){return e.querySelector("#hbeForm,.hbe-form,form")}function M(e){return e.querySelector('#hbePass,.hbe-input-field,input[type="password"]')}var K=0;function N(e,t){if(!t)return;let n=t.id;if(n&&document.getElementById(n)===t)return;let r;do r=(n||"hbePass")+"-"+ ++K;while(document.getElementById(r));let o=e.querySelectorAll("label");for(let c=0;c<o.length;c++)n&&o[c].htmlFor===n&&(o[c].htmlFor=r);t.id=r}function R(e,t,n){if(e){e.setAttribute("aria-busy",n?"true":"false");let r=e.querySelector(".hbe-button");r&&(r.disabled=!!n)}t&&(t.disabled=!!n)}function q(e){if(!e)return;let t=e.closest(".hbe-input")||e.parentNode,n=()=>t.classList.toggle("hbe-input--filled",e.value!=="");e.addEventListener("input",n),e.addEventListener("change",n),n()}function G(e){e&&(e.focus(),e.select())}function W(e,t){let n=e.querySelector('[role="alert"]');if(!n){n=document.createElement("div"),n.setAttribute("role","alert"),n.className="hbe hbe-error";let r=w(e);r?r.parentNode.insertBefore(n,r.nextSibling):e.appendChild(n)}n.textContent=t}function U(e){let t=e.querySelector('[role="alert"]');t&&(t.textContent="")}function Y(e,t){e&&e.addEventListener("submit",n=>{n.preventDefault(),t()})}function V(e,t,n){let r=document.createElement("div");r.className="hbe hbe-forget";let o=document.createElement("button");o.type="button",o.className="hbe hbe-forget-button",o.textContent=t.forget;let c=document.createElement("span");c.setAttribute("role","status"),c.tabIndex=-1,o.addEventListener("click",()=>{n(),r.removeChild(o),c.textContent=t.forgotten,c.focus()}),r.appendChild(o),r.appendChild(c),e.parentNode.insertBefore(r,e)}x.exports={findForm:w,findInput:M,ensureUniqueId:N,setBusy:R,trackFilled:q,refocus:G,showError:W,clearError:U,attachSubmit:Y,addForgetButton:V}});var A=y((ve,S)=>{"use strict";var j=new TextDecoder("utf-8",{fatal:!0}),X=new TextEncoder;function d(e){let t=e.length;if(t%2!==0)throw new Error("hexo-blog-encrypt: hex string has odd length");let n=new Uint8Array(t/2);for(let r=0;r<n.length;r++)n[r]=parseInt(e.substr(r*2,2),16);return n}async function v(e,t,n){let r=await crypto.subtle.importKey("raw",X.encode(e),{name:"PBKDF2"},!1,["deriveKey"]);return crypto.subtle.deriveKey({name:"PBKDF2",salt:t,iterations:n,hash:"SHA-256"},r,{name:"AES-GCM",length:256},!0,["decrypt"])}async function h(e,t,n){let r=await crypto.subtle.decrypt({name:"AES-GCM",iv:t,tagLength:128},e,n);return j.decode(r)}async function z({password:e,saltHex:t,nonceHex:n,ciphertextHex:r,iterations:o}){try{let c=d(t),i=d(n),u=d(r);if(u.length<16)return{ok:!1};let s=await v(e,c,o);return{ok:!0,plaintext:await h(s,i,u),key:s}}catch(c){return{ok:!1}}}async function J({key:e,nonceHex:t,ciphertextHex:n}){try{let r=d(t),o=d(n);return o.length<16?{ok:!1}:{ok:!0,plaintext:await h(e,r,o)}}catch(r){return{ok:!1}}}S.exports={tryDecryptWithPassword:z,tryDecryptWithKey:J,deriveKey:v,decryptWithKey:h,hexToBytes:d}});var _=y((Se,C)=>{"use strict";var $=/^(?:application|text)\/(?:x-)?(?:java|ecma)script$|^text\/(?:javascript1\.[0-5]|jscript|livescript)$/i;function T(e){let t=document.createElement("script");for(let n of e.attributes)t.setAttribute(n.name,n.value);return e.nonce&&(t.nonce=e.nonce),t.text=e.text,t}function I(e){let t=document.createElement("div");return t.innerHTML=e,t}function Q(e){let t=e.getAttribute("type");if(t===null){let o=e.getAttribute("language");t=o?"text/"+o:""}let n=t.toLowerCase();if(n==="module")return"module";if(n==="importmap"||n==="speculationrules")return"declarative";let r=t.replace(/^[\t\n\f\r ]+|[\t\n\f\r ]+$/g,"");return t===""||$.test(r)?"classic":"data"}function Z(e,t,n){return n?new Promise(r=>{let o=()=>{clearTimeout(c),t.removeEventListener("load",o),t.removeEventListener("error",o),r()},c=setTimeout(()=>{console.warn("hexo-blog-encrypt: timed out waiting for a decrypted script to load."),o()},15e3);t.addEventListener("load",o),t.addEventListener("error",o),e.parentNode.replaceChild(t,e)}):(e.parentNode.replaceChild(t,e),Promise.resolve())}async function ee(e){let t=[],n="noModule"in document.createElement("script");for(let r of e.querySelectorAll("script")){if(!e.contains(r))continue;let o=Q(r);if(o==="data"||o==="classic"&&r.noModule&&n||o==="module"&&!n)continue;let c=T(r),i=o!=="declarative"&&c.hasAttribute("src"),u=i&&c.hasAttribute("async");o==="classic"&&i&&!u&&(c.async=!1);let s=Z(r,c,i);u?t.push(s):await s}await Promise.all(t)}async function te(e,t){let n=I(t);return n.id="hexo-blog-encrypt",n.classList.add("hbe","hbe-decrypted-content"),e.parentNode.replaceChild(n,e),await ee(n),n}C.exports={getExecutableScript:T,convertHTMLToElement:I,swapInDecryptedDOM:te}});var L=y((Ae,H)=>{"use strict";var B="hbe.v4.";function p(e){return B+e}function ne(e){let t="";for(let n=0;n<e.length;n++)t+=String.fromCharCode(e[n]);return btoa(t)}function re(e){let t=atob(e),n=new Uint8Array(t.length);for(let r=0;r<t.length;r++)n[r]=t.charCodeAt(r);return n}async function oe({id:e,key:t,saltHex:n,nonceHex:r,autoSave:o}){if(o)try{let c=await crypto.subtle.exportKey("raw",t),i={version:4,dk:ne(new Uint8Array(c)),salt:n,nonce:r};localStorage.setItem(p(e),JSON.stringify(i))}catch(c){}}async function ce({id:e,expectedSaltHex:t}){let n;try{let r=localStorage.getItem(p(e));if(!r)return null;n=JSON.parse(r)}catch(r){return null}if(!n||n.version!==4||typeof n.dk!="string"||typeof n.salt!="string"||typeof n.nonce!="string"||"hmk"in n||"hmacDigest"in n){try{localStorage.removeItem(p(e))}catch(r){}return null}if(n.salt!==t){try{localStorage.removeItem(p(e))}catch(r){}return null}try{let r=re(n.dk);return await crypto.subtle.importKey("raw",r,{name:"AES-GCM"},!0,["decrypt"])}catch(r){try{localStorage.removeItem(p(e))}catch(o){}return null}}function ae(e){try{localStorage.removeItem(p(e))}catch(t){}}var se="hexo-blog-encrypt:#";function ie(){try{for(let e=localStorage.length-1;e>=0;e--){let t=localStorage.key(e);t&&t.indexOf(se)===0&&localStorage.removeItem(t)}}catch(e){}}H.exports={save:oe,load:ce,clear:ae,purgeLegacy:ie,STORAGE_KEY_PREFIX:B,SCHEMA_VERSION:4}});var O=y((Te,D)=>{"use strict";var k={en:{incompatible:"hexo-blog-encrypt: this page was built with an incompatible plugin version. Rebuild the site after upgrading the plugin.",noCrypto:"hexo-blog-encrypt: this browser cannot decrypt here because Web Crypto is unavailable. Open the page over HTTPS (or localhost) in an up-to-date browser.",forget:"Forget saved key",forgotten:"Saved key forgotten."},zh:{incompatible:"hexo-blog-encrypt: \u6B64\u9875\u9762\u7531\u63D2\u4EF6\u7248\u672C\u4E0D\u517C\u5BB9\u7684\u6784\u5EFA\u751F\u6210. \u5347\u7EA7\u63D2\u4EF6\u540E\u8BF7\u91CD\u65B0\u6784\u5EFA\u7AD9\u70B9.",noCrypto:"hexo-blog-encrypt: Web Crypto \u4E0D\u53EF\u7528, \u5F53\u524D\u6D4F\u89C8\u5668\u65E0\u6CD5\u5728\u6B64\u89E3\u5BC6. \u8BF7\u901A\u8FC7 HTTPS (\u6216 localhost) \u4F7F\u7528\u6700\u65B0\u7248\u6D4F\u89C8\u5668\u6253\u5F00\u6B64\u9875\u9762.",forget:"\u5FD8\u8BB0\u5DF2\u4FDD\u5B58\u7684\u5BC6\u94A5",forgotten:"\u5DF2\u5FD8\u8BB0\u4FDD\u5B58\u7684\u5BC6\u94A5."}};function le(){return(document.documentElement.getAttribute("lang")||"").toLowerCase().indexOf("zh")===0?k.zh:k.en}D.exports={messages:le}});var a=E(),P=A(),ue=_(),f=L(),de=O(),pe="4",fe=25e4,m="[data-hbe-format],.hbe-container,#hexo-blog-encrypt",F="__hbeInit";function ye(){return!!(window.isSecureContext&&window.crypto&&window.crypto.subtle)}function he(e){let t=e.dataset,n=e.querySelector('script#hbeData,script[type="hbeData"]');return{format:t.hbeFormat,wpm:t.wpm||"Wrong password.",whm:t.whm||t.wpm||"Wrong password.",saltHex:t.salt,nonceHex:t.nonce,iterations:parseInt(t.kdfIterations,10)||fe,autoSave:t.autoSave==="true",ciphertextHex:n?n.textContent.trim():""}}function me(e,t){try{window.dispatchEvent(new CustomEvent("hexo-blog-decrypt",{detail:{mode:e,element:t}}))}catch(n){try{window.dispatchEvent(new Event("hexo-blog-decrypt"))}catch(r){}}}function ge(e){let t=location.pathname+location.search,n=Array.prototype.indexOf.call(document.querySelectorAll(m),e);return n>0?t+"#"+n:t}function be(e){if(e[F]||e.classList.contains("hbe-decrypted-content"))return;e[F]=!0;let t=he(e),n=de.messages();if(t.format!==pe){a.showError(e,n.incompatible);return}let r=a.findForm(e),o=a.findInput(e);a.ensureUniqueId(e,o),a.trackFilled(o);let c=ge(e);if(t.autoSave||f.clear(c),!ye()){a.showError(e,n.noCrypto),a.attachSubmit(r,()=>a.showError(e,n.noCrypto));return}let i=!1,u=async(s,l)=>{if(i)return;i=!0;let b=await ue.swapInDecryptedDOM(e,s);l==="cached"&&a.addForgetButton(b,n,()=>f.clear(c)),me(l,b)};a.attachSubmit(r,async()=>{a.clearError(e);let s=o?o.value:"";if(!s){a.showError(e,t.wpm),a.refocus(o);return}a.setBusy(r,o,!0);let l;try{l=await P.tryDecryptWithPassword({password:s,saltHex:t.saltHex,nonceHex:t.nonceHex,ciphertextHex:t.ciphertextHex,iterations:t.iterations})}finally{a.setBusy(r,o,!1)}if(!l.ok){a.showError(e,t.wpm),a.refocus(o);return}t.autoSave&&await f.save({id:c,key:l.key,saltHex:t.saltHex,nonceHex:t.nonceHex,autoSave:!0}),await u(l.plaintext,"manual")}),we(t,c,u).then(s=>{let l=document.activeElement;!s&&!i&&o&&(!l||l===document.body)&&o.focus()})}async function we(e,t,n){if(!e.autoSave)return!1;let r=await f.load({id:t,expectedSaltHex:e.saltHex});if(!r)return!1;let o=await P.tryDecryptWithKey({key:r,nonceHex:e.nonceHex,ciphertextHex:e.ciphertextHex});return o.ok?(await n(o.plaintext,"cached"),!0):(f.clear(t),!1)}function g(e){e=e||document;let t=Array.prototype.slice.call(e.querySelectorAll(m));e.matches&&e.matches(m)&&t.unshift(e);for(let n=0;n<t.length;n++)be(t[n])}if(!window.hexoBlogEncrypt){window.hexoBlogEncrypt={init:g},f.purgeLegacy();let e=()=>window.hexoBlogEncrypt.init();["pjax:complete","pjax:success","swup:contentReplaced","turbo:load"].forEach(t=>{document.addEventListener(t,e)})}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",()=>g()):g();})();
|
|
2
2
|
//# sourceMappingURL=hbe.bundle.js.map
|