quickdash/
algorithms.rs

1/* Copyright [2021] [Cerda]
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *    http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16use std::str::FromStr;
17
18use clap::ArgEnum;
19
20/// A hashing algorithm.
21///
22/// # Examples
23///
24/// ```
25/// # use std::str::FromStr;
26/// assert_eq!(
27/// 	quickdash::Algorithm::from_str("BLAKE3"),
28/// 	Ok(quickdash::Algorithm::BLAKE3)
29/// );
30/// assert_eq!(
31/// 	quickdash::Algorithm::from_str("MD5"),
32/// 	Ok(quickdash::Algorithm::MD5)
33/// );
34/// ```
35
36#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, ArgEnum)]
37pub enum Algorithm {
38	SHA1,
39	SHA2224,
40	SHA2256,
41	SHA2384,
42	SHA2512,
43	SHA3224,
44	SHA3256,
45	SHA3384,
46	SHA3512,
47	XXH32,
48	XXH64,
49	XXH3,
50	CRC32,
51	MD5,
52	WhirlPool,
53	BLAKE2B,
54	BLAKE2S,
55	BLAKE3,
56}
57
58impl Algorithm {
59	/// Length, in bytes, of the algorithm's output hex string
60	pub fn hexlen(&self) -> usize {
61		match *self {
62			Algorithm::CRC32 | Algorithm::XXH32 => 8,
63			Algorithm::XXH3 | Algorithm::XXH64 => 16,
64			Algorithm::MD5 => 32,
65			Algorithm::SHA3256 | Algorithm::SHA2256 | Algorithm::BLAKE2S | Algorithm::BLAKE3 => 64,
66			Algorithm::SHA1 => 40,
67			Algorithm::SHA2224 | Algorithm::SHA3224 => 56,
68			Algorithm::SHA2384 | Algorithm::SHA3384 => 96,
69			Algorithm::BLAKE2B | Algorithm::SHA3512 | Algorithm::SHA2512 | Algorithm::WhirlPool => {
70				128
71			}
72		}
73	}
74}
75
76impl FromStr for Algorithm {
77	type Err = String;
78
79	fn from_str(s: &str) -> Result<Self, Self::Err> {
80		match &s.replace('_', "-").to_lowercase()[..] {
81			"sha-1" | "sha1" => Ok(Algorithm::SHA1),
82			"sha2224" | "sha-224" | "sha-2-224" => Ok(Algorithm::SHA2224),
83			"sha2256" | "sha-256" | "sha-2-256" => Ok(Algorithm::SHA2256),
84			"sha2384" | "sha-384" | "sha-2-384" => Ok(Algorithm::SHA2384),
85			"sha2512" | "sha-512" | "sha-2-512" => Ok(Algorithm::SHA2512),
86			"sha3224" | "sha3-224" | "sha-3-224" => Ok(Algorithm::SHA3224),
87			"sha3256" | "sha3-256" | "sha-3-256" => Ok(Algorithm::SHA3256),
88			"sha3384" | "sha3-384" | "sha-3-384" => Ok(Algorithm::SHA3384),
89			"sha3512" | "sha3-512" | "sha-3-512" => Ok(Algorithm::SHA3512),
90			"crc32" => Ok(Algorithm::CRC32),
91			"xxhash64" | "xxh64" => Ok(Algorithm::XXH64),
92			"xxhash32" | "xxh32" => Ok(Algorithm::XXH32),
93			"xxhash3" | "xxh3" => Ok(Algorithm::XXH3),
94			"md5" => Ok(Algorithm::MD5),
95			"blake2b" => Ok(Algorithm::BLAKE2B),
96			"blake2s" => Ok(Algorithm::BLAKE2S),
97			"blake3" => Ok(Algorithm::BLAKE3),
98			"whirlpool" => Ok(Algorithm::WhirlPool),
99			_ => Err(format!("\"{}\" is not a recognised hashing algorithm", s)),
100		}
101	}
102}