Skip to main content

Module guard

Module guard 

Source
Expand description

请求守卫:交给处理器的一个共享加密句柄。

框架集成层(axum / actix-web / rocket / poem / salvo / warp / bee-rust / e-cat) 全都在这一层之上做薄适配 —— 把 Guard 按各自框架的惯例注入请求上下文, 处理器拿到它就能加解密,不必把主密钥或 Manager 一路透传下去。

use encryption::guard::Guard;

let master_key = [7u8; 32]; // 生产环境从环境变量 / KMS 读取
let guard = Guard::from_master_key(&master_key, "aes-256-gcm")?;

// 处理器里
let stored = guard.encrypt("13800138000".as_bytes())?;
assert_eq!(guard.decrypt(&stored)?, "13800138000".as_bytes());

§为什么是 Arc

每个请求都要拿到同一个 Manager,而框架普遍要求注入的状态是 Send + Sync + 'static 且常常要求 Clone。Manager 本身是只读的、构造后不再变,所以 Arc 共享即可 —— 子密钥只在构造时派生一次,每请求零成本。

Structs§

Guard
请求守卫:一个可跨请求共享、可跨线程克隆的加密句柄。