Skip to main content

osdp/command/
scrypt.rs

1//! `osdp_SCRYPT` (`0x77`) — server cryptogram.
2//!
3//! # Spec: §6.18, Annex D.4
4//!
5//! Body is the 16-byte server cryptogram. Packet carries SCB of type `SCS_13`.
6
7use crate::error::Error;
8use alloc::vec::Vec;
9
10/// `osdp_SCRYPT` body.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub struct SCrypt {
13    /// Server cryptogram.
14    pub server_cryptogram: [u8; 16],
15}
16
17impl SCrypt {
18    /// New.
19    pub const fn new(c: [u8; 16]) -> Self {
20        Self {
21            server_cryptogram: c,
22        }
23    }
24
25    /// Encode.
26    pub fn encode(&self) -> Result<Vec<u8>, Error> {
27        Ok(self.server_cryptogram.to_vec())
28    }
29
30    /// Decode.
31    pub fn decode(data: &[u8]) -> Result<Self, Error> {
32        if data.len() != 16 {
33            return Err(Error::MalformedPayload {
34                code: 0x77,
35                reason: "SCRYPT requires 16-byte cryptogram",
36            });
37        }
38        let mut c = [0u8; 16];
39        c.copy_from_slice(data);
40        Ok(Self {
41            server_cryptogram: c,
42        })
43    }
44}