osdp 0.3.0

Pure-Rust, no_std-friendly implementation of the SIA Open Supervised Device Protocol (OSDP) v2.2
Documentation
//! `osdp_SCRYPT` (`0x77`) — server cryptogram.
//!
//! # Spec: §6.18, Annex D.4
//!
//! Body is the 16-byte server cryptogram. Packet carries SCB of type `SCS_13`.

use crate::error::Error;
use crate::payload_util::require_exact_len;
use alloc::vec::Vec;

/// `osdp_SCRYPT` body.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SCrypt {
    /// Server cryptogram.
    pub server_cryptogram: [u8; 16],
}

impl SCrypt {
    /// New.
    pub const fn new(c: [u8; 16]) -> Self {
        Self {
            server_cryptogram: c,
        }
    }

    /// Encode.
    pub fn encode(&self) -> Result<Vec<u8>, Error> {
        Ok(self.server_cryptogram.to_vec())
    }

    /// Decode.
    pub fn decode(data: &[u8]) -> Result<Self, Error> {
        require_exact_len(data, 16, 0x77)?;
        let mut c = [0u8; 16];
        c.copy_from_slice(data);
        Ok(Self {
            server_cryptogram: c,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn roundtrip() {
        let body = SCrypt::new([0x42u8; 16]);
        let bytes = body.encode().unwrap();
        assert_eq!(bytes.len(), 16);
        assert_eq!(SCrypt::decode(&bytes).unwrap(), body);
    }

    #[test]
    fn decode_rejects_wrong_length() {
        assert!(matches!(
            SCrypt::decode(&[0; 15]),
            Err(Error::PayloadLength { code: 0x77, .. })
        ));
    }
}