1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use generic_array::sequence::Split;
use generic_array::GenericArray;
use serde::{Deserialize, Serialize};
use sha3::{Digest, Sha3_256};
use typenum::U16;
use umbral_pre::{PublicKey, SerializableToArray};

use crate::arrays_as_bytes;

/// "hashed resource access code".
///
/// A hash of:
/// * Publisher's verifying key
/// * Bob's verifying key
/// * the label
///
/// Publisher and Bob have all the information they need to construct this.
/// Ursula does not, so we share it with her.
#[allow(clippy::upper_case_acronyms)]
#[derive(PartialEq, Debug, Copy, Clone, Serialize, Deserialize)]
pub struct HRAC(#[serde(with = "arrays_as_bytes")] [u8; HRAC::SIZE]);

impl HRAC {
    /// The size of HRAC in bytes.
    pub const SIZE: usize = 16;

    /// Creates a new HRAC.
    pub fn new(
        publisher_verifying_key: &PublicKey,
        bob_verifying_key: &PublicKey,
        label: &[u8],
    ) -> Self {
        let digest = Sha3_256::new()
            .chain(&publisher_verifying_key.to_array())
            .chain(&bob_verifying_key.to_array())
            .chain(label)
            .finalize();

        // No problem with hardcoding here, since the size will be checked in compile-time
        let (hrac, _rest): (GenericArray<u8, U16>, GenericArray<u8, _>) = digest.split();
        Self(hrac.into())
    }
}

impl From<[u8; HRAC::SIZE]> for HRAC {
    fn from(bytes: [u8; HRAC::SIZE]) -> Self {
        Self(bytes)
    }
}

impl AsRef<[u8]> for HRAC {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}