1use crate::error::Error;
8use crate::payload_util::require_exact_len;
9use alloc::vec::Vec;
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub struct SCrypt {
14 pub server_cryptogram: [u8; 16],
16}
17
18impl SCrypt {
19 pub const fn new(c: [u8; 16]) -> Self {
21 Self {
22 server_cryptogram: c,
23 }
24 }
25
26 pub fn encode(&self) -> Result<Vec<u8>, Error> {
28 Ok(self.server_cryptogram.to_vec())
29 }
30
31 pub fn decode(data: &[u8]) -> Result<Self, Error> {
33 require_exact_len(data, 16, 0x77)?;
34 let mut c = [0u8; 16];
35 c.copy_from_slice(data);
36 Ok(Self {
37 server_cryptogram: c,
38 })
39 }
40}
41
42#[cfg(test)]
43mod tests {
44 use super::*;
45
46 #[test]
47 fn roundtrip() {
48 let body = SCrypt::new([0x42u8; 16]);
49 let bytes = body.encode().unwrap();
50 assert_eq!(bytes.len(), 16);
51 assert_eq!(SCrypt::decode(&bytes).unwrap(), body);
52 }
53
54 #[test]
55 fn decode_rejects_wrong_length() {
56 assert!(matches!(
57 SCrypt::decode(&[0; 15]),
58 Err(Error::PayloadLength { code: 0x77, .. })
59 ));
60 }
61}