bc_components/encrypted_key/
key_derivation_method.rs1use bc_ur::prelude::*;
2
3use crate::{Error, Result};
4
5#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
16pub enum KeyDerivationMethod {
17 HKDF = 0,
18 PBKDF2 = 1,
19 Scrypt = 2,
20 Argon2id = 3,
21 #[cfg(feature = "ssh-agent")]
22 SSHAgent = 4,
23}
24
25impl KeyDerivationMethod {
26 pub fn index(&self) -> usize { *self as usize }
28
29 pub fn from_index(index: usize) -> Option<Self> {
31 match index {
32 0 => Some(KeyDerivationMethod::HKDF),
33 1 => Some(KeyDerivationMethod::PBKDF2),
34 2 => Some(KeyDerivationMethod::Scrypt),
35 3 => Some(KeyDerivationMethod::Argon2id),
36 #[cfg(feature = "ssh-agent")]
37 4 => Some(KeyDerivationMethod::SSHAgent),
38 _ => None,
39 }
40 }
41}
42
43impl std::fmt::Display for KeyDerivationMethod {
44 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45 match self {
46 KeyDerivationMethod::HKDF => write!(f, "HKDF"),
47 KeyDerivationMethod::PBKDF2 => write!(f, "PBKDF2"),
48 KeyDerivationMethod::Scrypt => write!(f, "Scrypt"),
49 KeyDerivationMethod::Argon2id => write!(f, "Argon2id"),
50 #[cfg(feature = "ssh-agent")]
51 KeyDerivationMethod::SSHAgent => write!(f, "SSHAgent"),
52 }
53 }
54}
55
56impl TryFrom<&CBOR> for KeyDerivationMethod {
57 type Error = Error;
58
59 fn try_from(cbor: &CBOR) -> Result<Self> {
60 let i: usize = cbor.clone().try_into()?;
61 KeyDerivationMethod::from_index(i)
62 .ok_or_else(|| Error::general("Invalid KeyDerivationMethod"))
63 }
64}