feat(stdlib): Mid M3 hash digests and secure tokens
Add lib/hash and lib/secrets (EN/ZH) with known-vector golds, getrandom-backed tokens, and public demos. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
94ae6713aa
commit
08384c7540
25 files changed
+489
-17
No files matched your search
+2
-1
@@ -3,9 +3,10 @@
|
||||
## Unreleased
|
||||
|
||||
### Added
|
||||
- **中层标准库 M3**:`lib/hash` / `哈希`(sha256/sha1/md5/hmac_sha256)与 `lib/secrets` / `机密`(token_hex/token_urlsafe);金样与 public `10-hash-secrets`。设计 [stdlib-hash.md](doc/design/stdlib-hash.md) · [stdlib-secrets.md](doc/design/stdlib-secrets.md)。
|
||||
- **中层标准库 M2**:`lib/path` / `lib/路径`(join/split/normalize/…)与 `fs`/`文件` 加厚(`copy_file`/`move`/`make_temp`);金样与 public `09-path-fs`。设计 [stdlib-path.md](doc/design/stdlib-path.md) · [stdlib-fs-mid.md](doc/design/stdlib-fs-mid.md)。
|
||||
- **中层标准库 M1**:`lib/re` / `lib/正则`(`is_match`/`find`/`find_all`/`replace`/`split`)与 `lib/encoding` / `lib/编码`(base64/hex);host `host_re_*` + `host_encoding_*`;金样与 public `08-re-encoding`。设计 [stdlib-mid.md](doc/design/stdlib-mid.md) · [stdlib-re.md](doc/design/stdlib-re.md) · [stdlib-encoding.md](doc/design/stdlib-encoding.md)。
|
||||
- **中层标准库(规划)**:通用原语沉 `lib/`(hash / csv …),减少「事事 ABI 插件」。路线 [stdlib-mid.md](doc/roadmap/stdlib-mid.md)。
|
||||
- **中层标准库(规划)**:通用原语沉 `lib/`(csv / text++ …),减少「事事 ABI 插件」。路线 [stdlib-mid.md](doc/roadmap/stdlib-mid.md)。
|
||||
- **`ext/linalg`**:L0–L6 + 公式文档面 **F1–F3**(中缀、`declare`、展示/`explicit` 默认化简、`T_ascii`、public「如何写公式文档」);示例 `linalg-transpose` · `linalg-svd` · `linalg-least-squares`。设计:[ext-linalg-formula-doc.md](doc/design/ext-linalg-formula-doc.md)。
|
||||
|
||||
### Fixed
|
||||
|
||||
Generated
+20
-5
@@ -465,9 +465,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-utils"
|
||||
version = "0.8.22"
|
||||
version = "0.8.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "61803da095bee82a81bb1a452ecc25d3b2f1416d1897eb86430c6159ef717c17"
|
||||
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
|
||||
|
||||
[[package]]
|
||||
name = "crypto-common"
|
||||
@@ -1031,14 +1031,19 @@ dependencies = [
|
||||
"chrono",
|
||||
"clap",
|
||||
"futures-util",
|
||||
"getrandom 0.2.17",
|
||||
"hmac 0.12.1",
|
||||
"idna_adapter",
|
||||
"include_dir",
|
||||
"libc",
|
||||
"libloading",
|
||||
"md-5 0.10.6",
|
||||
"predicates",
|
||||
"pulldown-cmark",
|
||||
"regex",
|
||||
"serde_json",
|
||||
"sha1",
|
||||
"sha2 0.10.9",
|
||||
"thiserror 1.0.69",
|
||||
"tiny_http",
|
||||
"tokio",
|
||||
@@ -1116,6 +1121,16 @@ version = "0.8.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3"
|
||||
|
||||
[[package]]
|
||||
name = "md-5"
|
||||
version = "0.10.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"digest 0.10.7",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "md-5"
|
||||
version = "0.11.0"
|
||||
@@ -1346,7 +1361,7 @@ dependencies = [
|
||||
"bytes",
|
||||
"fallible-iterator 0.2.0",
|
||||
"hmac 0.13.0",
|
||||
"md-5",
|
||||
"md-5 0.11.0",
|
||||
"memchr",
|
||||
"rand 0.10.2",
|
||||
"sha2 0.11.0",
|
||||
@@ -1734,9 +1749,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "sha1"
|
||||
version = "0.10.7"
|
||||
version = "0.10.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a978451301f4db1d02937a4ab3ccce137717b81826e79b7d49ffe3244a13c3b8"
|
||||
checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"cpufeatures 0.2.17",
|
||||
|
||||
@@ -32,6 +32,11 @@ pulldown-cmark = "0.12"
|
||||
serde_json = "1"
|
||||
thiserror = "1"
|
||||
tiny_http = { version = "0.12", optional = true }
|
||||
sha2 = { version = "=0.10.9", default-features = false }
|
||||
sha1 = { version = "=0.10.6", default-features = false }
|
||||
md-5 = { version = "=0.10.6", default-features = false }
|
||||
hmac = { version = "=0.12.1", default-features = false }
|
||||
getrandom = { version = "=0.2.17", default-features = false, features = ["std"] }
|
||||
# Mid M1: Unicode regex for lib/re (no backrefs; RE2-like).
|
||||
regex = { version = "=1.13.1", default-features = false, features = ["std", "unicode"] }
|
||||
# Pin for Cargo/rustc 1.81 (newer rustls/idna/zeroize need edition2024).
|
||||
|
||||
@@ -83,6 +83,8 @@ Marqdo 换 **Rust 或 C++** 都不改变「主前端不该是 Flex/Bison」这
|
||||
| **serde** + **serde_yaml** | 元信息 | 仅解析 frontmatter 中的 YAML **键值**;`import x:x.mq.md` 仍按**行**抽取(见 Spike) |
|
||||
| **ureq** (=2.9.7) | HTTP(S) | `lib/net`;为 Cargo 1.81 另钉 `idna_adapter`/`zeroize`(见根 `Cargo.toml`) |
|
||||
| **regex** (=1.13.1) | 正则 | Mid M1 `lib/re`;`default-features=false` + `std`/`unicode`;无回溯引用 |
|
||||
| **sha2** (=0.10.9) / **sha1** (=0.10.6) / **md-5** (=0.10.6) / **hmac** (=0.12.1) | 摘要 | Mid M3 `lib/hash` |
|
||||
| **getrandom** (=0.2.17) | OS 熵 | Mid M3 `lib/secrets` |
|
||||
|
||||
#### 强烈建议
|
||||
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
# `lib/hash` — 摘要与 HMAC(Mid M3)
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **已落地(M3)** |
|
||||
| 日期 | 2026-09-07 |
|
||||
| 相关 | [stdlib-mid.md](stdlib-mid.md) · [stdlib-secrets.md](stdlib-secrets.md) |
|
||||
| 导入 | `lib/hash.mq.md` · `lib/哈希.mq.md` |
|
||||
| Host | `host_hash_*`(`sha2` / `sha1` / `md-5` / `hmac`) |
|
||||
|
||||
---
|
||||
|
||||
## 1. API
|
||||
|
||||
| 英文 | 中文 | 形参 | 结果 |
|
||||
|------|------|------|------|
|
||||
| `sha256` | `哈希256` | `text` | 小写 hex |
|
||||
| `sha1` | `哈希1` | `text` | 小写 hex |
|
||||
| `md5` | `md5` | `text` | 小写 hex |
|
||||
| `hmac_sha256` | `密钥哈希256` | `key`, `text` | 小写 hex |
|
||||
|
||||
输入按 **UTF-8 字节**摘要。文档标明:用于完整性标识 / 缓存键,**不是**密码存储(无 Argon2/bcrypt)。
|
||||
|
||||
## 2. 刻意不做
|
||||
|
||||
密码哈希、流式大文件哈希 API、非 UTF-8 二进制类型。
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **规划中 · M1–M2 已实现** |
|
||||
| 状态 | **规划中 · M1–M3 已实现** |
|
||||
| 日期 | 2026-09-07 |
|
||||
| 相关 | [stdlib.md](stdlib.md) · [stdlib-modules.md](stdlib-modules.md) · [ext-abi.md](ext-abi.md) · [roadmap/stdlib-mid.md](../roadmap/stdlib-mid.md) |
|
||||
| 目标 | 在接下来数个发版周期内,把「通用编程」能力沉到 **L0.5 宿主 + L1 `.mq.md`**,减少「凡是稍有能力就必须 ABI 插件」的路径依赖 |
|
||||
@@ -120,7 +120,7 @@ OS / 算法库(精选 crate,默认 features 最小化)
|
||||
|------|-----------------|----------|----------|
|
||||
| **M1** | `re` / `正则` · `encoding` / `编码` | match / find / replace;base64 / hex | **done** |
|
||||
| **M2** | `path` / `路径` · `fs` 加厚 | join / normalize;copy / move / temp | **done** |
|
||||
| **M3** | `hash` / `哈希` · `secrets` / `机密` | sha256 / hmac;token | host + L1 |
|
||||
| **M3** | `hash` / `哈希` · `secrets` / `机密` | sha256 / hmac;token | **done** |
|
||||
| **M4** | `csv` / `逗号表` · `text` 加厚 | parse / stringify;contains / replace | host 或纯 mq |
|
||||
| **M5** | `listx` 或 `table` 加厚 · `cli` | sort / zip;flags | 优先纯 mq |
|
||||
| **M6** | `log` / `日志` · `uuid` / `标识` | level 日志;v4 | 薄 host 或纯 mq |
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **核心已落地**;**中层 Mid:M1–M2 已落地** · [stdlib-mid.md](stdlib-mid.md) / [roadmap/stdlib-mid.md](../roadmap/stdlib-mid.md) |
|
||||
| 状态 | **核心已落地**;**中层 Mid:M1–M3 已落地** · [stdlib-mid.md](stdlib-mid.md) / [roadmap/stdlib-mid.md](../roadmap/stdlib-mid.md) |
|
||||
| 日期 | 2026-09-07 |
|
||||
| 原则 | 全部经 frontmatter **导入**;除 JSON 外中英分文件;内核保持少而精;**通用能力优先 Mid,插件留给领域核** |
|
||||
| 相关 | [stdlib.md](stdlib.md) · [stdlib-i18n.md](stdlib-i18n.md) · [stdlib-mid.md](stdlib-mid.md) · [ext-abi.md](ext-abi.md) · [ext-llm.md](ext-llm.md) · [ext-agent.md](ext-agent.md) · [**module-namespace.md**](module-namespace.md)(导入命名空间;**M1/M2 已落地**:`库.成员` / `库.对象.成员` / `use`) |
|
||||
@@ -19,6 +19,8 @@
|
||||
| **正则** | `lib/re.mq.md` | `lib/正则.mq.md` | **Mid M1** |
|
||||
| **编解码** | `lib/encoding.mq.md` | `lib/编码.mq.md` | **Mid M1** |
|
||||
| **路径** | `lib/path.mq.md` | `lib/路径.mq.md` | **Mid M2** |
|
||||
| **哈希** | `lib/hash.mq.md` | `lib/哈希.mq.md` | **Mid M3** |
|
||||
| **机密** | `lib/secrets.mq.md` | `lib/机密.mq.md` | **Mid M3** |
|
||||
| 表 | `lib/table.mq.md` | `lib/表.mq.md` | 已有(`put`/`改` + list/map;见 [stdlib-table.md](stdlib-table.md)) |
|
||||
| 浏览器效应 | `lib/browser.mq.md` | `lib/浏览器.mq.md` | WASM 客户端效应助手(GFM + helpers;勿用 `json.set` 链) |
|
||||
| 文件 | `lib/fs.mq.md` | `lib/文件.mq.md` | 已有 + Mid M2:`copy_file` / `move` / `make_temp` |
|
||||
@@ -34,7 +36,7 @@
|
||||
|
||||
官方可选扩展(**非** stdlib):`ext/llm` · `ext/agent` · `ext/web` · `ext/quantum` · `ext/linalg` — 见各 `ext-*.md`。
|
||||
|
||||
**中层下一波候选(未落地):** `hash` / `secrets` / `csv` / … — 见 [stdlib-mid.md](stdlib-mid.md)。**已落地:** M1 `re`/`encoding` · M2 `path` + `fs` 加厚。
|
||||
**中层下一波候选(未落地):** `csv` / text 加厚 / … — 见 [stdlib-mid.md](stdlib-mid.md)。**已落地:** M1–M3(`re`/`encoding`/`path`/`fs+`/`hash`/`secrets`)。
|
||||
|
||||
设计细节:[stdlib-writeback.md](stdlib-writeback.md) · [stdlib-subtask.md](stdlib-subtask.md)。
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
# `lib/secrets` — 安全随机令牌(Mid M3)
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **已落地(M3)** |
|
||||
| 日期 | 2026-09-07 |
|
||||
| 相关 | [stdlib-hash.md](stdlib-hash.md) · [stdlib-mid.md](stdlib-mid.md) |
|
||||
| 导入 | `lib/secrets.mq.md` · `lib/机密.mq.md` |
|
||||
| Host | `host_secrets_*`(`getrandom`) |
|
||||
|
||||
---
|
||||
|
||||
## 1. API
|
||||
|
||||
| 英文 | 中文 | 形参 | 结果 |
|
||||
|------|------|------|------|
|
||||
| `token_hex` | `十六进制令牌` | 可选 `n`(字节数,默认 16) | 小写 hex,长度 `2*n` |
|
||||
| `token_urlsafe` | `网址安全令牌` | 可选 `n`(字节数,默认 16) | URL-safe Base64(无填充) |
|
||||
|
||||
**不要**用 `math.random` / `math.seed` 生成令牌。
|
||||
|
||||
## 2. 刻意不做
|
||||
|
||||
密钥派生、加密/解密、密码哈希。
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **M1–M2 已落地 · M3–M6 规划中** |
|
||||
| 状态 | **M1–M3 已落地 · M4–M6 规划中** |
|
||||
| 日期 | 2026-09-07 |
|
||||
| 设计 | [stdlib-mid.md](../design/stdlib-mid.md) |
|
||||
| 相关 | [stdlib-modules.md](../design/stdlib-modules.md) · [next-phase.md](next-phase.md) · [ext-cli.md](../design/ext-cli.md) |
|
||||
@@ -80,13 +80,16 @@ M6 可观测与标识 log + uuid(+ 可选 yaml)
|
||||
|
||||
---
|
||||
|
||||
### M3 — 哈希与机密
|
||||
### M3 — 哈希与机密 — **done**
|
||||
|
||||
| 交付 | 说明 |
|
||||
|------|------|
|
||||
| `lib/hash.mq.md` / `lib/哈希.mq.md` | `sha256` / `sha1` / `md5`(文档标明「完整性标识,非密码存储」)/ `hmac_sha256` |
|
||||
| `lib/secrets.mq.md` / `lib/机密.mq.md` | `token_hex` / `token_urlsafe`;**禁止**与 `math.random` 混用文档 |
|
||||
| 验收 | 已知向量金样;agent/web 若有手写摘要可改为依赖本库(可选跟进) |
|
||||
| `lib/hash.mq.md` / `lib/哈希.mq.md` | `sha256` / `sha1` / `md5` / `hmac_sha256` |
|
||||
| `lib/secrets.mq.md` / `lib/机密.mq.md` | `token_hex` / `token_urlsafe` |
|
||||
| Host | `host_hash_*`、`host_secrets_*`(`sha2`/`sha1`/`md-5`/`hmac`/`getrandom`) |
|
||||
| 金样 | `hash-smoke` · `哈希-烟测` · `secrets-smoke` · `机密-烟测` |
|
||||
| 文档 | [stdlib-hash.md](../design/stdlib-hash.md) · [stdlib-secrets.md](../design/stdlib-secrets.md) · public `10-hash-secrets` |
|
||||
| 验收 | 已知向量 + 令牌长度 |
|
||||
|
||||
**刻意不做:** 密码哈希(Argon2/bcrypt)、完整 TLS、证书管理——仍属服务端/安全产品。
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
---
|
||||
title: lib/hash
|
||||
description: Digests and HMAC (Mid M3). Integrity / cache keys — not password hashing.
|
||||
---
|
||||
|
||||
## sha256
|
||||
+ `text`
|
||||
|
||||
**> host_hash_sha256 text=`text`**
|
||||
|
||||
## sha1
|
||||
+ `text`
|
||||
|
||||
**> host_hash_sha1 text=`text`**
|
||||
|
||||
## md5
|
||||
+ `text`
|
||||
|
||||
**> host_hash_md5 text=`text`**
|
||||
|
||||
## hmac_sha256
|
||||
+ `key`
|
||||
+ `text`
|
||||
|
||||
**> host_hash_hmac_sha256 key=`key` text=`text`**
|
||||
@@ -0,0 +1,18 @@
|
||||
---
|
||||
title: lib/secrets
|
||||
description: Cryptographic tokens (Mid M3). Do not use math.random for secrets.
|
||||
---
|
||||
|
||||
## token_hex
|
||||
+ `n`=None
|
||||
|
||||
Byte length `n` (default 16). Returns lowercase hex of length `2*n`.
|
||||
|
||||
**> host_secrets_token_hex n=`n`**
|
||||
|
||||
## token_urlsafe
|
||||
+ `n`=None
|
||||
|
||||
Byte length `n` (default 16). URL-safe Base64 without padding.
|
||||
|
||||
**> host_secrets_token_urlsafe n=`n`**
|
||||
@@ -0,0 +1,25 @@
|
||||
---
|
||||
title: lib/哈希
|
||||
description: 摘要与 HMAC(中层 M3)。完整性 / 缓存键 — 非密码存储。
|
||||
---
|
||||
|
||||
## 哈希256
|
||||
+ `内容`
|
||||
|
||||
**> host_hash_sha256 text=`内容`**
|
||||
|
||||
## 哈希1
|
||||
+ `内容`
|
||||
|
||||
**> host_hash_sha1 text=`内容`**
|
||||
|
||||
## md5
|
||||
+ `内容`
|
||||
|
||||
**> host_hash_md5 text=`内容`**
|
||||
|
||||
## 密钥哈希256
|
||||
+ `密钥`
|
||||
+ `内容`
|
||||
|
||||
**> host_hash_hmac_sha256 key=`密钥` text=`内容`**
|
||||
@@ -0,0 +1,18 @@
|
||||
---
|
||||
title: lib/机密
|
||||
description: 安全随机令牌(中层 M3)。勿用 math.random 生成令牌。
|
||||
---
|
||||
|
||||
## 十六进制令牌
|
||||
+ `字节数`=None
|
||||
|
||||
字节长度(默认 16),返回长度为 `2*字节数` 的小写 hex。
|
||||
|
||||
**> host_secrets_token_hex n=`字节数`**
|
||||
|
||||
## 网址安全令牌
|
||||
+ `字节数`=None
|
||||
|
||||
字节长度(默认 16),URL-safe Base64(无填充)。
|
||||
|
||||
**> host_secrets_token_urlsafe n=`字节数`**
|
||||
+1
-1
@@ -14,7 +14,7 @@
|
||||
|
||||
官方扩展(非 stdlib):`ext/llm`、`ext/agent`、`ext/web`、`ext/quantum`、`ext/linalg` — 见 `features/05-*.mq.md`、`06-*.mq.md`、`07-linalg.mq.md` / `07-线性代数.mq.md` 与 `doc/design/ext-*.md`。
|
||||
|
||||
中层标准库(Mid):`features/08-re-encoding.mq.md` / `08-正则编解码.mq.md`;`09-path-fs.mq.md` / `09-路径文件.mq.md`。
|
||||
中层标准库(Mid):`08-re-encoding` · `09-path-fs` · `10-hash-secrets`(及对应中文页)。
|
||||
|
||||
**不放** `errors/`:失败样例只在 `tests/errors/`。
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
---
|
||||
title: Mid stdlib — hash and secrets
|
||||
description: lib/hash + lib/secrets without plugins (Mid M3)
|
||||
import hash:lib/hash.mq.md
|
||||
import sec:lib/secrets.mq.md
|
||||
---
|
||||
|
||||
# Digests and secure tokens
|
||||
|
||||
Integrity digests and cryptographic tokens stay in `lib/` — do not use `math.random` for secrets.
|
||||
|
||||
# main
|
||||
|
||||
*`d` = > hash.sha256 text="marqdo"*
|
||||
*`t` = > sec.token_hex n=8*
|
||||
*`L` = > len `t`*
|
||||
> print text=`d`
|
||||
> print text=`L`
|
||||
1. `L` == 16
|
||||
> print text=mid-m3-ok
|
||||
2. *
|
||||
> print text=mid-m3-fail
|
||||
@@ -0,0 +1,22 @@
|
||||
---
|
||||
title: 中层标准库 — 哈希与机密
|
||||
description: lib/哈希 + lib/机密,无需插件(中层 M3)
|
||||
导入 哈希:lib/哈希.mq.md
|
||||
导入 机密:lib/机密.mq.md
|
||||
---
|
||||
|
||||
# 摘要与安全令牌
|
||||
|
||||
完整性摘要与加密令牌进 `lib/` — 勿用 `math.random` 生成令牌。
|
||||
|
||||
# main
|
||||
|
||||
*`d` = > 哈希.哈希256 内容="marqdo"*
|
||||
*`t` = > 机密.十六进制令牌 字节数=8*
|
||||
*`L` = > 长度 `t`*
|
||||
> 打印 内容=`d`
|
||||
> 打印 内容=`L`
|
||||
1. `L` == 16
|
||||
> 打印 内容=mid-m3-ok
|
||||
2. *
|
||||
> 打印 内容=mid-m3-fail
|
||||
+39
-1
@@ -2,7 +2,9 @@
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::host::{encoding, fs, json, math, net, path, re, sys, time, writeback, HostContext};
|
||||
use crate::host::{
|
||||
encoding, fs, hash, json, math, net, path, re, secrets, sys, time, writeback, HostContext,
|
||||
};
|
||||
#[cfg(feature = "exec-host")]
|
||||
use crate::host::{foreign, subtask};
|
||||
#[cfg(feature = "plugin-host")]
|
||||
@@ -147,6 +149,12 @@ pub enum HostFn {
|
||||
FsCopyFile = 141,
|
||||
FsMove = 142,
|
||||
FsMakeTemp = 143,
|
||||
HashSha256 = 144,
|
||||
HashSha1 = 145,
|
||||
HashMd5 = 146,
|
||||
HashHmacSha256 = 147,
|
||||
SecretsTokenHex = 148,
|
||||
SecretsTokenUrlsafe = 149,
|
||||
}
|
||||
|
||||
|
||||
@@ -287,6 +295,12 @@ impl HostFn {
|
||||
141 => Self::FsCopyFile,
|
||||
142 => Self::FsMove,
|
||||
143 => Self::FsMakeTemp,
|
||||
144 => Self::HashSha256,
|
||||
145 => Self::HashSha1,
|
||||
146 => Self::HashMd5,
|
||||
147 => Self::HashHmacSha256,
|
||||
148 => Self::SecretsTokenHex,
|
||||
149 => Self::SecretsTokenUrlsafe,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
@@ -430,6 +444,12 @@ impl HostFn {
|
||||
"host_copy_file" | "copy_file" => Self::FsCopyFile,
|
||||
"host_move" | "move_path" => Self::FsMove,
|
||||
"host_make_temp" | "make_temp" => Self::FsMakeTemp,
|
||||
"host_hash_sha256" | "hash_sha256" => Self::HashSha256,
|
||||
"host_hash_sha1" | "hash_sha1" => Self::HashSha1,
|
||||
"host_hash_md5" | "hash_md5" => Self::HashMd5,
|
||||
"host_hash_hmac_sha256" | "hash_hmac_sha256" => Self::HashHmacSha256,
|
||||
"host_secrets_token_hex" | "secrets_token_hex" => Self::SecretsTokenHex,
|
||||
"host_secrets_token_urlsafe" | "secrets_token_urlsafe" => Self::SecretsTokenUrlsafe,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
@@ -560,6 +580,12 @@ impl HostFn {
|
||||
Self::FsCopyFile => "host_copy_file",
|
||||
Self::FsMove => "host_move",
|
||||
Self::FsMakeTemp => "host_make_temp",
|
||||
Self::HashSha256 => "host_hash_sha256",
|
||||
Self::HashSha1 => "host_hash_sha1",
|
||||
Self::HashMd5 => "host_hash_md5",
|
||||
Self::HashHmacSha256 => "host_hash_hmac_sha256",
|
||||
Self::SecretsTokenHex => "host_secrets_token_hex",
|
||||
Self::SecretsTokenUrlsafe => "host_secrets_token_urlsafe",
|
||||
Self::WritebackRecord => "host_writeback_record",
|
||||
Self::WritebackGet => "host_writeback_get",
|
||||
Self::WritebackClear => "host_writeback_clear",
|
||||
@@ -662,6 +688,9 @@ impl HostFn {
|
||||
| Self::PathIsAbsolute => &["path"],
|
||||
Self::FsCopyFile | Self::FsMove => &["src", "dest"],
|
||||
Self::FsMakeTemp => &[],
|
||||
Self::HashSha256 | Self::HashSha1 | Self::HashMd5 => &["text"],
|
||||
Self::HashHmacSha256 => &["key", "text"],
|
||||
Self::SecretsTokenHex | Self::SecretsTokenUrlsafe => &[],
|
||||
Self::WritebackRecord => &["value"],
|
||||
Self::WritebackGet | Self::WritebackClear => &[],
|
||||
Self::WritebackList => &[],
|
||||
@@ -696,6 +725,7 @@ impl HostFn {
|
||||
Self::ListSlice => &["end"],
|
||||
Self::ReReplace => &["count"],
|
||||
Self::FsMakeTemp => &["prefix"],
|
||||
Self::SecretsTokenHex | Self::SecretsTokenUrlsafe => &["n"],
|
||||
_ => &[],
|
||||
}
|
||||
}
|
||||
@@ -1091,6 +1121,14 @@ pub fn call_host(
|
||||
}
|
||||
HostFn::FsMove => fs::move_path(ctx, require(bound, "src")?, require(bound, "dest")?),
|
||||
HostFn::FsMakeTemp => fs::make_temp(ctx, bound.get("prefix")),
|
||||
HostFn::HashSha256 => hash::sha256(require(bound, "text")?),
|
||||
HostFn::HashSha1 => hash::sha1(require(bound, "text")?),
|
||||
HostFn::HashMd5 => hash::md5(require(bound, "text")?),
|
||||
HostFn::HashHmacSha256 => {
|
||||
hash::hmac_sha256(require(bound, "key")?, require(bound, "text")?)
|
||||
}
|
||||
HostFn::SecretsTokenHex => secrets::token_hex(bound.get("n")),
|
||||
HostFn::SecretsTokenUrlsafe => secrets::token_urlsafe(bound.get("n")),
|
||||
HostFn::OuterCallLine => Ok(Value::Int(
|
||||
ctx.call_site_lines
|
||||
.first()
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
//! Digest / HMAC host primitives for Mid M3 (`lib/hash`).
|
||||
|
||||
use hmac::{Hmac, Mac};
|
||||
use md5::Md5;
|
||||
use sha1::Sha1;
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
use crate::value::Value;
|
||||
|
||||
type HmacSha256 = Hmac<Sha256>;
|
||||
|
||||
fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> {
|
||||
match v {
|
||||
Value::Text(s) => Ok(s.as_str()),
|
||||
_ => Err(format!("{label} needs text")),
|
||||
}
|
||||
}
|
||||
|
||||
fn to_hex(bytes: &[u8]) -> String {
|
||||
bytes.iter().map(|b| format!("{b:02x}")).collect()
|
||||
}
|
||||
|
||||
pub fn sha256(text: &Value) -> Result<Value, String> {
|
||||
let t = as_text(text, "text")?;
|
||||
let mut h = Sha256::new();
|
||||
h.update(t.as_bytes());
|
||||
Ok(Value::Text(to_hex(&h.finalize())))
|
||||
}
|
||||
|
||||
pub fn sha1(text: &Value) -> Result<Value, String> {
|
||||
let t = as_text(text, "text")?;
|
||||
let mut h = Sha1::new();
|
||||
h.update(t.as_bytes());
|
||||
Ok(Value::Text(to_hex(&h.finalize())))
|
||||
}
|
||||
|
||||
pub fn md5(text: &Value) -> Result<Value, String> {
|
||||
let t = as_text(text, "text")?;
|
||||
let mut h = Md5::new();
|
||||
h.update(t.as_bytes());
|
||||
Ok(Value::Text(to_hex(&h.finalize())))
|
||||
}
|
||||
|
||||
pub fn hmac_sha256(key: &Value, text: &Value) -> Result<Value, String> {
|
||||
let k = as_text(key, "key")?;
|
||||
let t = as_text(text, "text")?;
|
||||
let mut mac =
|
||||
HmacSha256::new_from_slice(k.as_bytes()).map_err(|e| format!("hmac_sha256: {e}"))?;
|
||||
mac.update(t.as_bytes());
|
||||
Ok(Value::Text(to_hex(&mac.finalize().into_bytes())))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn sha256_empty() {
|
||||
let v = sha256(&Value::Text("".into())).unwrap();
|
||||
assert_eq!(
|
||||
v,
|
||||
Value::Text(
|
||||
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855".into()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -12,11 +12,13 @@ pub mod event_bus;
|
||||
#[cfg(feature = "exec-host")]
|
||||
pub mod foreign;
|
||||
mod fs;
|
||||
mod hash;
|
||||
pub(crate) mod json;
|
||||
pub mod math;
|
||||
mod net;
|
||||
mod path;
|
||||
mod re;
|
||||
mod secrets;
|
||||
#[cfg(feature = "plugin-host")]
|
||||
pub mod plugin;
|
||||
#[cfg(not(feature = "plugin-host"))]
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
//! Cryptographic token helpers for Mid M3 (`lib/secrets`).
|
||||
|
||||
use crate::value::Value;
|
||||
|
||||
fn nbytes(n: Option<&Value>) -> Result<usize, String> {
|
||||
match n {
|
||||
None | Some(Value::None) => Ok(16),
|
||||
Some(Value::Int(i)) => {
|
||||
if *i <= 0 {
|
||||
return Err("n must be positive".into());
|
||||
}
|
||||
if *i > 1024 {
|
||||
return Err("n too large (max 1024)".into());
|
||||
}
|
||||
Ok(*i as usize)
|
||||
}
|
||||
Some(_) => Err("n needs int".into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn fill_random(buf: &mut [u8]) -> Result<(), String> {
|
||||
getrandom::getrandom(buf).map_err(|e| format!("secrets: getrandom failed: {e}"))
|
||||
}
|
||||
|
||||
pub fn token_hex(n: Option<&Value>) -> Result<Value, String> {
|
||||
let len = nbytes(n)?;
|
||||
let mut buf = vec![0u8; len];
|
||||
fill_random(&mut buf)?;
|
||||
Ok(Value::Text(
|
||||
buf.iter().map(|b| format!("{b:02x}")).collect(),
|
||||
))
|
||||
}
|
||||
|
||||
const URLSAFE: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
||||
|
||||
pub fn token_urlsafe(n: Option<&Value>) -> Result<Value, String> {
|
||||
let len = nbytes(n)?;
|
||||
let mut buf = vec![0u8; len];
|
||||
fill_random(&mut buf)?;
|
||||
// URL-safe Base64 without padding.
|
||||
let mut out = String::with_capacity((len + 2) / 3 * 4);
|
||||
let mut i = 0;
|
||||
while i + 3 <= buf.len() {
|
||||
let n = ((buf[i] as u32) << 16) | ((buf[i + 1] as u32) << 8) | (buf[i + 2] as u32);
|
||||
out.push(URLSAFE[((n >> 18) & 63) as usize] as char);
|
||||
out.push(URLSAFE[((n >> 12) & 63) as usize] as char);
|
||||
out.push(URLSAFE[((n >> 6) & 63) as usize] as char);
|
||||
out.push(URLSAFE[(n & 63) as usize] as char);
|
||||
i += 3;
|
||||
}
|
||||
let rem = buf.len() - i;
|
||||
if rem == 1 {
|
||||
let n = (buf[i] as u32) << 16;
|
||||
out.push(URLSAFE[((n >> 18) & 63) as usize] as char);
|
||||
out.push(URLSAFE[((n >> 12) & 63) as usize] as char);
|
||||
} else if rem == 2 {
|
||||
let n = ((buf[i] as u32) << 16) | ((buf[i + 1] as u32) << 8);
|
||||
out.push(URLSAFE[((n >> 18) & 63) as usize] as char);
|
||||
out.push(URLSAFE[((n >> 12) & 63) as usize] as char);
|
||||
out.push(URLSAFE[((n >> 6) & 63) as usize] as char);
|
||||
}
|
||||
Ok(Value::Text(out))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn token_hex_len() {
|
||||
let v = token_hex(Some(&Value::Int(8))).unwrap();
|
||||
match v {
|
||||
Value::Text(s) => assert_eq!(s.len(), 16),
|
||||
_ => panic!("expected text"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1095,6 +1095,32 @@ fn lib_fs_copy_zh() {
|
||||
assert_out("tests/lib/文件-复制.mq.md", "zh-body");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lib_hash_smoke() {
|
||||
assert_out(
|
||||
"tests/lib/hash-smoke.mq.md",
|
||||
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\nba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad\n5d41402abc4b2a76b9719d911017c592\nf7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lib_hash_smoke_zh() {
|
||||
assert_out(
|
||||
"tests/lib/哈希-烟测.mq.md",
|
||||
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n5d41402abc4b2a76b9719d911017c592",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lib_secrets_smoke() {
|
||||
assert_out("tests/lib/secrets-smoke.mq.md", "16\n12");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lib_secrets_smoke_zh() {
|
||||
assert_out("tests/lib/机密-烟测.mq.md", "16");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lib_re_smoke() {
|
||||
assert_out(
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
---
|
||||
title: lib/hash smoke
|
||||
import hash:lib/hash.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
*s = > hash.sha256 text=""*
|
||||
> print text=`s`
|
||||
|
||||
*a = > hash.sha256 text="abc"*
|
||||
> print text=`a`
|
||||
|
||||
*m = > hash.md5 text="hello"*
|
||||
> print text=`m`
|
||||
|
||||
*h = > hash.hmac_sha256 key="key" text="The quick brown fox jumps over the lazy dog"*
|
||||
> print text=`h`
|
||||
@@ -0,0 +1,14 @@
|
||||
---
|
||||
title: lib/secrets smoke
|
||||
import sec:lib/secrets.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
*t = > sec.token_hex n=8*
|
||||
*L = > len `t`*
|
||||
> print text=`L`
|
||||
|
||||
*u = > sec.token_urlsafe n=9*
|
||||
*ul = > len `u`*
|
||||
> print text=`ul`
|
||||
@@ -0,0 +1,12 @@
|
||||
---
|
||||
title: lib/哈希 烟测
|
||||
导入 哈希:lib/哈希.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
*s = > 哈希.哈希256 内容=""*
|
||||
> 打印 内容=`s`
|
||||
|
||||
*m = > 哈希.md5 内容="hello"*
|
||||
> 打印 内容=`m`
|
||||
@@ -0,0 +1,10 @@
|
||||
---
|
||||
title: lib/机密 烟测
|
||||
导入 机密:lib/机密.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
*t = > 机密.十六进制令牌 字节数=8*
|
||||
*L = > 长度 `t`*
|
||||
> 打印 内容=`L`
|
||||
Reference in new issue
Block a user