Skip to main content

Crypto

Struct Crypto 

Source
pub struct Crypto;
Expand description

加密解密工具集

提供 SHA-256、HMAC、AES-256-GCM、Argon2 密码哈希、Base64 URL 编解码、 随机密钥/Token 生成等常用密码学操作。

Implementations§

Source§

impl Crypto

Source

pub fn sha256(data: &str) -> String

SHA-256 哈希(返回 hex 编码,64 字符)

Source

pub fn hmac_sha256(key: &[u8], data: &str) -> String

HMAC-SHA256 签名(返回 hex 编码)

Source

pub fn aes_encrypt(key: &[u8], plaintext: &str) -> Option<(String, String)>

AES-256-GCM 加密

  • key: 32 字节密钥,若非 32 字节返回 None
  • 返回 (Base64 密文, Base64 nonce) 元组
Source

pub fn aes_decrypt( key: &[u8], ciphertext_b64: &str, nonce_b64: &str, ) -> Option<String>

AES-256-GCM 解密(与 aes_encrypt 配对使用)

Source

pub fn hash_password(password: &str) -> Result<String, Error>

Argon2 密码哈希(自动生成随机盐)

Source

pub fn verify_password(password: &str, hash: &str) -> Result<bool, Error>

验证密码是否匹配 Argon2 哈希

Source

pub fn base64_url_encode(data: &[u8]) -> String

Base64 URL 安全编码(无填充,适合放在 URL/文件名中)

Source

pub fn base64_url_decode(s: &str) -> Option<Vec<u8>>

Base64 URL 安全解码

Source

pub fn random_key() -> Vec<u8>

生成 32 字节随机密钥(用于 AES-256-GCM)

Source

pub fn random_token(len: usize) -> String

生成随机 hex Token

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more