use px_errors::AppError;
use crate::cipher::b64::h_p;
use crate::cipher::offsets::v_n;
use crate::cipher::secret::v_l;
use crate::cipher::splice::v_q;
use crate::cipher::xor::{IS, jw};
pub fn encrypt_sensor(events_json: &[u8], pf: &[u8], cu: &[u8]) -> Result<Vec<u8>, AppError> {
let secret_feed = v_l(pf);
let encrypted = h_p(&jw(events_json, IS)).into_bytes();
let offsets = v_n(secret_feed.len(), encrypted.len(), cu);
v_q(&secret_feed, &encrypted, &offsets)
}
#[cfg(test)]
#[allow(clippy::expect_used)]
mod tests {
use super::*;
#[test]
fn deterministic_for_same_inputs() {
let a = encrypt_sensor(b"[]", b"pedidosya.com.ar", b"cu-1").expect("encrypt");
let b = encrypt_sensor(b"[]", b"pedidosya.com.ar", b"cu-1").expect("encrypt");
assert_eq!(a, b);
}
#[test]
fn output_differs_for_distinct_pf() {
let a = encrypt_sensor(b"[]", b"pf-A", b"cu-shared").expect("encrypt");
let b = encrypt_sensor(b"[]", b"pf-B", b"cu-shared").expect("encrypt");
assert_ne!(a, b);
}
}