Skip to main content

open

Function open 

Source
pub fn open<T: Decode<()>>(
    blob: &[u8],
    key: Option<&str>,
) -> Result<T, SerializationError>
Expand description

Decode a byte blob produced by seal back into T.

If key is None the default key is used.

§Errors

Returns SerializationError::Decode if the blob is malformed or the key is incorrect.

Examples found in repository?
examples/veil_seal_open.rs (line 43)
25fn main() {
26    let key = "my-secret-key";
27
28    let original = Payload {
29        user:  "alice".into(),
30        score: 9001,
31        tags:  vec!["rust".into(), "crypto".into()],
32    };
33
34    println!("Original : {original:?}");
35
36    // ── Seal ──────────────────────────────────────────────────────────────────
37    // Transforms Payload into an opaque byte blob.
38    let blob = seal(&original, Some(key)).expect("seal failed");
39    println!("Sealed   : {} bytes  (opaque — no structure visible)", blob.len());
40
41    // ── Open ──────────────────────────────────────────────────────────────────
42    // Reconstructs Payload from the opaque blob using the same key.
43    let recovered: Payload = open(&blob, Some(key)).expect("open failed");
44    println!("Recovered: {recovered:?}");
45
46    assert_eq!(original, recovered, "round-trip mismatch!");
47    println!("\nRound-trip successful ✓");
48
49    // ── Wrong key rejects ─────────────────────────────────────────────────────
50    let bad: Result<Payload, _> = open(&blob, Some("wrong-key"));
51    assert!(bad.is_err(), "wrong key should fail to open");
52    println!("Wrong-key rejection ✓");
53}