A library for hiding and extracting secret data in files (specifically PNG/BMP).
stego-lib provides a simple, extensible interface for steganography:
- Algorithms (currently
LSB) read/write bits in a container (e.g., image pixels). - Adapters (e.g.,
ImageAdapter) parse a specific container format and provide raw bytes to the algorithm.
- Supported algorithm:
LSB(Least Significant Bit). - Analysis: Mean Squared Error (MSE) in the
analysismodule. - Supported container formats via
ImageAdapter: PNG, BMP (using theimagecrate).
- Installing the Library from GitHub
You can add
stego-libdirectly from GitHub in yourCargo.toml:
[dependencies]
stego-lib = { git = "https://github.com/Chardje/stego-lib.git" }Then run cargo build to fetch and compile the library. After that, you can use the library in your Rust project as shown in the examples above.
- Initialize the adapter registry
Before use, initialize the global adapter registry once:
use std::sync::Mutex;
use stego_lib::adapters::ImageAdapter;
stego_lib::ADAPTERS.set(Mutex::new(vec![ImageAdapter::create]));- Hiding data (
hide)
use std::fs::File;
use std::io::Cursor;
use stego_lib::algorithms::LSBAlgorithm;
let container_file = File::open("tests/teststego.png")?;
let secret = Cursor::new(b"Hello, world!".to_vec());
let mut stego = stego_lib::hide::<_, _, LSBAlgorithm>(container_file, secret)?;
// `stego` — Cursor<Vec<u8>> with the modified container bytes- Extracting data (
extract)
use std::fs::File;
use stego_lib::algorithms::LSBAlgorithm;
let container_file = File::open("tests/out.png")?;
let mut secret_cursor = stego_lib::extract::<_, LSBAlgorithm>(container_file)?;
let mut secret = Vec::new();
secret_cursor.read_to_end(&mut secret)?;
// `secret` now contains the extracted hidden dataADAPTERS— globalOnceLock<Mutex<Vec<AdapterCtor>>>storing adapter constructors.ImageAdapterdecodes images into an RGBA buffer, provides access viareadeble_mut_data(), and re-encodes PNG throughexport().- The
LSBalgorithm insrc/algorithms/lsb.rsstores the secret length in the first 64 bytes (one bit per byte), followed by the secret bits (1 secret bit per container byte).
- CLI example: examples/cli.rs
- Image adapter: src/adapters/image_adapter.rs
- LSB implementation: src/algorithms/lsb.rs
- Build:
cargo build - Run tests:
cargo test - CLI example:
cargo run --example cli -- hide -c tests/teststego.png -s "Hello" -o tests/out.png -a lsb
To support other formats or algorithms, implement a new adapter (constructor function fn(&Vec<u8>) -> Result<Box<dyn IAdapter>, Box<dyn Error>>) and add it to ADAPTERS.