Spaces:
Sleeping
Sleeping
Download src/core/embedder.py from low-key-007/crypto-api: direct link, hf CLI and curl.
- Browser
- Download file 1.45 kB
-
https://huggingface.co/spaces/low-key-007/crypto-api/resolve/main/src/core/embedder.py
- Command line
-
hf download hf://spaces/low-key-007/crypto-api/src/core/embedder.py
-
curl -L -o embedder.py https://huggingface.co/spaces/low-key-007/crypto-api/resolve/main/src/core/embedder.py
1.45 kB
| import numpy as np | |
| from PIL import Image | |
| from cryptography.fernet import Fernet | |
| from pathlib import Path | |
| class StegoEmbedder: | |
| def process(self, encrypted_img_path: Path, secret_img_path: Path, output_dir: Path): | |
| """ | |
| Embeds a secret image into the LSB of the encrypted cover. | |
| Returns: Tuple(path_to_stego_img, key3) | |
| """ | |
| enc_img = Image.open(encrypted_img_path).convert("L") | |
| sec_img = Image.open(secret_img_path).convert("L") | |
| enc_arr = np.array(enc_img) | |
| sec_arr = np.array(sec_img) | |
| # Flatten secret data | |
| secret_data = sec_arr.flatten().astype(np.uint8) | |
| secret_bin = ''.join(format(x, '08b') for x in secret_data) | |
| # Key 3: Secret Shuffle Key | |
| key3 = Fernet.generate_key() | |
| # LSB Embedding Logic | |
| h, w = enc_arr.shape | |
| flat = enc_arr.flatten() | |
| data_len = len(secret_bin) | |
| # Header: Length of data (16 bits) | |
| bin_len = format(data_len, '016b') | |
| payload = bin_len + secret_bin | |
| payload = payload[:len(flat)] # Safety clip | |
| new_flat = flat.copy() | |
| for i, bit in enumerate(payload): | |
| new_flat[i] = (new_flat[i] & ~1) | int(bit) | |
| new_arr = new_flat.reshape((h, w)) | |
| output_path = output_dir / "stego_image.png" | |
| Image.fromarray(new_arr).save(output_path) | |
| return output_path, key3.decode() |