1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (c) 2017 Martijn Rijkeboer <[email protected]>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use crate::error::Error;
use crate::result::Result;
use std::fmt;

/// The Argon2 variant.
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub enum Variant {
    /// Argon2 using data-dependent memory access to thwart tradeoff attacks.
    /// Recommended for cryptocurrencies and backend servers.
    Argon2d = 0,

    /// Argon2 using data-independent memory access to thwart side-channel
    /// attacks. Recommended for password hashing and password-based key
    /// derivation.
    Argon2i = 1,

    /// Argon2 using hybrid construction.
    Argon2id = 2,
}

impl Variant {
    /// Gets the lowercase string slice representation of the variant.
    pub fn as_lowercase_str(&self) -> &'static str {
        match *self {
            Variant::Argon2d => "argon2d",
            Variant::Argon2i => "argon2i",
            Variant::Argon2id => "argon2id",
        }
    }

    /// Gets the u32 representation of the variant.
    pub fn as_u32(&self) -> u32 {
        *self as u32
    }

    /// Gets the u64 representation of the variant.
    pub fn as_u64(&self) -> u64 {
        *self as u64
    }

    /// Gets the uppercase string slice representation of the variant.
    pub fn as_uppercase_str(&self) -> &'static str {
        match *self {
            Variant::Argon2d => "Argon2d",
            Variant::Argon2i => "Argon2i",
            Variant::Argon2id => "Argon2id",
        }
    }

    /// Attempts to create a variant from a string slice.
    pub fn from_str(str: &str) -> Result<Variant> {
        match str {
            "Argon2d" => Ok(Variant::Argon2d),
            "Argon2i" => Ok(Variant::Argon2i),
            "Argon2id" => Ok(Variant::Argon2id),
            "argon2d" => Ok(Variant::Argon2d),
            "argon2i" => Ok(Variant::Argon2i),
            "argon2id" => Ok(Variant::Argon2id),
            _ => Err(Error::DecodingFail),
        }
    }

    /// Attempts to create a variant from an u32.
    pub fn from_u32(val: u32) -> Result<Variant> {
        match val {
            0 => Ok(Variant::Argon2d),
            1 => Ok(Variant::Argon2i),
            2 => Ok(Variant::Argon2id),
            _ => Err(Error::IncorrectType),
        }
    }
}

impl Default for Variant {
    fn default() -> Variant {
        Variant::Argon2i
    }
}

impl fmt::Display for Variant {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.as_lowercase_str())
    }
}

#[cfg(test)]
mod tests {

    use crate::error::Error;
    use crate::variant::Variant;

    #[test]
    fn as_lowercase_str_returns_correct_str() {
        assert_eq!(Variant::Argon2d.as_lowercase_str(), "argon2d");
        assert_eq!(Variant::Argon2i.as_lowercase_str(), "argon2i");
        assert_eq!(Variant::Argon2id.as_lowercase_str(), "argon2id");
    }

    #[test]
    fn as_u32_returns_correct_u32() {
        assert_eq!(Variant::Argon2d.as_u32(), 0);
        assert_eq!(Variant::Argon2i.as_u32(), 1);
        assert_eq!(Variant::Argon2id.as_u32(), 2);
    }

    #[test]
    fn as_u64_returns_correct_u64() {
        assert_eq!(Variant::Argon2d.as_u64(), 0);
        assert_eq!(Variant::Argon2i.as_u64(), 1);
        assert_eq!(Variant::Argon2id.as_u64(), 2);
    }

    #[test]
    fn as_uppercase_str_returns_correct_str() {
        assert_eq!(Variant::Argon2d.as_uppercase_str(), "Argon2d");
        assert_eq!(Variant::Argon2i.as_uppercase_str(), "Argon2i");
        assert_eq!(Variant::Argon2id.as_uppercase_str(), "Argon2id");
    }

    #[test]
    fn default_returns_correct_variant() {
        assert_eq!(Variant::default(), Variant::Argon2i);
    }

    #[test]
    fn display_returns_correct_string() {
        assert_eq!(format!("{}", Variant::Argon2d), "argon2d");
        assert_eq!(format!("{}", Variant::Argon2i), "argon2i");
        assert_eq!(format!("{}", Variant::Argon2id), "argon2id");
    }

    #[test]
    fn from_str_returns_correct_result() {
        assert_eq!(Variant::from_str("Argon2d"), Ok(Variant::Argon2d));
        assert_eq!(Variant::from_str("Argon2i"), Ok(Variant::Argon2i));
        assert_eq!(Variant::from_str("Argon2id"), Ok(Variant::Argon2id));
        assert_eq!(Variant::from_str("argon2d"), Ok(Variant::Argon2d));
        assert_eq!(Variant::from_str("argon2i"), Ok(Variant::Argon2i));
        assert_eq!(Variant::from_str("argon2id"), Ok(Variant::Argon2id));
        assert_eq!(Variant::from_str("foobar"), Err(Error::DecodingFail));
    }

    #[test]
    fn from_u32_returns_correct_result() {
        assert_eq!(Variant::from_u32(0), Ok(Variant::Argon2d));
        assert_eq!(Variant::from_u32(1), Ok(Variant::Argon2i));
        assert_eq!(Variant::from_u32(2), Ok(Variant::Argon2id));
        assert_eq!(Variant::from_u32(3), Err(Error::IncorrectType));
    }
}