libreauth/hash.rs
1//! Hash functions used in the library
2
3use std::fmt;
4use std::str::FromStr;
5
6pub enum HashFunctionError {
7 ImportError,
8}
9
10/// ## C interface
11/// The C interface uses an enum of type `libreauth_hash_function` and
12/// the members has been renamed as follows:
13/// <table>
14/// <thead>
15/// <tr>
16/// <th>Rust</th>
17/// <th>C</th>
18/// </tr>
19/// </thead>
20/// <tbody>
21/// <tr>
22/// <td>Sha1</td>
23/// <td>LIBREAUTH_HASH_SHA_1</td>
24/// </tr>
25/// <tr>
26/// <td>Sha224</td>
27/// <td>LIBREAUTH_HASH_SHA_224</td>
28/// </tr>
29/// <tr>
30/// <td>Sha256</td>
31/// <td>LIBREAUTH_HASH_SHA_256</td>
32/// </tr>
33/// <tr>
34/// <td>Sha384</td>
35/// <td>LIBREAUTH_HASH_SHA_384</td>
36/// </tr>
37/// <tr>
38/// <td>Sha512</td>
39/// <td>LIBREAUTH_HASH_SHA_512</td>
40/// </tr>
41/// <tr>
42/// <td>Sha512Trunc224</td>
43/// <td>LIBREAUTH_HASH_SHA_512_TRUNC_224</td>
44/// </tr>
45/// <tr>
46/// <td>Sha512Trunc256</td>
47/// <td>LIBREAUTH_HASH_SHA_512_TRUNC_256</td>
48/// </tr>
49/// <tr>
50/// <td>Sha3_224</td>
51/// <td>LIBREAUTH_HASH_SHA_3_224</td>
52/// </tr>
53/// <tr>
54/// <td>Sha3_256</td>
55/// <td>LIBREAUTH_HASH_SHA_3_256</td>
56/// </tr>
57/// <tr>
58/// <td>Sha3_384</td>
59/// <td>LIBREAUTH_HASH_SHA_3_384</td>
60/// </tr>
61/// <tr>
62/// <td>Sha3_512</td>
63/// <td>LIBREAUTH_HASH_SHA_3_512</td>
64/// </tr>
65/// <tr>
66/// <td>Keccak224</td>
67/// <td>LIBREAUTH_HASH_KECCAK_224</td>
68/// </tr>
69/// <tr>
70/// <td>Keccak256</td>
71/// <td>LIBREAUTH_HASH_KECCAK_256</td>
72/// </tr>
73/// <tr>
74/// <td>Keccak384</td>
75/// <td>LIBREAUTH_HASH_KECCAK_384</td>
76/// </tr>
77/// <tr>
78/// <td>Keccak512</td>
79/// <td>LIBREAUTH_HASH_KECCAK_512</td>
80/// </tr>
81/// </tbody>
82/// </table>
83#[repr(C)]
84#[derive(Clone, Copy, Eq, PartialEq)]
85pub enum HashFunction {
86 Sha1 = 1,
87 Sha224 = 2,
88 Sha256 = 3,
89 Sha384 = 4,
90 Sha512 = 5,
91 Sha512Trunc224 = 6,
92 Sha512Trunc256 = 7,
93 Sha3_224 = 8,
94 Sha3_256 = 9,
95 Sha3_384 = 10,
96 Sha3_512 = 11,
97 Keccak224 = 12,
98 Keccak256 = 13,
99 Keccak384 = 14,
100 Keccak512 = 15,
101}
102
103impl fmt::Display for HashFunction {
104 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
105 let s = match self {
106 HashFunction::Sha1 => "SHA1",
107 HashFunction::Sha224 => "SHA224",
108 HashFunction::Sha256 => "SHA256",
109 HashFunction::Sha384 => "SHA384",
110 HashFunction::Sha512 => "SHA512",
111 HashFunction::Sha512Trunc224 => "SHA512-224",
112 HashFunction::Sha512Trunc256 => "SHA512-256",
113 HashFunction::Sha3_224 => "SHA3-224",
114 HashFunction::Sha3_256 => "SHA3-256",
115 HashFunction::Sha3_384 => "SHA3-384",
116 HashFunction::Sha3_512 => "SHA3-512",
117 HashFunction::Keccak224 => "Keccak224",
118 HashFunction::Keccak256 => "Keccak256",
119 HashFunction::Keccak384 => "Keccak384",
120 HashFunction::Keccak512 => "Keccak512",
121 };
122 write!(f, "{}", s)
123 }
124}
125
126impl FromStr for HashFunction {
127 type Err = HashFunctionError;
128
129 fn from_str(data: &str) -> Result<Self, Self::Err> {
130 Ok(match data.to_lowercase().as_str() {
131 "sha1" => HashFunction::Sha1,
132 "sha224" => HashFunction::Sha224,
133 "sha256" => HashFunction::Sha256,
134 "sha384" => HashFunction::Sha384,
135 "sha512" => HashFunction::Sha512,
136 "sha512-224" | "sha512t224" => HashFunction::Sha512Trunc224,
137 "sha512-256" | "sha512t256" => HashFunction::Sha512Trunc256,
138 "sha3-224" => HashFunction::Sha3_224,
139 "sha3-256" => HashFunction::Sha3_256,
140 "sha3-384" => HashFunction::Sha3_384,
141 "sha3-512" => HashFunction::Sha3_512,
142 "keccak224" => HashFunction::Keccak224,
143 "keccak256" => HashFunction::Keccak256,
144 "keccak384" => HashFunction::Keccak384,
145 "keccak512" => HashFunction::Keccak512,
146 _ => {
147 return Err(HashFunctionError::ImportError);
148 }
149 })
150 }
151}