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
//! Hash functions used in the library

use std::fmt;
use std::str::FromStr;

pub enum HashFunctionError {
    ImportError,
}

/// ## C interface
/// The C interface uses an enum of type `libreauth_hash_function` and
/// the members has been renamed as follows:
/// <table>
///     <thead>
///         <tr>
///             <th>Rust</th>
///             <th>C</th>
///         </tr>
///     </thead>
///     <tbody>
///         <tr>
///             <td>Sha1</td>
///             <td>LIBREAUTH_HASH_SHA_1</td>
///         </tr>
///         <tr>
///             <td>Sha224</td>
///             <td>LIBREAUTH_HASH_SHA_224</td>
///         </tr>
///         <tr>
///             <td>Sha256</td>
///             <td>LIBREAUTH_HASH_SHA_256</td>
///         </tr>
///         <tr>
///             <td>Sha384</td>
///             <td>LIBREAUTH_HASH_SHA_384</td>
///         </tr>
///         <tr>
///             <td>Sha512</td>
///             <td>LIBREAUTH_HASH_SHA_512</td>
///         </tr>
///         <tr>
///             <td>Sha512Trunc224</td>
///             <td>LIBREAUTH_HASH_SHA_512_TRUNC_224</td>
///         </tr>
///         <tr>
///             <td>Sha512Trunc256</td>
///             <td>LIBREAUTH_HASH_SHA_512_TRUNC_256</td>
///         </tr>
///         <tr>
///             <td>Sha3_224</td>
///             <td>LIBREAUTH_HASH_SHA_3_224</td>
///         </tr>
///         <tr>
///             <td>Sha3_256</td>
///             <td>LIBREAUTH_HASH_SHA_3_256</td>
///         </tr>
///         <tr>
///             <td>Sha3_384</td>
///             <td>LIBREAUTH_HASH_SHA_3_384</td>
///         </tr>
///         <tr>
///             <td>Sha3_512</td>
///             <td>LIBREAUTH_HASH_SHA_3_512</td>
///         </tr>
///         <tr>
///             <td>Keccak224</td>
///             <td>LIBREAUTH_HASH_KECCAK_224</td>
///         </tr>
///         <tr>
///             <td>Keccak256</td>
///             <td>LIBREAUTH_HASH_KECCAK_256</td>
///         </tr>
///         <tr>
///             <td>Keccak384</td>
///             <td>LIBREAUTH_HASH_KECCAK_384</td>
///         </tr>
///         <tr>
///             <td>Keccak512</td>
///             <td>LIBREAUTH_HASH_KECCAK_512</td>
///         </tr>
///     </tbody>
/// </table>
#[repr(C)]
#[derive(Clone, Copy, Eq, PartialEq)]
pub enum HashFunction {
    Sha1 = 1,
    Sha224 = 2,
    Sha256 = 3,
    Sha384 = 4,
    Sha512 = 5,
    Sha512Trunc224 = 6,
    Sha512Trunc256 = 7,
    Sha3_224 = 8,
    Sha3_256 = 9,
    Sha3_384 = 10,
    Sha3_512 = 11,
    Keccak224 = 12,
    Keccak256 = 13,
    Keccak384 = 14,
    Keccak512 = 15,
}

impl fmt::Display for HashFunction {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = match self {
            HashFunction::Sha1 => "SHA1",
            HashFunction::Sha224 => "SHA224",
            HashFunction::Sha256 => "SHA256",
            HashFunction::Sha384 => "SHA384",
            HashFunction::Sha512 => "SHA512",
            HashFunction::Sha512Trunc224 => "SHA512-224",
            HashFunction::Sha512Trunc256 => "SHA512-256",
            HashFunction::Sha3_224 => "SHA3-224",
            HashFunction::Sha3_256 => "SHA3-256",
            HashFunction::Sha3_384 => "SHA3-384",
            HashFunction::Sha3_512 => "SHA3-512",
            HashFunction::Keccak224 => "Keccak224",
            HashFunction::Keccak256 => "Keccak256",
            HashFunction::Keccak384 => "Keccak384",
            HashFunction::Keccak512 => "Keccak512",
        };
        write!(f, "{}", s)
    }
}

impl FromStr for HashFunction {
    type Err = HashFunctionError;

    fn from_str(data: &str) -> Result<Self, Self::Err> {
        Ok(match data.to_lowercase().as_str() {
            "sha1" => HashFunction::Sha1,
            "sha224" => HashFunction::Sha224,
            "sha256" => HashFunction::Sha256,
            "sha384" => HashFunction::Sha384,
            "sha512" => HashFunction::Sha512,
            "sha512-224" | "sha512t224" => HashFunction::Sha512Trunc224,
            "sha512-256" | "sha512t256" => HashFunction::Sha512Trunc256,
            "sha3-224" => HashFunction::Sha3_224,
            "sha3-256" => HashFunction::Sha3_256,
            "sha3-384" => HashFunction::Sha3_384,
            "sha3-512" => HashFunction::Sha3_512,
            "keccak224" => HashFunction::Keccak224,
            "keccak256" => HashFunction::Keccak256,
            "keccak384" => HashFunction::Keccak384,
            "keccak512" => HashFunction::Keccak512,
            _ => {
                return Err(HashFunctionError::ImportError);
            }
        })
    }
}