pub struct Crypto;Expand description
加密解密工具集
提供 SHA-256、HMAC、AES-256-GCM、Argon2 密码哈希、Base64 URL 编解码、 随机密钥/Token 生成等常用密码学操作。
Implementations§
Source§impl Crypto
impl Crypto
Sourcepub fn hmac_sha256(key: &[u8], data: &str) -> String
pub fn hmac_sha256(key: &[u8], data: &str) -> String
HMAC-SHA256 签名(返回 hex 编码)
Sourcepub fn aes_encrypt(key: &[u8], plaintext: &str) -> Option<(String, String)>
pub fn aes_encrypt(key: &[u8], plaintext: &str) -> Option<(String, String)>
AES-256-GCM 加密
key: 32 字节密钥,若非 32 字节返回None- 返回
(Base64 密文, Base64 nonce)元组
Sourcepub fn aes_decrypt(
key: &[u8],
ciphertext_b64: &str,
nonce_b64: &str,
) -> Option<String>
pub fn aes_decrypt( key: &[u8], ciphertext_b64: &str, nonce_b64: &str, ) -> Option<String>
AES-256-GCM 解密(与 aes_encrypt 配对使用)
Sourcepub fn base64_url_encode(data: &[u8]) -> String
pub fn base64_url_encode(data: &[u8]) -> String
Base64 URL 安全编码(无填充,适合放在 URL/文件名中)
Sourcepub fn random_key() -> Vec<u8> ⓘ
pub fn random_key() -> Vec<u8> ⓘ
生成 32 字节随机密钥(用于 AES-256-GCM)
Sourcepub fn random_token(len: usize) -> String
pub fn random_token(len: usize) -> String
生成随机 hex Token
Auto Trait Implementations§
impl Freeze for Crypto
impl RefUnwindSafe for Crypto
impl Send for Crypto
impl Sync for Crypto
impl Unpin for Crypto
impl UnsafeUnpin for Crypto
impl UnwindSafe for Crypto
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more