SecureUtil

Struct SecureUtil 

Source
pub struct SecureUtil;

Implementations§

Source§

impl SecureUtil

Source

pub fn base64_encode(bytes: &[u8]) -> String

对字节数组进行Base64编码

§参数
  • bytes - 需要编码的字节数组切片
§返回值

返回编码后的Base64字符串

Source

pub fn base64_decode(base64_str: &str) -> Result<Vec<u8>, DecodeError>

对Base64编码的字符串进行解码

§参数
  • base64_str - 需要解码的Base64编码字符串
§返回值
  • Ok(Vec<u8>) - 解码成功,返回解码后的字节数据
  • Err(DecodeError) - 解码失败,返回错误信息
§示例
let decoded = base64_decode("SGVsbG8=").unwrap();
assert_eq!(decoded, b"Hello");
Source

pub fn hmac_sha1(data: &[u8], key: &str) -> Vec<u8>

使用HMAC-SHA1算法对数据进行签名

该函数接收待签名的数据和密钥,使用HMAC-SHA1算法生成数字签名。

§参数
  • data - 待签名的字节数据切片
  • key - 用于签名的密钥字符串
§返回值

返回HMAC-SHA1签名结果的字节向量

§Panics

当密钥长度无效时会panic,但在实际中HMAC可以接受任意长度的密钥

Source

pub fn to_md5_str(bytes: &[u8]) -> String

将字节数组转换为MD5哈希字符串

该函数接收一个字节切片,计算其MD5哈希值,并将结果格式化为十六进制字符串。

§参数
  • bytes - 需要计算MD5哈希的字节数据切片
§返回值

返回表示MD5哈希值的十六进制字符串

§示例
let data = b"hello world";
let md5_str = to_md5_str(data);
assert_eq!(md5_str, "5eb63bbbe01eeed093cb22bb8f5acdc3");
Source

pub fn to_md5(bytes: &[u8]) -> Vec<u8>

计算字节数组的MD5哈希值

§参数
  • bytes - 需要计算MD5哈希值的字节数组切片
§返回值

返回计算得到的MD5哈希值,以字节数组形式表示

§示例
let data = b"hello world";
let hash = to_md5(data);

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, 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.