encryption-rust 1.0.1

可插拔密码学组件库的 Rust 实现:在统一契约下提供对称加密、非对称加密、哈希与密钥派生(HKDF / PBKDF2),包含 AES/Sodium 与国密 SM2/SM3/SM4/ZUC 等实现
Documentation
# Copyright (c) 2026 erik <erik@erik.xyz> — https://erik.xyz

[package]
name = "encryption-rust"
version = "1.0.1"
edition = "2024"
rust-version = "1.88"
description = "可插拔密码学组件库的 Rust 实现:在统一契约下提供对称加密、非对称加密、哈希与密钥派生(HKDF / PBKDF2),包含 AES/Sodium 与国密 SM2/SM3/SM4/ZUC 等实现"
license = "MIT"
repository = "https://github.com/erikwang2013/encryption-rust"
authors = ["erik <erik@erik.xyz>"]
keywords = ["encryption", "cryptography", "sm2", "sm3", "sm4"]
categories = ["cryptography"]
readme = "README.md"

# 随包只发「读代码 / 用库」要用的东西。`docs/pet.svg` 绝不能排 —— src/pet.rs 用
# include_str! 内联它,排掉就编译不过(cargo package 会当场报错,不会静默漏发)。
exclude = [
    "CLAUDE.md",   # 仓库的 agent 配置,与使用者无关
    "AGENTS.md",
    "ruvector.db",
    ".swarm",
    ".claude-flow",
]

[lib]
name = "encryption"

[dev-dependencies]
# 只给 examples/ 演示「密文存库前转文本」用(库本身产出/接受二进制)。
base64 = "0.22"

# 只给框架适配层的测试用(各自 feature 打开时才编译那些测试)。这些是
# dev-dependencies,不会进使用者的依赖图。
tokio = { version = "1", features = ["macros", "rt"] }
tower = { version = "0.5", features = ["util"] }
# warp 的 `test` 模块(warp::test::request)挂在它的 `test` feature 后面,
# 而 warp 的默认 feature 是空集。放在 dev-dependencies 里打开它,
# 使用者的构建不会因此多拿任何东西。
warp = { version = "0.4", features = ["test"] }

[dependencies]
# 加密 / 哈希 / KDF 统一走 RustCrypto「cipher 0.4 / digest 0.10」代际 —— 与兄弟项目
# encryptable-rust 在本机验证过的组合一致,避免与 cipher 0.5 代际混用时的 trait 不匹配。
zeroize = "1"                    # 密钥材质 Drop 清零(src/key.rs)
rand_core = { version = "0.6", features = ["getrandom"] }   # 随机 IV / nonce(OsRng)

aes-gcm = "0.10"                 # aes-256-gcm(认证加密,工厂默认算法)
aes = "0.8"                      # aes-256-cbc-hmac 的底层块密码
cbc = { version = "0.1", features = ["alloc", "block-padding"] }  # CBC 模式 + PKCS#7
hmac = "0.12"                    # encrypt-then-MAC 与子密钥派生(HMAC-SHA256)
sha2 = "0.10"                    # SHA-256(HMAC / HKDF / PBKDF2 的底层)
chacha20poly1305 = "0.10"        # sodium-xchacha20(纯 Rust 实现,恒可用)
hkdf = "0.12"                    # hkdf-sha256(RFC 5869)
pbkdf2 = "0.12"                  # pbkdf2-sha256

sm2 = { version = "0.14", default-features = false, features = [
    "arithmetic",        # 椭圆曲线运算(加解密的前提)
    "pke",               # 公钥加密(C1C3C2 / C1C2C3)
    "alloc",             # encrypt/decrypt 返回 Vec<u8>
    "std",
    "precomputed-tables", # 基点预计算表,加速密钥生成与加解密
] }                  # 国密 SM2 非对称加解密
sm3 = "0.5"                      # 国密 SM3 哈希(与 sm2 内部同版本,避免重复依赖)
sm4 = "0.5"                      # 国密 SM4-CBC
zuc = "0.4"                      # ZUC-128 流密码
# SM2 的密钥生成与加密需要外部随机源;SysRng 即 rand_core 0.10 体系的操作系统随机
getrandom = { version = "0.4", features = ["sys_rng"] }

# ── 框架集成(全部 opt-in)────────────────────────────────────────────────
# 一律保留各框架的默认 feature:适配层要用的是它们的提取器 / 守卫 / 路由核心,
# 而那几块常常挂在默认 feature 上。反正这些依赖只在用户主动开启对应 feature
# 时才进图,且用户自己的框架依赖本来就会把默认 feature 拉进来(Cargo 取并集),
# 在这里关掉除了让 `cargo check --features <那一个>` 编译不过之外没有收益。
axum = { version = "0.8", optional = true }
actix-web = { version = "4", optional = true }
rocket = { version = "0.5", optional = true }
poem = { version = "3", optional = true }
salvo = { version = "1", optional = true }
warp = { version = "0.4", optional = true }
# bee-rust 的路由/上下文层
bee_router = { version = "1", optional = true, default-features = false }
# e-cat 的中间件层(其 feature 只有 cors / redis,默认不额外拉东西)
ecat-middleware = { version = "4", optional = true, default-features = false }
# e-cat 的 guard 要写成 tower Layer/Service,这两个 crate 提供那两个 trait
tower = { version = "0.5", optional = true }
http = { version = "1", optional = true }

[features]
default = []

# ── 框架集成 ────────────────────────────────────────────────────────────────
# 每个都是 opt-in:默认构建一个框架都不拉。它们做的事都一样 —— 把 Guard
# (src/guard.rs,框架无关)按各自框架的惯例注入请求上下文,仅此而已。
#
# 注意 Cargo 的 feature 是**并集**:同一个二进制里同时开两个框架特性时,
# 两个都会进依赖图。生产构建只开你真正在用的那个。
axum = ["dep:axum"]
actix-web = ["dep:actix-web"]
rocket = ["dep:rocket"]
poem = ["dep:poem"]
salvo = ["dep:salvo"]
warp = ["dep:warp"]
# bee-rust 的路由层(bee_router)直接吃 axum handler,状态也走 axum 的 State,
# 所以 axum 适配层在这里原样可用,顺带把它带上。
bee-rust = ["axum", "dep:bee_router"]
# e-cat 同样是 axum + tower 的组合:HTTP 传输层是 axum,中间件是 tower Layer。
# 它的 guard 形态是「往请求扩展里塞东西」,多需要 tower 与 http 两个 trait 来源。
ecat = ["axum", "dep:ecat-middleware", "dep:tower", "dep:http"]