Skip to main content

test_encode/
test_encode.rs

1// Copyright (c) 2026 Christoph Gaffga
2// SPDX-License-Identifier: GPL-3.0-only
3// https://github.com/cgaffga/phasmcore
4
5//! Example: encode and decode a hidden message in a JPEG image.
6use std::fs;
7
8fn main() {
9    let args: Vec<String> = std::env::args().collect();
10    if args.len() < 4 {
11        eprintln!("Usage: test_encode <input.jpg> <message> <passphrase>");
12        eprintln!("       test_encode --decode <stego.jpg> <passphrase>");
13        std::process::exit(1);
14    }
15
16    if args[1] == "--decode" {
17        let stego = fs::read(&args[2]).expect("Could not read stego image");
18        match phasm_core::ghost_decode(&stego, &args[3]) {
19            Ok(payload) => {
20                println!("Decoded message: {}", payload.text);
21                for f in &payload.files {
22                    println!("  File: {} ({} bytes)", f.filename, f.content.len());
23                }
24            }
25            Err(e) => eprintln!("Decode failed: {:?}", e),
26        }
27    } else {
28        let cover = fs::read(&args[1]).expect("Could not read cover image");
29        let message = &args[2];
30        let passphrase = &args[3];
31
32        let stego = phasm_core::ghost_encode(&cover, message, passphrase)
33            .expect("Encode failed");
34
35        let out_path = args[1].replace(".jpg", "_stego.jpg").replace(".JPG", "_stego.jpg");
36        fs::write(&out_path, &stego).expect("Could not write output");
37        println!("Stego image written to: {}", out_path);
38        println!("Original: {} bytes, Stego: {} bytes", cover.len(), stego.len());
39    }
40}