SecureUtil

Struct SecureUtil 

Source
pub struct SecureUtil;
Expand description

加解密工具,摘要算法等也归集次此工具类中

Implementations§

Source§

impl SecureUtil

Source

pub fn aes_random_key() -> String

TODO 待完善

Source

pub fn aes_encode(bytes: &[u8], key: &str) -> String

TODO 待完善

Source

pub fn aes_decode(decode_str: &str, key: &str) -> Vec<u8>

TODO 待完善

Source

pub fn rsa_random_key() -> (RsaPublicKey, RsaPrivateKey)

生成RSA随机密钥对

该函数使用系统随机数生成器创建一个2048位的RSA密钥对。

§返回值
  • RsaPublicKey - RSA公钥
  • RsaPrivateKey - RSA私钥
§Panics

当密钥生成失败时会panic

Source

pub fn rsa_encode(pub_key: &RsaPublicKey, bytes: &[u8]) -> Vec<u8>

使用RSA公钥对字节数据进行加密

该函数使用PKCS#1 v1.5填充方案对输入的字节数据进行RSA加密操作。 加密过程中会使用随机数生成器来确保加密的安全性。

§参数
  • pub_key - RSA公钥引用,用于执行加密操作
  • bytes - 需要加密的原始字节数据
§返回值

返回加密后的字节数据向量

§Panics

当加密操作失败时会panic,例如输入数据过长等情况

Source

pub fn rsa_decode(priv_key: &RsaPrivateKey, enc_data: &[u8]) -> Vec<u8>

使用RSA私钥解密数据

§参数
  • priv_key - RSA私钥引用,用于解密操作
  • enc_data - 待解密的加密数据字节切片
§返回值

返回解密后的原始数据字节向量

§Panics

当解密操作失败时会panic

Source

pub fn rsa_private_key_to_pkcs8(priv_key: &RsaPrivateKey) -> String

将RSA私钥转换为PKCS#8格式的PEM字符串

该函数接收一个RSA私钥对象,将其序列化为PKCS#8格式的PEM编码字符串。 PKCS#8是一种标准的私钥存储格式,使用PEM编码便于文本传输和存储。

§参数
  • priv_key - 要转换的RSA私钥引用
§返回值

返回PKCS#8格式的PEM编码字符串

§Panics

当私钥转换失败时会panic,因为使用了unwrap()

Source

pub fn rsa_public_key_to_pkcs8(pub_key: &RsaPublicKey) -> String

将RSA公钥转换为PKCS#8格式的PEM字符串

该函数接收一个RSA公钥对象,将其序列化为PEM格式的字符串, 使用LF作为换行符

§参数
  • pub_key - 要转换的RSA公钥引用
§返回值

返回PEM格式的PKCS#8公钥字符串

§Panics

当公钥转换失败时会panic(通过unwrap调用)

Source

pub fn rsa_private_key_from_pkcs8(pkcs8_string: &str) -> RsaPrivateKey

从PKCS#8格式的PEM字符串中解析RSA私钥

该函数接收一个PKCS#8格式的PEM编码字符串,将其解析为RsaPrivateKey对象。

§参数
  • pkcs8_string - 包含PKCS#8格式RSA私钥的PEM编码字符串
§返回值

返回解析得到的RsaPrivateKey对象

§Panics

当PEM字符串格式不正确或无法解析时,函数会panic

Source

pub fn rsa_public_key_from_pkcs8(pkcs8_string: &str) -> RsaPublicKey

从PKCS8格式的字符串创建RSA公钥

该函数接收一个PKCS8格式的PEM编码字符串,将其解析为RSA公钥对象。

§参数
  • pkcs8_string - PKCS8格式的PEM编码字符串
§返回值
  • RsaPublicKey - 解析得到的RSA公钥对象
§注意

该函数使用unwrap()处理结果,如果解析失败会panic

Source

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

计算字节数组的SHA256哈希值并返回十六进制字符串

固定生成256 位(32 字节) 的哈希值,以 64 位十六进制字符串形式展示

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

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

Source

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

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

👎Deprecated: This function is deprecated, please use md5_string instead

将字节数组转换为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> 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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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
Source§

impl<T> ErasedDestructor for T
where T: 'static,