Enum KdfPasswordAlgorithm

Source
pub enum KdfPasswordAlgorithm {
    Argon2(Option<Argon2Params>),
    Pbkdf2 {
        hash: HashAlgorithm,
        c: Option<u32>,
    },
}
Expand description

Password-based Key Derivation Function algorithm enumeration.

基于密码的密钥派生函数算法枚举。

§Algorithm Selection | 算法选择

Choose based on your security and compatibility requirements:

根据您的安全性和兼容性要求选择:

  • Argon2: Recommended for new applications (better security)

  • PBKDF2: Use for legacy compatibility or resource constraints

  • Argon2: 推荐用于新应用(更好的安全性)

  • PBKDF2: 用于遗留兼容性或资源约束

Variants§

§

Argon2(Option<Argon2Params>)

Argon2 memory-hard password hashing function.

Argon2 内存困难密码哈希函数。

§Properties | 属性
  • Type: Memory-hard function
  • Standard: RFC 9106
  • Resistance: GPU, FPGA, ASIC attacks
  • Parameters: Memory, time, parallelism costs
§Variants | 变体
  • Some(params): Custom parameters for specific requirements

  • None: Default parameters suitable for most applications

  • Some(params): 特定要求的自定义参数

  • None: 适用于大多数应用的默认参数

§

Pbkdf2

PBKDF2 time-hard password-based key derivation function.

PBKDF2 时间困难的基于密码的密钥派生函数。

§Properties | 属性
  • Type: Time-hard function
  • Standard: RFC 2898, PKCS #5
  • Resistance: Basic brute-force attacks
  • Parameters: Hash function, iteration count
§Configuration | 配置
  • hash: Underlying hash function (SHA-256/384/512)

  • c: Iteration count (Some(count) for custom, None for default)

  • hash: 底层哈希函数(SHA-256/384/512)

  • c: 迭代次数(Some(count) 自定义,None 默认)

Implementations§

Source§

impl KdfPasswordAlgorithm

Source

pub fn build() -> KdfPasswordAlgorithmBuilder

Creates a new password-based KDF algorithm builder.

创建新的基于密码的 KDF 算法构建器。

§Returns | 返回值

A builder that provides access to different password-based KDF algorithms. Use the builder methods to select the specific algorithm and parameters.

提供访问不同基于密码的 KDF 算法的构建器。 使用构建器方法选择特定的算法和参数。

§Examples | 示例
use seal_crypto_wrapper::algorithms::kdf::passwd::KdfPasswordAlgorithm;

// Default configurations
let argon2 = KdfPasswordAlgorithm::build().argon2_default();
let pbkdf2 = KdfPasswordAlgorithm::build().pbkdf2_sha256_default();

// Custom configurations
let custom_argon2 = KdfPasswordAlgorithm::build().argon2_with_params(65536, 3, 4);
let custom_pbkdf2 = KdfPasswordAlgorithm::build().pbkdf2_sha256_with_params(100000);
Source§

impl KdfPasswordAlgorithm

Source

pub fn into_wrapper(self) -> KdfPasswordWrapper

Converts the algorithm enum into a concrete wrapper implementation.

将算法枚举转换为具体的包装器实现。

§Purpose | 目的

This method creates a wrapper that implements the password-based KDF algorithm trait, enabling actual cryptographic operations like key derivation from passwords with appropriate computational cost to resist brute-force attacks.

此方法创建一个实现基于密码的 KDF 算法 trait 的包装器, 启用实际的密码操作,如从密码派生密钥,并具有适当的计算成本以抵抗暴力攻击。

§Returns | 返回值

A KdfPasswordWrapper that can perform:

  • Password-based key derivation
  • Salt-based key separation
  • Configurable computational cost
  • Secure password verification

可以执行以下操作的 KdfPasswordWrapper

  • 基于密码的密钥派生
  • 基于盐的密钥分离
  • 可配置的计算成本
  • 安全密码验证
§Examples | 示例
use seal_crypto_wrapper::algorithms::kdf::passwd::KdfPasswordAlgorithm;
use seal_crypto_wrapper::prelude::SecretBox;

// Argon2 for new applications (recommended)
let argon2_alg = KdfPasswordAlgorithm::build().argon2_default();
let argon2_kdf = argon2_alg.into_wrapper();

let password = SecretBox::new(Box::from(b"my-secret-password".as_slice()));
let salt = b"random_salt_16_bytes";
let derived_key = argon2_kdf.derive(&password, salt, 32)?;

// PBKDF2 for compatibility
let pbkdf2_alg = KdfPasswordAlgorithm::build().pbkdf2_sha256_with_params(100000);
let pbkdf2_kdf = pbkdf2_alg.into_wrapper();

let key2 = pbkdf2_kdf.derive(&password, salt, 32)?;
§Security Best Practices | 安全最佳实践

When using the wrapper:

  1. Use Random Salts: Generate unique salts for each password
  2. Sufficient Cost: Use appropriate computational cost parameters
  3. Secure Storage: Store salts and derived keys securely
  4. Regular Updates: Update cost parameters as hardware improves

使用包装器时:

  1. 使用随机盐: 为每个密码生成唯一盐
  2. 足够成本: 使用适当的计算成本参数
  3. 安全存储: 安全存储盐和派生密钥
  4. 定期更新: 随着硬件改进更新成本参数
§Algorithm-Specific Notes | 算法特定注意事项
§Argon2
  • Memory-hard: requires significant RAM
  • Resistant to GPU/ASIC attacks
  • Configurable memory/time/parallelism
§PBKDF2
  • Time-hard: only CPU time cost
  • Widely supported and standardized
  • Vulnerable to specialized hardware attacks
§Argon2
  • 内存困难:需要大量 RAM
  • 抗 GPU/ASIC 攻击
  • 可配置内存/时间/并行度
§PBKDF2
  • 时间困难:仅 CPU 时间成本
  • 广泛支持和标准化
  • 易受专用硬件攻击
§Performance Considerations | 性能考虑
  • Argon2: Higher memory usage, better security

  • PBKDF2: Lower memory usage, faster on some platforms

  • Parameter Tuning: Test on target hardware for optimal settings

  • Argon2: 更高内存使用,更好安全性

  • PBKDF2: 更低内存使用,在某些平台上更快

  • 参数调优: 在目标硬件上测试以获得最佳设置

Trait Implementations§

Source§

impl<'__de, __Context> BorrowDecode<'__de, __Context> for KdfPasswordAlgorithm

Source§

fn borrow_decode<__D: BorrowDecoder<'__de, Context = __Context>>( decoder: &mut __D, ) -> Result<Self, DecodeError>

Attempt to decode this type with the given BorrowDecode.
Source§

impl Clone for KdfPasswordAlgorithm

Source§

fn clone(&self) -> KdfPasswordAlgorithm

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for KdfPasswordAlgorithm

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<__Context> Decode<__Context> for KdfPasswordAlgorithm

Source§

fn decode<__D: Decoder<Context = __Context>>( decoder: &mut __D, ) -> Result<Self, DecodeError>

Attempt to decode this type with the given Decode.
Source§

impl<'de> Deserialize<'de> for KdfPasswordAlgorithm

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Encode for KdfPasswordAlgorithm

Source§

fn encode<__E: Encoder>(&self, encoder: &mut __E) -> Result<(), EncodeError>

Encode a given type.
Source§

impl From<KdfPasswordAlgorithm> for KdfPasswordWrapper

Source§

fn from(algorithm: KdfPasswordAlgorithm) -> Self

Converts to this type from the input type.
Source§

impl Hash for KdfPasswordAlgorithm

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for KdfPasswordAlgorithm

Source§

fn eq(&self, other: &KdfPasswordAlgorithm) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for KdfPasswordAlgorithm

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Copy for KdfPasswordAlgorithm

Source§

impl Eq for KdfPasswordAlgorithm

Source§

impl StructuralPartialEq for KdfPasswordAlgorithm

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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> ConditionallySerde for T
where T: Serialize + for<'de> Deserialize<'de>,

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,