Coverage Report

Created: 2026-08-14 08:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/argon2-0.5.3/src/algorithm.rs
Line
Count
Source
1
//! Argon2 algorithms (e.g. Argon2d, Argon2i, Argon2id).
2
3
use crate::{Error, Result};
4
use core::{
5
    fmt::{self, Display},
6
    str::FromStr,
7
};
8
9
#[cfg(feature = "password-hash")]
10
use password_hash::Ident;
11
12
/// Argon2d algorithm identifier
13
#[cfg(feature = "password-hash")]
14
#[cfg_attr(docsrs, doc(cfg(feature = "password-hash")))]
15
pub const ARGON2D_IDENT: Ident<'_> = Ident::new_unwrap("argon2d");
16
17
/// Argon2i algorithm identifier
18
#[cfg(feature = "password-hash")]
19
#[cfg_attr(docsrs, doc(cfg(feature = "password-hash")))]
20
pub const ARGON2I_IDENT: Ident<'_> = Ident::new_unwrap("argon2i");
21
22
/// Argon2id algorithm identifier
23
#[cfg(feature = "password-hash")]
24
#[cfg_attr(docsrs, doc(cfg(feature = "password-hash")))]
25
pub const ARGON2ID_IDENT: Ident<'_> = Ident::new_unwrap("argon2id");
26
27
/// Argon2 primitive type: variants of the algorithm.
28
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Default, Ord)]
29
pub enum Algorithm {
30
    /// Optimizes against GPU cracking attacks but vulnerable to side-channels.
31
    ///
32
    /// Accesses the memory array in a password dependent order, reducing the
33
    /// possibility of time–memory tradeoff (TMTO) attacks.
34
    Argon2d = 0,
35
36
    /// Optimized to resist side-channel attacks.
37
    ///
38
    /// Accesses the memory array in a password independent order, increasing the
39
    /// possibility of time-memory tradeoff (TMTO) attacks.
40
    Argon2i = 1,
41
42
    /// Hybrid that mixes Argon2i and Argon2d passes (*default*).
43
    ///
44
    /// Uses the Argon2i approach for the first half pass over memory and
45
    /// Argon2d approach for subsequent passes. This effectively places it in
46
    /// the "middle" between the other two: it doesn't provide as good
47
    /// TMTO/GPU cracking resistance as Argon2d, nor as good of side-channel
48
    /// resistance as Argon2i, but overall provides the most well-rounded
49
    /// approach to both classes of attacks.
50
    #[default]
51
    Argon2id = 2,
52
}
53
54
impl Algorithm {
55
    /// Parse an [`Algorithm`] from the provided string.
56
0
    pub fn new(id: impl AsRef<str>) -> Result<Self> {
57
0
        id.as_ref().parse()
58
0
    }
59
60
    /// Get the identifier string for this PBKDF2 [`Algorithm`].
61
0
    pub const fn as_str(&self) -> &'static str {
62
0
        match self {
63
0
            Algorithm::Argon2d => "argon2d",
64
0
            Algorithm::Argon2i => "argon2i",
65
0
            Algorithm::Argon2id => "argon2id",
66
        }
67
0
    }
68
69
    /// Get the [`Ident`] that corresponds to this Argon2 [`Algorithm`].
70
    #[cfg(feature = "password-hash")]
71
    #[cfg_attr(docsrs, doc(cfg(feature = "password-hash")))]
72
1.03k
    pub const fn ident(&self) -> Ident<'static> {
73
1.03k
        match self {
74
0
            Algorithm::Argon2d => ARGON2D_IDENT,
75
0
            Algorithm::Argon2i => ARGON2I_IDENT,
76
1.03k
            Algorithm::Argon2id => ARGON2ID_IDENT,
77
        }
78
1.03k
    }
79
80
    /// Serialize primitive type as little endian bytes
81
1.03k
    pub(crate) const fn to_le_bytes(self) -> [u8; 4] {
82
1.03k
        (self as u32).to_le_bytes()
83
1.03k
    }
84
}
85
86
impl AsRef<str> for Algorithm {
87
0
    fn as_ref(&self) -> &str {
88
0
        self.as_str()
89
0
    }
90
}
91
92
impl Display for Algorithm {
93
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94
0
        f.write_str(self.as_str())
95
0
    }
96
}
97
98
impl FromStr for Algorithm {
99
    type Err = Error;
100
101
0
    fn from_str(s: &str) -> Result<Algorithm> {
102
0
        match s {
103
0
            "argon2d" => Ok(Algorithm::Argon2d),
104
0
            "argon2i" => Ok(Algorithm::Argon2i),
105
0
            "argon2id" => Ok(Algorithm::Argon2id),
106
0
            _ => Err(Error::AlgorithmInvalid),
107
        }
108
0
    }
109
}
110
111
#[cfg(feature = "password-hash")]
112
#[cfg_attr(docsrs, doc(cfg(feature = "password-hash")))]
113
impl From<Algorithm> for Ident<'static> {
114
0
    fn from(alg: Algorithm) -> Ident<'static> {
115
0
        alg.ident()
116
0
    }
117
}
118
119
#[cfg(feature = "password-hash")]
120
#[cfg_attr(docsrs, doc(cfg(feature = "password-hash")))]
121
impl<'a> TryFrom<Ident<'a>> for Algorithm {
122
    type Error = password_hash::Error;
123
124
0
    fn try_from(ident: Ident<'a>) -> password_hash::Result<Algorithm> {
125
0
        match ident {
126
0
            ARGON2D_IDENT => Ok(Algorithm::Argon2d),
127
0
            ARGON2I_IDENT => Ok(Algorithm::Argon2i),
128
0
            ARGON2ID_IDENT => Ok(Algorithm::Argon2id),
129
0
            _ => Err(password_hash::Error::Algorithm),
130
        }
131
0
    }
132
}