1use file_system::{ControlCommand, IoErrorKind, Result, define_command};
2
3#[repr(u8)]
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum HashAlgorithm {
6 Md5 = 0,
7 Sha1 = 1,
8 Sha224 = 2,
9 Sha256 = 3,
10 Sha384 = 4,
11 Sha512 = 5,
12 Sha512_224 = 6,
13 Sha512_256 = 7,
14}
15
16impl TryFrom<u8> for HashAlgorithm {
17 type Error = IoErrorKind;
18
19 fn try_from(value: u8) -> Result<Self> {
20 match value {
21 0 => Ok(Self::Md5),
22 1 => Ok(Self::Sha1),
23 2 => Ok(Self::Sha224),
24 3 => Ok(Self::Sha256),
25 4 => Ok(Self::Sha384),
26 5 => Ok(Self::Sha512),
27 6 => Ok(Self::Sha512_224),
28 7 => Ok(Self::Sha512_256),
29 _ => Err(IoErrorKind::InvalidArgument),
30 }
31 }
32}
33
34define_command!(RESET, Write, crate::kind::HASH, 0, (), ());
35define_command!(SET_ALGORITHM, Write, crate::kind::HASH, 1, u8, ());
40
41#[cfg(test)]
42mod tests {
43 use super::*;
44
45 #[test]
46 fn hash_algorithm_try_from_rejects_unknown_bytes() {
47 assert_eq!(
48 HashAlgorithm::try_from(HashAlgorithm::Sha256 as u8).unwrap(),
49 HashAlgorithm::Sha256
50 );
51 assert_eq!(
52 HashAlgorithm::try_from(200),
53 Err(IoErrorKind::InvalidArgument)
54 );
55 }
56}