bc_components/symmetric/
hash_type.rs

1use dcbor::prelude::*;
2use anyhow::{Error, Result};
3
4/// Enum representing the supported hash types.
5///
6/// CDDL:
7/// ```cddl
8/// HashType = SHA256 / SHA512
9/// SHA256 = 0
10/// SHA512 = 1
11/// ```
12#[derive(Copy, Debug, Clone, PartialEq, Eq, Hash)]
13pub enum HashType {
14    SHA256 = 0,
15    SHA512 = 1,
16}
17
18impl std::fmt::Display for HashType {
19    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        match self {
21            HashType::SHA256 => write!(f, "SHA256"),
22            HashType::SHA512 => write!(f, "SHA512"),
23        }
24    }
25}
26
27impl Into<CBOR> for HashType {
28    fn into(self) -> CBOR {
29        CBOR::from(self as u8)
30    }
31}
32
33impl TryFrom<CBOR> for HashType {
34    type Error = Error;
35
36    fn try_from(cbor: CBOR) -> Result<Self> {
37        let i: u8 = cbor.try_into()?;
38        match i {
39            0 => Ok(HashType::SHA256),
40            1 => Ok(HashType::SHA512),
41            _ => Err(Error::msg("Invalid HashType")),
42        }
43    }
44}