revault_lockbox_api/model/
lockbox_id.rs1use std::fmt;
2
3use crate::{Error, Result};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
7pub struct LockboxId([u8; 16]);
8
9impl LockboxId {
10 pub fn new_random() -> Result<Self> {
14 let mut bytes = [0u8; 16];
15 getrandom::fill(&mut bytes).map_err(|err| Error::Io(err.to_string()))?;
16 bytes[6] = (bytes[6] & 0x0f) | 0x40;
17 bytes[8] = (bytes[8] & 0x3f) | 0x80;
18 Ok(Self(bytes))
19 }
20
21 pub fn from_bytes(bytes: [u8; 16]) -> Self {
23 Self(bytes)
24 }
25
26 pub fn as_bytes(&self) -> &[u8; 16] {
28 &self.0
29 }
30}
31
32impl fmt::Display for LockboxId {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 let b = self.0;
35 write!(
36 f,
37 "{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
38 b[0],
39 b[1],
40 b[2],
41 b[3],
42 b[4],
43 b[5],
44 b[6],
45 b[7],
46 b[8],
47 b[9],
48 b[10],
49 b[11],
50 b[12],
51 b[13],
52 b[14],
53 b[15]
54 )
55 }
56}