1mod sha2;
5
6pub use self::sha2::Sha256Hash;
7
8use crate::algorithm::SHA256_ALG_ID;
9use crate::{
10 encoding::Encodable,
11 error::{Error, ErrorKind},
12};
13use anomaly::fail;
14use std::convert::TryInto;
15
16pub enum Hash {
18 Sha256(Sha256Hash),
20}
21
22impl Hash {
23 pub fn new(alg: &str, bytes: &[u8]) -> Result<Self, Error> {
25 let result = match alg {
26 SHA256_ALG_ID => Hash::Sha256(bytes.try_into()?),
27 _ => fail!(ErrorKind::AlgorithmInvalid, "{}", alg),
28 };
29
30 Ok(result)
31 }
32
33 pub fn sha256_digest(&self) -> Option<&Sha256Hash> {
35 match self {
36 Hash::Sha256(ref digest) => Some(digest),
37 }
38 }
39
40 pub fn is_sha256_digest(&self) -> bool {
42 self.sha256_digest().is_some()
43 }
44}
45
46impl Encodable for Hash {
47 fn to_uri_string(&self) -> String {
49 match self {
50 Hash::Sha256(ref digest) => digest.to_uri_string(),
51 }
52 }
53
54 fn to_dasherized_string(&self) -> String {
56 match self {
57 Hash::Sha256(ref digest) => digest.to_dasherized_string(),
58 }
59 }
60}