Skip to main content

genvectors/
genvectors.rs

1//! Emits Rust-encrypted Laravel payloads as JSON on stdout. CI pipes this into
2//! `scripts/verify_rust_vectors.php`, which decrypts each with the real Laravel encrypter and
3//! asserts equality: the Rust -> Laravel direction (the PHP-generated fixtures cover
4//! Laravel -> Rust).
5//!
6//! "string" vectors use `encrypt_bytes` / `decryptString`; "serialized" vectors use
7//! `encrypt` / `decrypt` and build the values named in `scripts/cases.php`.
8
9use laravel_rust_encryption::{Cipher, Encrypter, Value};
10use serde_json::json;
11
12const KEY: [u8; 32] = [0x2b; 32];
13
14fn strings() -> Vec<(&'static str, Vec<u8>)> {
15    vec![
16        ("simple", b"Hello from Rust".to_vec()),
17        ("empty", Vec::new()),
18        ("unicode", "Grüße 世界 🚀 café".as_bytes().to_vec()),
19        ("json", br#"{"user":"alice","id":7,"active":true}"#.to_vec()),
20        ("multiline", b"first line\nsecond line\n".to_vec()),
21        ("binary", vec![0x00, 0x01, 0xff, 0xfe, 0x80, 0x7f]),
22        ("block_aligned", vec![b'a'; 32]),
23        (
24            "long",
25            "The quick brown fox 0123456789 ".repeat(50).into_bytes(),
26        ),
27    ]
28}
29
30fn cases() -> Vec<(&'static str, Value)> {
31    vec![
32        ("null", Value::Null),
33        ("true", true.into()),
34        ("false", false.into()),
35        ("int_zero", 0.into()),
36        ("int_negative", (-42).into()),
37        ("int_max", i64::MAX.into()),
38        ("int_min", i64::MIN.into()),
39        ("float", 2.5.into()),
40        ("float_whole", 2.0.into()),
41        ("float_tenth", 0.1.into()),
42        ("float_large", 1.0e25.into()),
43        ("float_small", 1.5e-7.into()),
44        ("float_negative_zero", (-0.0).into()),
45        ("float_pi", std::f64::consts::PI.into()),
46        ("float_max", f64::MAX.into()),
47        ("float_min_subnormal", 4.9e-324.into()),
48        ("float_inf", f64::INFINITY.into()),
49        ("float_negative_inf", f64::NEG_INFINITY.into()),
50        ("float_nan", f64::NAN.into()),
51        ("string_empty", "".into()),
52        ("string_unicode", "Grüße 世界 🚀 café".into()),
53        ("string_quotes", r#"say "hi"; s:3:"x";"#.into()),
54        ("string_binary", Value::Bytes(vec![0x00, 0xff, 0xfe, 0x80])),
55        (
56            "list",
57            Value::list([
58                Value::Int(1),
59                "two".into(),
60                true.into(),
61                Value::Null,
62                Value::list([3, 4]),
63            ]),
64        ),
65        ("empty_array", Value::List(vec![])),
66        (
67            "assoc",
68            Value::map([
69                ("user", Value::from("alice")),
70                ("id", 7.into()),
71                ("active", true.into()),
72                ("score", 2.5.into()),
73                ("tags", Value::list(["x", "y"])),
74                ("meta", Value::Null),
75            ]),
76        ),
77        (
78            "mixed_keys",
79            Value::Map(vec![
80                (5.into(), "a".into()),
81                ("x".into(), "b".into()),
82                (7.into(), Value::map([("nested", Value::list([1, 2]))])),
83            ]),
84        ),
85        (
86            "non_sequential_list",
87            Value::Map(vec![(1.into(), "a".into()), (0.into(), "b".into())]),
88        ),
89        (
90            "nested",
91            Value::map([(
92                "l1",
93                Value::map([(
94                    "l2",
95                    Value::map([(
96                        "l3",
97                        Value::List(vec!["deep".into(), 1.5.into(), false.into()]),
98                    )]),
99                )]),
100            )]),
101        ),
102    ]
103}
104
105fn main() {
106    let mut vectors = Vec::new();
107    for cipher in [
108        Cipher::Aes128Cbc,
109        Cipher::Aes256Cbc,
110        Cipher::Aes128Gcm,
111        Cipher::Aes256Gcm,
112    ] {
113        let enc = Encrypter::new(&KEY[..cipher.key_size()], cipher).expect("valid key");
114
115        for (name, plaintext) in strings() {
116            vectors.push(json!({
117                "kind": "string",
118                "name": format!("{cipher}/{name}"),
119                "cipher": cipher.name(),
120                "plaintext_b64": base64(&plaintext),
121                "payload": enc.encrypt_bytes(&plaintext).expect("encrypt"),
122            }));
123        }
124        for (name, value) in cases() {
125            vectors.push(json!({
126                "kind": "serialized",
127                "name": format!("{cipher}/{name}"),
128                "case": name,
129                "cipher": cipher.name(),
130                "payload": enc.encrypt(&value).expect("encrypt"),
131            }));
132        }
133    }
134
135    println!("{}", json!({ "key_b64": base64(&KEY), "vectors": vectors }));
136}
137
138fn base64(bytes: &[u8]) -> String {
139    use base64::Engine as _;
140    base64::engine::general_purpose::STANDARD.encode(bytes)
141}