ferogram_crypto/
auth_key.rs1use crate::sha1;
17
18#[derive(Clone)]
20pub struct AuthKey {
21 pub(crate) data: [u8; 256],
22 pub(crate) aux_hash: [u8; 8],
23 pub(crate) key_id: [u8; 8],
24}
25
26impl AuthKey {
27 pub fn from_bytes(data: [u8; 256]) -> Self {
29 let sha = sha1!(&data);
30 let mut aux_hash = [0u8; 8];
31 aux_hash.copy_from_slice(&sha[..8]);
32 let mut key_id = [0u8; 8];
33 key_id.copy_from_slice(&sha[12..20]);
34 Self {
35 data,
36 aux_hash,
37 key_id,
38 }
39 }
40
41 pub fn to_bytes(&self) -> [u8; 256] {
43 self.data
44 }
45
46 pub fn key_id(&self) -> [u8; 8] {
48 self.key_id
49 }
50
51 pub fn calc_new_nonce_hash(&self, new_nonce: &[u8; 32], number: u8) -> [u8; 16] {
53 let data: Vec<u8> = new_nonce
54 .iter()
55 .copied()
56 .chain([number])
57 .chain(self.aux_hash.iter().copied())
58 .collect();
59 let sha = sha1!(&data);
60 let mut out = [0u8; 16];
61 out.copy_from_slice(&sha[4..]);
62 out
63 }
64}
65
66impl std::fmt::Debug for AuthKey {
67 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68 write!(f, "AuthKey(id={})", u64::from_le_bytes(self.key_id))
69 }
70}
71
72impl PartialEq for AuthKey {
73 fn eq(&self, other: &Self) -> bool {
74 self.key_id == other.key_id
75 }
76}