Lint cleanup: ruff fixes across entire codebase
- Strip trailing whitespace from all Python files - Fix import sorting (I001) across all modules - Convert Optional[X] to X | None syntax (UP045) - Remove unused imports (F401) - Convert lambda assignments to def functions (E731) - Add TYPE_CHECKING import for forward references - Update pyproject.toml ruff config: - Move select/ignore to [tool.ruff.lint] section - Add per-file ignores for DCT colorspace naming (N803/N806) - Add per-file ignores for __init__.py import structure (E402) - Exclude defunct test_routes.py - Remove frontends/web/test_routes.py (defunct debug snippet) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -22,20 +22,16 @@ NEW in v3.0.1: DCT output format selection (PNG or JPEG) and color mode (graysca
|
||||
"""
|
||||
|
||||
import io
|
||||
import mimetypes
|
||||
import os
|
||||
import secrets
|
||||
import sys
|
||||
import time
|
||||
import secrets
|
||||
import mimetypes
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
|
||||
from flask import Flask, flash, jsonify, redirect, render_template, request, send_file, url_for
|
||||
from PIL import Image
|
||||
|
||||
from flask import (
|
||||
Flask, render_template, request, send_file,
|
||||
jsonify, flash, redirect, url_for
|
||||
)
|
||||
|
||||
import os
|
||||
os.environ['NUMPY_MADVISE_HUGEPAGE'] = '0'
|
||||
os.environ['OMP_NUM_THREADS'] = '1'
|
||||
|
||||
@@ -44,75 +40,76 @@ sys.path.insert(0, str(Path(__file__).parent.parent.parent / 'src'))
|
||||
|
||||
import stegasoo
|
||||
from stegasoo import (
|
||||
generate_credentials,
|
||||
export_rsa_key_pem, load_rsa_key,
|
||||
validate_pin, validate_message, validate_image,
|
||||
validate_rsa_key, validate_security_factors,
|
||||
validate_file_payload, validate_passphrase,
|
||||
generate_filename,
|
||||
StegasooError, DecryptionError, CapacityError,
|
||||
has_argon2,
|
||||
CapacityError,
|
||||
DecryptionError,
|
||||
FilePayload,
|
||||
# Embedding modes
|
||||
EMBED_MODE_LSB,
|
||||
EMBED_MODE_DCT,
|
||||
EMBED_MODE_AUTO,
|
||||
has_dct_support,
|
||||
# Channel key functions (v4.0.0)
|
||||
has_channel_key,
|
||||
StegasooError,
|
||||
export_rsa_key_pem,
|
||||
generate_credentials,
|
||||
generate_filename,
|
||||
get_channel_status,
|
||||
has_argon2,
|
||||
# Channel key functions (v4.0.0)
|
||||
has_dct_support,
|
||||
load_rsa_key,
|
||||
validate_channel_key,
|
||||
generate_channel_key,
|
||||
# NOTE: encode, decode, compare_modes, will_fit_by_mode now use subprocess isolation
|
||||
validate_file_payload,
|
||||
validate_image,
|
||||
validate_message,
|
||||
validate_passphrase,
|
||||
validate_pin,
|
||||
validate_rsa_key,
|
||||
validate_security_factors,
|
||||
)
|
||||
from stegasoo.constants import (
|
||||
__version__,
|
||||
MAX_MESSAGE_SIZE, MAX_MESSAGE_CHARS,
|
||||
MIN_PIN_LENGTH, MAX_PIN_LENGTH,
|
||||
MIN_PASSPHRASE_WORDS, RECOMMENDED_PASSPHRASE_WORDS,
|
||||
DEFAULT_PASSPHRASE_WORDS,
|
||||
VALID_RSA_SIZES, MAX_FILE_SIZE,
|
||||
MAX_FILE_PAYLOAD_SIZE, MAX_UPLOAD_SIZE,
|
||||
TEMP_FILE_EXPIRY, TEMP_FILE_EXPIRY_MINUTES,
|
||||
THUMBNAIL_SIZE, THUMBNAIL_QUALITY,
|
||||
MAX_FILE_PAYLOAD_SIZE,
|
||||
MAX_FILE_SIZE,
|
||||
MAX_MESSAGE_CHARS,
|
||||
MAX_PIN_LENGTH,
|
||||
MAX_UPLOAD_SIZE,
|
||||
MIN_PASSPHRASE_WORDS,
|
||||
MIN_PIN_LENGTH,
|
||||
RECOMMENDED_PASSPHRASE_WORDS,
|
||||
TEMP_FILE_EXPIRY,
|
||||
TEMP_FILE_EXPIRY_MINUTES,
|
||||
THUMBNAIL_QUALITY,
|
||||
THUMBNAIL_SIZE,
|
||||
VALID_RSA_SIZES,
|
||||
__version__,
|
||||
)
|
||||
|
||||
# QR Code support
|
||||
try:
|
||||
import qrcode
|
||||
from qrcode.constants import ERROR_CORRECT_L, ERROR_CORRECT_M
|
||||
import qrcode # noqa: F401
|
||||
from qrcode.constants import ERROR_CORRECT_L, ERROR_CORRECT_M # noqa: F401
|
||||
HAS_QRCODE = True
|
||||
except ImportError:
|
||||
HAS_QRCODE = False
|
||||
|
||||
# QR Code reading
|
||||
try:
|
||||
from pyzbar.pyzbar import decode as pyzbar_decode
|
||||
from pyzbar.pyzbar import decode as pyzbar_decode # noqa: F401
|
||||
HAS_QRCODE_READ = True
|
||||
except ImportError:
|
||||
HAS_QRCODE_READ = False
|
||||
|
||||
import zlib
|
||||
import base64
|
||||
|
||||
# Import QR utilities
|
||||
from stegasoo.qr_utils import (
|
||||
compress_data, decompress_data, auto_decompress,
|
||||
is_compressed, can_fit_in_qr, needs_compression,
|
||||
generate_qr_code, read_qr_code, extract_key_from_qr,
|
||||
detect_and_crop_qr,
|
||||
has_qr_write, has_qr_read,
|
||||
QR_MAX_BINARY, COMPRESSION_PREFIX
|
||||
)
|
||||
|
||||
# ============================================================================
|
||||
# SUBPROCESS ISOLATION FOR STEGASOO OPERATIONS
|
||||
# ============================================================================
|
||||
# Runs encode/decode/compare in subprocesses to prevent jpegio/scipy crashes
|
||||
# from taking down the Flask server.
|
||||
|
||||
from subprocess_stego import SubprocessStego
|
||||
|
||||
from stegasoo.qr_utils import (
|
||||
can_fit_in_qr,
|
||||
detect_and_crop_qr,
|
||||
extract_key_from_qr,
|
||||
generate_qr_code,
|
||||
)
|
||||
|
||||
# Initialize subprocess wrapper (worker script must be in same directory)
|
||||
subprocess_stego = SubprocessStego(timeout=180) # 3 minute timeout for large images
|
||||
|
||||
@@ -139,7 +136,7 @@ def inject_globals():
|
||||
"""Inject global variables into all templates."""
|
||||
# Get channel status (v4.0.0)
|
||||
channel_status = get_channel_status()
|
||||
|
||||
|
||||
return {
|
||||
'version': __version__,
|
||||
'max_message_chars': MAX_MESSAGE_CHARS,
|
||||
@@ -172,20 +169,20 @@ try:
|
||||
print(f"Current MAX_FILE_PAYLOAD_SIZE: {MAX_FILE_PAYLOAD_SIZE}")
|
||||
print(f"DCT support: {has_dct_support()}")
|
||||
print(f"QR code support: write={HAS_QRCODE}, read={HAS_QRCODE_READ}")
|
||||
|
||||
|
||||
# Channel key status (v4.0.0)
|
||||
channel_status = get_channel_status()
|
||||
print(f"Channel key: {channel_status['mode']} mode")
|
||||
if channel_status['configured']:
|
||||
print(f" Fingerprint: {channel_status.get('fingerprint')}")
|
||||
print(f" Source: {channel_status.get('source')}")
|
||||
|
||||
|
||||
DESIRED_PAYLOAD_SIZE = 2 * 1024 * 1024 # 2MB
|
||||
|
||||
|
||||
if hasattr(stegasoo, 'MAX_FILE_PAYLOAD_SIZE'):
|
||||
print(f"Overriding MAX_FILE_PAYLOAD_SIZE to {DESIRED_PAYLOAD_SIZE}")
|
||||
stegasoo.MAX_FILE_PAYLOAD_SIZE = DESIRED_PAYLOAD_SIZE
|
||||
|
||||
|
||||
except Exception as e:
|
||||
print(f"Could not override stegasoo limits: {e}")
|
||||
|
||||
@@ -197,10 +194,10 @@ except Exception as e:
|
||||
def resolve_channel_key_form(channel_key_value: str) -> str:
|
||||
"""
|
||||
Resolve channel key from form input.
|
||||
|
||||
|
||||
Args:
|
||||
channel_key_value: Form value ('auto', 'none', or explicit key)
|
||||
|
||||
|
||||
Returns:
|
||||
Value to pass to subprocess_stego ('auto', 'none', or explicit key)
|
||||
"""
|
||||
@@ -234,10 +231,10 @@ def generate_thumbnail(image_data: bytes, size: tuple = THUMBNAIL_SIZE) -> bytes
|
||||
img = img.convert('RGB')
|
||||
elif img.mode != 'RGB':
|
||||
img = img.convert('RGB')
|
||||
|
||||
|
||||
# Create thumbnail
|
||||
img.thumbnail(size, Image.Resampling.LANCZOS)
|
||||
|
||||
|
||||
# Save to bytes
|
||||
buffer = io.BytesIO()
|
||||
img.save(buffer, format='JPEG', quality=THUMBNAIL_QUALITY, optimize=True)
|
||||
@@ -251,7 +248,7 @@ def cleanup_temp_files():
|
||||
"""Remove expired temporary files."""
|
||||
now = time.time()
|
||||
expired = [fid for fid, info in TEMP_FILES.items() if now - info['timestamp'] > TEMP_FILE_EXPIRY]
|
||||
|
||||
|
||||
for fid in expired:
|
||||
TEMP_FILES.pop(fid, None)
|
||||
# Also clean up corresponding thumbnail
|
||||
@@ -294,12 +291,12 @@ def index():
|
||||
def api_channel_status():
|
||||
"""
|
||||
Get current channel key status (v4.0.0).
|
||||
|
||||
|
||||
Returns JSON with mode, fingerprint, and source.
|
||||
"""
|
||||
# Use subprocess for isolation
|
||||
result = subprocess_stego.get_channel_status(reveal=False)
|
||||
|
||||
|
||||
if result.success:
|
||||
return jsonify({
|
||||
'success': True,
|
||||
@@ -324,16 +321,16 @@ def api_channel_status():
|
||||
def api_channel_validate():
|
||||
"""
|
||||
Validate a channel key format (v4.0.0).
|
||||
|
||||
|
||||
Returns JSON with validation result.
|
||||
"""
|
||||
key = request.form.get('key', '') or request.json.get('key', '') if request.is_json else ''
|
||||
|
||||
|
||||
if not key:
|
||||
return jsonify({'valid': False, 'error': 'No key provided'})
|
||||
|
||||
|
||||
is_valid = validate_channel_key(key)
|
||||
|
||||
|
||||
if is_valid:
|
||||
fingerprint = f"{key[:4]}-••••-••••-••••-••••-••••-••••-{key[-4:]}"
|
||||
return jsonify({
|
||||
@@ -358,20 +355,20 @@ def generate():
|
||||
words_per_passphrase = int(request.form.get('words_per_passphrase', DEFAULT_PASSPHRASE_WORDS))
|
||||
use_pin = request.form.get('use_pin') == 'on'
|
||||
use_rsa = request.form.get('use_rsa') == 'on'
|
||||
|
||||
|
||||
if not use_pin and not use_rsa:
|
||||
flash('You must select at least one security factor (PIN or RSA Key)', 'error')
|
||||
return render_template('generate.html', generated=False, has_qrcode=HAS_QRCODE)
|
||||
|
||||
|
||||
pin_length = int(request.form.get('pin_length', 6))
|
||||
rsa_bits = int(request.form.get('rsa_bits', 2048))
|
||||
|
||||
|
||||
# Clamp values
|
||||
words_per_passphrase = max(MIN_PASSPHRASE_WORDS, min(12, words_per_passphrase))
|
||||
pin_length = max(MIN_PIN_LENGTH, min(MAX_PIN_LENGTH, pin_length))
|
||||
if rsa_bits not in VALID_RSA_SIZES:
|
||||
rsa_bits = 2048
|
||||
|
||||
|
||||
try:
|
||||
# v3.2.0 FIX: Use correct parameter name 'passphrase_words'
|
||||
creds = generate_credentials(
|
||||
@@ -381,19 +378,19 @@ def generate():
|
||||
rsa_bits=rsa_bits,
|
||||
passphrase_words=words_per_passphrase, # FIX: was words_per_passphrase=
|
||||
)
|
||||
|
||||
|
||||
# Store RSA key temporarily for QR generation
|
||||
qr_token = None
|
||||
qr_needs_compression = False
|
||||
qr_too_large = False
|
||||
|
||||
|
||||
if creds.rsa_key_pem and HAS_QRCODE:
|
||||
# Check if key fits in QR code
|
||||
if can_fit_in_qr(creds.rsa_key_pem, compress=True):
|
||||
qr_needs_compression = True
|
||||
else:
|
||||
qr_too_large = True
|
||||
|
||||
|
||||
if not qr_too_large:
|
||||
qr_token = secrets.token_urlsafe(16)
|
||||
cleanup_temp_files()
|
||||
@@ -404,7 +401,7 @@ def generate():
|
||||
'type': 'rsa_key',
|
||||
'compress': qr_needs_compression
|
||||
}
|
||||
|
||||
|
||||
# v3.2.0: Single passphrase instead of daily phrases
|
||||
return render_template('generate.html',
|
||||
passphrase=creds.passphrase, # v3.2.0: Single passphrase
|
||||
@@ -428,7 +425,7 @@ def generate():
|
||||
except Exception as e:
|
||||
flash(f'Error generating credentials: {e}', 'error')
|
||||
return render_template('generate.html', generated=False, has_qrcode=HAS_QRCODE)
|
||||
|
||||
|
||||
return render_template('generate.html', generated=False, has_qrcode=HAS_QRCODE)
|
||||
|
||||
|
||||
@@ -437,19 +434,19 @@ def generate_qr(token):
|
||||
"""Generate QR code for RSA key."""
|
||||
if not HAS_QRCODE:
|
||||
return "QR code support not available", 501
|
||||
|
||||
|
||||
if token not in TEMP_FILES:
|
||||
return "Token expired or invalid", 404
|
||||
|
||||
|
||||
file_info = TEMP_FILES[token]
|
||||
if file_info.get('type') != 'rsa_key':
|
||||
return "Invalid token type", 400
|
||||
|
||||
|
||||
try:
|
||||
key_pem = file_info['data'].decode('utf-8')
|
||||
compress = file_info.get('compress', False)
|
||||
qr_png = generate_qr_code(key_pem, compress=compress)
|
||||
|
||||
|
||||
return send_file(
|
||||
io.BytesIO(qr_png),
|
||||
mimetype='image/png',
|
||||
@@ -464,19 +461,19 @@ def generate_qr_download(token):
|
||||
"""Download QR code as PNG file."""
|
||||
if not HAS_QRCODE:
|
||||
return "QR code support not available", 501
|
||||
|
||||
|
||||
if token not in TEMP_FILES:
|
||||
return "Token expired or invalid", 404
|
||||
|
||||
|
||||
file_info = TEMP_FILES[token]
|
||||
if file_info.get('type') != 'rsa_key':
|
||||
return "Invalid token type", 400
|
||||
|
||||
|
||||
try:
|
||||
key_pem = file_info['data'].decode('utf-8')
|
||||
compress = file_info.get('compress', False)
|
||||
qr_png = generate_qr_code(key_pem, compress=compress)
|
||||
|
||||
|
||||
return send_file(
|
||||
io.BytesIO(qr_png),
|
||||
mimetype='image/png',
|
||||
@@ -491,29 +488,29 @@ def generate_qr_download(token):
|
||||
def qr_crop():
|
||||
"""
|
||||
Detect and crop QR code from an image.
|
||||
|
||||
|
||||
Useful for extracting QR codes from photos taken at an angle,
|
||||
with extra background, etc. Returns the cropped QR as PNG.
|
||||
"""
|
||||
if not HAS_QRCODE_READ:
|
||||
return jsonify({'error': 'QR code reading not available (install pyzbar)'}), 501
|
||||
|
||||
|
||||
image_file = request.files.get('image')
|
||||
if not image_file:
|
||||
return jsonify({'error': 'No image provided'}), 400
|
||||
|
||||
|
||||
try:
|
||||
image_data = image_file.read()
|
||||
|
||||
|
||||
# Use the new crop function
|
||||
cropped = detect_and_crop_qr(image_data)
|
||||
|
||||
|
||||
if cropped is None:
|
||||
return jsonify({'error': 'No QR code detected in image'}), 404
|
||||
|
||||
|
||||
# Return as downloadable PNG or inline based on query param
|
||||
as_attachment = request.args.get('download', '').lower() in ('1', 'true', 'yes')
|
||||
|
||||
|
||||
return send_file(
|
||||
io.BytesIO(cropped),
|
||||
mimetype='image/png',
|
||||
@@ -567,18 +564,18 @@ def extract_key_from_qr_route():
|
||||
'success': False,
|
||||
'error': 'QR code reading not available. Install pyzbar and libzbar.'
|
||||
}), 501
|
||||
|
||||
|
||||
qr_image = request.files.get('qr_image')
|
||||
if not qr_image:
|
||||
return jsonify({
|
||||
'success': False,
|
||||
'error': 'No QR image provided'
|
||||
}), 400
|
||||
|
||||
|
||||
try:
|
||||
image_data = qr_image.read()
|
||||
key_pem = extract_key_from_qr(image_data)
|
||||
|
||||
|
||||
if key_pem:
|
||||
return jsonify({
|
||||
'success': True,
|
||||
@@ -589,7 +586,7 @@ def extract_key_from_qr_route():
|
||||
'success': False,
|
||||
'error': 'No valid RSA key found in QR code'
|
||||
}), 400
|
||||
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({
|
||||
'success': False,
|
||||
@@ -611,16 +608,16 @@ def api_compare_capacity():
|
||||
carrier = request.files.get('carrier')
|
||||
if not carrier:
|
||||
return jsonify({'error': 'No carrier image provided'}), 400
|
||||
|
||||
|
||||
try:
|
||||
carrier_data = carrier.read()
|
||||
|
||||
|
||||
# Use subprocess-isolated compare_modes
|
||||
result = subprocess_stego.compare_modes(carrier_data)
|
||||
|
||||
|
||||
if not result.success:
|
||||
return jsonify({'error': result.error or 'Comparison failed'}), 500
|
||||
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'width': result.width,
|
||||
@@ -652,29 +649,29 @@ def api_check_fit():
|
||||
carrier = request.files.get('carrier')
|
||||
payload_size = request.form.get('payload_size', type=int)
|
||||
embed_mode = request.form.get('embed_mode', 'lsb')
|
||||
|
||||
|
||||
if not carrier or payload_size is None:
|
||||
return jsonify({'error': 'Missing carrier or payload_size'}), 400
|
||||
|
||||
|
||||
if embed_mode not in ('lsb', 'dct'):
|
||||
return jsonify({'error': 'Invalid embed_mode'}), 400
|
||||
|
||||
|
||||
if embed_mode == 'dct' and not has_dct_support():
|
||||
return jsonify({'error': 'DCT mode requires scipy'}), 400
|
||||
|
||||
|
||||
try:
|
||||
carrier_data = carrier.read()
|
||||
|
||||
|
||||
# Use subprocess-isolated capacity check
|
||||
result = subprocess_stego.check_capacity(
|
||||
carrier_data=carrier_data,
|
||||
payload_size=payload_size,
|
||||
embed_mode=embed_mode,
|
||||
)
|
||||
|
||||
|
||||
if not result.success:
|
||||
return jsonify({'error': result.error or 'Capacity check failed'}), 500
|
||||
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'fits': result.fits,
|
||||
@@ -701,55 +698,55 @@ def encode_page():
|
||||
carrier = request.files.get('carrier')
|
||||
rsa_key_file = request.files.get('rsa_key')
|
||||
payload_file = request.files.get('payload_file')
|
||||
|
||||
|
||||
if not ref_photo or not carrier:
|
||||
flash('Both reference photo and carrier image are required', 'error')
|
||||
return render_template('encode.html', has_qrcode_read=HAS_QRCODE_READ)
|
||||
|
||||
|
||||
if not allowed_image(ref_photo.filename) or not allowed_image(carrier.filename):
|
||||
flash('Invalid file type. Use PNG, JPG, or BMP', 'error')
|
||||
return render_template('encode.html', has_qrcode_read=HAS_QRCODE_READ)
|
||||
|
||||
|
||||
# Get form data - v3.2.0: renamed from day_phrase to passphrase
|
||||
message = request.form.get('message', '')
|
||||
passphrase = request.form.get('passphrase', '') # v3.2.0: Renamed
|
||||
pin = request.form.get('pin', '').strip()
|
||||
rsa_password = request.form.get('rsa_password', '')
|
||||
payload_type = request.form.get('payload_type', 'text')
|
||||
|
||||
|
||||
# NEW in v3.0 - Embedding mode
|
||||
embed_mode = request.form.get('embed_mode', 'lsb')
|
||||
if embed_mode not in ('lsb', 'dct'):
|
||||
embed_mode = 'lsb'
|
||||
|
||||
|
||||
# NEW in v3.0.1 - DCT output format
|
||||
dct_output_format = request.form.get('dct_output_format', 'png')
|
||||
if dct_output_format not in ('png', 'jpeg'):
|
||||
dct_output_format = 'png'
|
||||
|
||||
|
||||
# NEW in v3.0.1 - DCT color mode
|
||||
dct_color_mode = request.form.get('dct_color_mode', 'color')
|
||||
if dct_color_mode not in ('grayscale', 'color'):
|
||||
dct_color_mode = 'color'
|
||||
|
||||
|
||||
# NEW in v4.0.0 - Channel key
|
||||
channel_key = resolve_channel_key_form(request.form.get('channel_key', 'auto'))
|
||||
|
||||
|
||||
# Check DCT availability
|
||||
if embed_mode == 'dct' and not has_dct_support():
|
||||
flash('DCT mode requires scipy. Install with: pip install scipy', 'error')
|
||||
return render_template('encode.html', has_qrcode_read=HAS_QRCODE_READ)
|
||||
|
||||
|
||||
# Determine payload
|
||||
if payload_type == 'file' and payload_file and payload_file.filename:
|
||||
# File payload
|
||||
file_data = payload_file.read()
|
||||
|
||||
|
||||
result = validate_file_payload(file_data, payload_file.filename)
|
||||
if not result.is_valid:
|
||||
flash(result.error_message, 'error')
|
||||
return render_template('encode.html', has_qrcode_read=HAS_QRCODE_READ)
|
||||
|
||||
|
||||
mime_type, _ = mimetypes.guess_type(payload_file.filename)
|
||||
payload = FilePayload(
|
||||
data=file_data,
|
||||
@@ -763,31 +760,31 @@ def encode_page():
|
||||
flash(result.error_message, 'error')
|
||||
return render_template('encode.html', has_qrcode_read=HAS_QRCODE_READ)
|
||||
payload = message
|
||||
|
||||
|
||||
# v3.2.0: Renamed from day_phrase
|
||||
if not passphrase:
|
||||
flash('Passphrase is required', 'error')
|
||||
return render_template('encode.html', has_qrcode_read=HAS_QRCODE_READ)
|
||||
|
||||
|
||||
# v3.2.0: Validate passphrase
|
||||
result = validate_passphrase(passphrase)
|
||||
if not result.is_valid:
|
||||
flash(result.error_message, 'error')
|
||||
return render_template('encode.html', has_qrcode_read=HAS_QRCODE_READ)
|
||||
|
||||
|
||||
# Show warning if passphrase is short
|
||||
if result.warning:
|
||||
flash(result.warning, 'warning')
|
||||
|
||||
|
||||
# Read files
|
||||
ref_data = ref_photo.read()
|
||||
carrier_data = carrier.read()
|
||||
|
||||
|
||||
# Handle RSA key - can come from .pem file or QR code image
|
||||
rsa_key_data = None
|
||||
rsa_key_qr = request.files.get('rsa_key_qr')
|
||||
rsa_key_from_qr = False
|
||||
|
||||
|
||||
if rsa_key_file and rsa_key_file.filename:
|
||||
rsa_key_data = rsa_key_file.read()
|
||||
elif rsa_key_qr and rsa_key_qr.filename and HAS_QRCODE_READ:
|
||||
@@ -799,36 +796,36 @@ def encode_page():
|
||||
else:
|
||||
flash('Could not extract RSA key from QR code image.', 'error')
|
||||
return render_template('encode.html', has_qrcode_read=HAS_QRCODE_READ)
|
||||
|
||||
|
||||
# Validate security factors
|
||||
result = validate_security_factors(pin, rsa_key_data)
|
||||
if not result.is_valid:
|
||||
flash(result.error_message, 'error')
|
||||
return render_template('encode.html', has_qrcode_read=HAS_QRCODE_READ)
|
||||
|
||||
|
||||
# Validate PIN if provided
|
||||
if pin:
|
||||
result = validate_pin(pin)
|
||||
if not result.is_valid:
|
||||
flash(result.error_message, 'error')
|
||||
return render_template('encode.html', has_qrcode_read=HAS_QRCODE_READ)
|
||||
|
||||
|
||||
# Determine key password
|
||||
key_password = None if rsa_key_from_qr else (rsa_password if rsa_password else None)
|
||||
|
||||
|
||||
# Validate RSA key if provided
|
||||
if rsa_key_data:
|
||||
result = validate_rsa_key(rsa_key_data, key_password)
|
||||
if not result.is_valid:
|
||||
flash(result.error_message, 'error')
|
||||
return render_template('encode.html', has_qrcode_read=HAS_QRCODE_READ)
|
||||
|
||||
|
||||
# Validate carrier image
|
||||
result = validate_image(carrier_data, "Carrier image")
|
||||
if not result.is_valid:
|
||||
flash(result.error_message, 'error')
|
||||
return render_template('encode.html', has_qrcode_read=HAS_QRCODE_READ)
|
||||
|
||||
|
||||
# v4.0.0: Include channel_key parameter
|
||||
# Use subprocess-isolated encode to prevent crashes
|
||||
if payload_type == 'file' and payload_file and payload_file.filename:
|
||||
@@ -861,14 +858,14 @@ def encode_page():
|
||||
dct_color_mode=dct_color_mode if embed_mode == 'dct' else 'color',
|
||||
channel_key=channel_key, # v4.0.0
|
||||
)
|
||||
|
||||
|
||||
# Check for subprocess errors
|
||||
if not encode_result.success:
|
||||
error_msg = encode_result.error or 'Encoding failed'
|
||||
if 'capacity' in error_msg.lower():
|
||||
raise CapacityError(error_msg)
|
||||
raise StegasooError(error_msg)
|
||||
|
||||
|
||||
# Determine actual output format for filename and storage
|
||||
if embed_mode == 'dct' and dct_output_format == 'jpeg':
|
||||
output_ext = '.jpg'
|
||||
@@ -876,14 +873,14 @@ def encode_page():
|
||||
else:
|
||||
output_ext = '.png'
|
||||
output_mime = 'image/png'
|
||||
|
||||
|
||||
# Use filename from result or generate one
|
||||
filename = encode_result.filename
|
||||
if not filename:
|
||||
filename = generate_filename('stego', output_ext)
|
||||
elif embed_mode == 'dct' and dct_output_format == 'jpeg' and filename.endswith('.png'):
|
||||
filename = filename[:-4] + '.jpg'
|
||||
|
||||
|
||||
# Store temporarily
|
||||
file_id = secrets.token_urlsafe(16)
|
||||
cleanup_temp_files()
|
||||
@@ -899,9 +896,9 @@ def encode_page():
|
||||
'channel_mode': encode_result.channel_mode,
|
||||
'channel_fingerprint': encode_result.channel_fingerprint,
|
||||
}
|
||||
|
||||
|
||||
return redirect(url_for('encode_result', file_id=file_id))
|
||||
|
||||
|
||||
except CapacityError as e:
|
||||
flash(str(e), 'error')
|
||||
return render_template('encode.html', has_qrcode_read=HAS_QRCODE_READ)
|
||||
@@ -911,7 +908,7 @@ def encode_page():
|
||||
except Exception as e:
|
||||
flash(f'Error: {e}', 'error')
|
||||
return render_template('encode.html', has_qrcode_read=HAS_QRCODE_READ)
|
||||
|
||||
|
||||
return render_template('encode.html', has_qrcode_read=HAS_QRCODE_READ)
|
||||
|
||||
|
||||
@@ -920,17 +917,17 @@ def encode_result(file_id):
|
||||
if file_id not in TEMP_FILES:
|
||||
flash('File expired or not found. Please encode again.', 'error')
|
||||
return redirect(url_for('encode_page'))
|
||||
|
||||
|
||||
file_info = TEMP_FILES[file_id]
|
||||
|
||||
|
||||
# Generate thumbnail
|
||||
thumbnail_data = generate_thumbnail(file_info['data'])
|
||||
thumbnail_id = None
|
||||
|
||||
|
||||
if thumbnail_data:
|
||||
thumbnail_id = f"{file_id}_thumb"
|
||||
THUMBNAIL_FILES[thumbnail_id] = thumbnail_data
|
||||
|
||||
|
||||
return render_template('encode_result.html',
|
||||
file_id=file_id,
|
||||
filename=file_info['filename'],
|
||||
@@ -949,7 +946,7 @@ def encode_thumbnail(thumb_id):
|
||||
"""Serve thumbnail image."""
|
||||
if thumb_id not in THUMBNAIL_FILES:
|
||||
return "Thumbnail not found", 404
|
||||
|
||||
|
||||
return send_file(
|
||||
io.BytesIO(THUMBNAIL_FILES[thumb_id]),
|
||||
mimetype='image/jpeg',
|
||||
@@ -962,10 +959,10 @@ def encode_download(file_id):
|
||||
if file_id not in TEMP_FILES:
|
||||
flash('File expired or not found.', 'error')
|
||||
return redirect(url_for('encode_page'))
|
||||
|
||||
|
||||
file_info = TEMP_FILES[file_id]
|
||||
mime_type = file_info.get('mime_type', 'image/png')
|
||||
|
||||
|
||||
return send_file(
|
||||
io.BytesIO(file_info['data']),
|
||||
mimetype=mime_type,
|
||||
@@ -979,10 +976,10 @@ def encode_file_route(file_id):
|
||||
"""Serve file for Web Share API."""
|
||||
if file_id not in TEMP_FILES:
|
||||
return "Not found", 404
|
||||
|
||||
|
||||
file_info = TEMP_FILES[file_id]
|
||||
mime_type = file_info.get('mime_type', 'image/png')
|
||||
|
||||
|
||||
return send_file(
|
||||
io.BytesIO(file_info['data']),
|
||||
mimetype=mime_type,
|
||||
@@ -995,11 +992,11 @@ def encode_file_route(file_id):
|
||||
def encode_cleanup(file_id):
|
||||
"""Manually cleanup a file after sharing."""
|
||||
TEMP_FILES.pop(file_id, None)
|
||||
|
||||
|
||||
# Also cleanup thumbnail if exists
|
||||
thumb_id = f"{file_id}_thumb"
|
||||
THUMBNAIL_FILES.pop(thumb_id, None)
|
||||
|
||||
|
||||
return jsonify({'status': 'ok'})
|
||||
|
||||
|
||||
@@ -1015,45 +1012,45 @@ def decode_page():
|
||||
ref_photo = request.files.get('reference_photo')
|
||||
stego_image = request.files.get('stego_image')
|
||||
rsa_key_file = request.files.get('rsa_key')
|
||||
|
||||
|
||||
if not ref_photo or not stego_image:
|
||||
flash('Both reference photo and stego image are required', 'error')
|
||||
return render_template('decode.html', has_qrcode_read=HAS_QRCODE_READ)
|
||||
|
||||
|
||||
# Get form data - v3.2.0: renamed from day_phrase to passphrase
|
||||
passphrase = request.form.get('passphrase', '') # v3.2.0: Renamed
|
||||
pin = request.form.get('pin', '').strip()
|
||||
rsa_password = request.form.get('rsa_password', '')
|
||||
|
||||
|
||||
# NEW in v3.0 - Extraction mode
|
||||
embed_mode = request.form.get('embed_mode', 'auto')
|
||||
if embed_mode not in ('auto', 'lsb', 'dct'):
|
||||
embed_mode = 'auto'
|
||||
|
||||
|
||||
# NEW in v4.0.0 - Channel key
|
||||
channel_key = resolve_channel_key_form(request.form.get('channel_key', 'auto'))
|
||||
|
||||
|
||||
# Check DCT availability
|
||||
if embed_mode == 'dct' and not has_dct_support():
|
||||
flash('DCT mode requires scipy. Install with: pip install scipy', 'error')
|
||||
return render_template('decode.html', has_qrcode_read=HAS_QRCODE_READ)
|
||||
|
||||
|
||||
# v3.2.0: Removed date handling (no stego_date needed)
|
||||
|
||||
|
||||
# v3.2.0: Renamed from day_phrase
|
||||
if not passphrase:
|
||||
flash('Passphrase is required', 'error')
|
||||
return render_template('decode.html', has_qrcode_read=HAS_QRCODE_READ)
|
||||
|
||||
|
||||
# Read files
|
||||
ref_data = ref_photo.read()
|
||||
stego_data = stego_image.read()
|
||||
|
||||
|
||||
# Handle RSA key - can come from .pem file or QR code image
|
||||
rsa_key_data = None
|
||||
rsa_key_qr = request.files.get('rsa_key_qr')
|
||||
rsa_key_from_qr = False
|
||||
|
||||
|
||||
if rsa_key_file and rsa_key_file.filename:
|
||||
rsa_key_data = rsa_key_file.read()
|
||||
elif rsa_key_qr and rsa_key_qr.filename and HAS_QRCODE_READ:
|
||||
@@ -1065,30 +1062,30 @@ def decode_page():
|
||||
else:
|
||||
flash('Could not extract RSA key from QR code image.', 'error')
|
||||
return render_template('decode.html', has_qrcode_read=HAS_QRCODE_READ)
|
||||
|
||||
|
||||
# Validate security factors
|
||||
result = validate_security_factors(pin, rsa_key_data)
|
||||
if not result.is_valid:
|
||||
flash(result.error_message, 'error')
|
||||
return render_template('decode.html', has_qrcode_read=HAS_QRCODE_READ)
|
||||
|
||||
|
||||
# Validate PIN if provided
|
||||
if pin:
|
||||
result = validate_pin(pin)
|
||||
if not result.is_valid:
|
||||
flash(result.error_message, 'error')
|
||||
return render_template('decode.html', has_qrcode_read=HAS_QRCODE_READ)
|
||||
|
||||
|
||||
# Determine key password
|
||||
key_password = None if rsa_key_from_qr else (rsa_password if rsa_password else None)
|
||||
|
||||
|
||||
# Validate RSA key if provided
|
||||
if rsa_key_data:
|
||||
result = validate_rsa_key(rsa_key_data, key_password)
|
||||
if not result.is_valid:
|
||||
flash(result.error_message, 'error')
|
||||
return render_template('decode.html', has_qrcode_read=HAS_QRCODE_READ)
|
||||
|
||||
|
||||
# v4.0.0: Include channel_key parameter
|
||||
# Use subprocess-isolated decode to prevent crashes
|
||||
decode_result = subprocess_stego.decode(
|
||||
@@ -1101,7 +1098,7 @@ def decode_page():
|
||||
embed_mode=embed_mode,
|
||||
channel_key=channel_key, # v4.0.0
|
||||
)
|
||||
|
||||
|
||||
# Check for subprocess errors
|
||||
if not decode_result.success:
|
||||
error_msg = decode_result.error or 'Decoding failed'
|
||||
@@ -1112,12 +1109,12 @@ def decode_page():
|
||||
if 'decrypt' in error_msg.lower() or decode_result.error_type == 'DecryptionError':
|
||||
raise DecryptionError(error_msg)
|
||||
raise StegasooError(error_msg)
|
||||
|
||||
|
||||
if decode_result.is_file:
|
||||
# File content - store temporarily for download
|
||||
file_id = secrets.token_urlsafe(16)
|
||||
cleanup_temp_files()
|
||||
|
||||
|
||||
filename = decode_result.filename or 'decoded_file'
|
||||
TEMP_FILES[file_id] = {
|
||||
'data': decode_result.file_data,
|
||||
@@ -1125,7 +1122,7 @@ def decode_page():
|
||||
'mime_type': decode_result.mime_type,
|
||||
'timestamp': time.time()
|
||||
}
|
||||
|
||||
|
||||
return render_template('decode.html',
|
||||
decoded_file=True,
|
||||
file_id=file_id,
|
||||
@@ -1136,11 +1133,11 @@ def decode_page():
|
||||
)
|
||||
else:
|
||||
# Text content
|
||||
return render_template('decode.html',
|
||||
return render_template('decode.html',
|
||||
decoded_message=decode_result.message,
|
||||
has_qrcode_read=HAS_QRCODE_READ
|
||||
)
|
||||
|
||||
|
||||
except DecryptionError:
|
||||
flash('Decryption failed. Check your passphrase, PIN, RSA key, reference photo, and channel key.', 'error')
|
||||
return render_template('decode.html', has_qrcode_read=HAS_QRCODE_READ)
|
||||
@@ -1150,7 +1147,7 @@ def decode_page():
|
||||
except Exception as e:
|
||||
flash(f'Error: {e}', 'error')
|
||||
return render_template('decode.html', has_qrcode_read=HAS_QRCODE_READ)
|
||||
|
||||
|
||||
return render_template('decode.html', has_qrcode_read=HAS_QRCODE_READ)
|
||||
|
||||
|
||||
@@ -1160,10 +1157,10 @@ def decode_download(file_id):
|
||||
if file_id not in TEMP_FILES:
|
||||
flash('File expired or not found.', 'error')
|
||||
return redirect(url_for('decode_page'))
|
||||
|
||||
|
||||
file_info = TEMP_FILES[file_id]
|
||||
mime_type = file_info.get('mime_type', 'application/octet-stream')
|
||||
|
||||
|
||||
return send_file(
|
||||
io.BytesIO(file_info['data']),
|
||||
mimetype=mime_type,
|
||||
@@ -1174,7 +1171,7 @@ def decode_download(file_id):
|
||||
|
||||
@app.route('/about')
|
||||
def about():
|
||||
return render_template('about.html',
|
||||
return render_template('about.html',
|
||||
has_argon2=has_argon2(),
|
||||
has_qrcode_read=HAS_QRCODE_READ
|
||||
)
|
||||
@@ -1188,7 +1185,7 @@ def test_capacity():
|
||||
carrier = request.files.get('carrier')
|
||||
if not carrier:
|
||||
return jsonify({'error': 'No carrier image provided'}), 400
|
||||
|
||||
|
||||
try:
|
||||
carrier_data = carrier.read()
|
||||
buffer = io.BytesIO(carrier_data)
|
||||
@@ -1197,11 +1194,11 @@ def test_capacity():
|
||||
fmt = img.format
|
||||
img.close()
|
||||
buffer.close()
|
||||
|
||||
|
||||
pixels = width * height
|
||||
lsb_bytes = (pixels * 3) // 8
|
||||
dct_bytes = ((width // 8) * (height // 8) * 16) // 8 - 10
|
||||
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'width': width,
|
||||
@@ -1220,7 +1217,7 @@ def test_capacity_nopil():
|
||||
carrier = request.files.get('carrier')
|
||||
if not carrier:
|
||||
return jsonify({'error': 'No carrier image provided'}), 400
|
||||
|
||||
|
||||
carrier_data = carrier.read()
|
||||
return jsonify({
|
||||
'success': True,
|
||||
|
||||
@@ -17,9 +17,9 @@ Usage:
|
||||
echo '{"operation": "encode", ...}' | python stego_worker.py
|
||||
"""
|
||||
|
||||
import sys
|
||||
import json
|
||||
import base64
|
||||
import json
|
||||
import sys
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
|
||||
@@ -31,10 +31,10 @@ sys.path.insert(0, str(Path(__file__).parent))
|
||||
def _resolve_channel_key(channel_key_param):
|
||||
"""
|
||||
Resolve channel_key parameter to value for stegasoo.
|
||||
|
||||
|
||||
Args:
|
||||
channel_key_param: 'auto', 'none', explicit key, or None
|
||||
|
||||
|
||||
Returns:
|
||||
None (auto), "" (public), or explicit key string
|
||||
"""
|
||||
@@ -49,41 +49,41 @@ def _resolve_channel_key(channel_key_param):
|
||||
def _get_channel_info(resolved_key):
|
||||
"""
|
||||
Get channel mode and fingerprint for response.
|
||||
|
||||
|
||||
Returns:
|
||||
(mode, fingerprint) tuple
|
||||
"""
|
||||
from stegasoo import has_channel_key, get_channel_status
|
||||
|
||||
from stegasoo import get_channel_status, has_channel_key
|
||||
|
||||
if resolved_key == "":
|
||||
return "public", None
|
||||
|
||||
|
||||
if resolved_key is not None:
|
||||
# Explicit key
|
||||
fingerprint = f"{resolved_key[:4]}-••••-••••-••••-••••-••••-••••-{resolved_key[-4:]}"
|
||||
return "private", fingerprint
|
||||
|
||||
|
||||
# Auto mode - check server config
|
||||
if has_channel_key():
|
||||
status = get_channel_status()
|
||||
return "private", status.get('fingerprint')
|
||||
|
||||
|
||||
return "public", None
|
||||
|
||||
|
||||
def encode_operation(params: dict) -> dict:
|
||||
"""Handle encode operation."""
|
||||
from stegasoo import encode, FilePayload
|
||||
|
||||
from stegasoo import FilePayload, encode
|
||||
|
||||
# Decode base64 inputs
|
||||
carrier_data = base64.b64decode(params['carrier_b64'])
|
||||
reference_data = base64.b64decode(params['reference_b64'])
|
||||
|
||||
|
||||
# Optional RSA key
|
||||
rsa_key_data = None
|
||||
if params.get('rsa_key_b64'):
|
||||
rsa_key_data = base64.b64decode(params['rsa_key_b64'])
|
||||
|
||||
|
||||
# Determine payload type
|
||||
if params.get('file_b64'):
|
||||
file_data = base64.b64decode(params['file_b64'])
|
||||
@@ -94,10 +94,10 @@ def encode_operation(params: dict) -> dict:
|
||||
)
|
||||
else:
|
||||
payload = params.get('message', '')
|
||||
|
||||
|
||||
# Resolve channel key (v4.0.0)
|
||||
resolved_channel_key = _resolve_channel_key(params.get('channel_key', 'auto'))
|
||||
|
||||
|
||||
# Call encode with correct parameter names
|
||||
result = encode(
|
||||
message=payload,
|
||||
@@ -112,7 +112,7 @@ def encode_operation(params: dict) -> dict:
|
||||
dct_color_mode=params.get('dct_color_mode', 'color'),
|
||||
channel_key=resolved_channel_key, # v4.0.0
|
||||
)
|
||||
|
||||
|
||||
# Build stats dict if available
|
||||
stats = None
|
||||
if hasattr(result, 'stats') and result.stats:
|
||||
@@ -121,10 +121,10 @@ def encode_operation(params: dict) -> dict:
|
||||
'capacity_used': getattr(result.stats, 'capacity_used', 0),
|
||||
'bytes_embedded': getattr(result.stats, 'bytes_embedded', 0),
|
||||
}
|
||||
|
||||
|
||||
# Get channel info for response (v4.0.0)
|
||||
channel_mode, channel_fingerprint = _get_channel_info(resolved_channel_key)
|
||||
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
'stego_b64': base64.b64encode(result.stego_image).decode('ascii'),
|
||||
@@ -138,19 +138,19 @@ def encode_operation(params: dict) -> dict:
|
||||
def decode_operation(params: dict) -> dict:
|
||||
"""Handle decode operation."""
|
||||
from stegasoo import decode
|
||||
|
||||
|
||||
# Decode base64 inputs
|
||||
stego_data = base64.b64decode(params['stego_b64'])
|
||||
reference_data = base64.b64decode(params['reference_b64'])
|
||||
|
||||
|
||||
# Optional RSA key
|
||||
rsa_key_data = None
|
||||
if params.get('rsa_key_b64'):
|
||||
rsa_key_data = base64.b64decode(params['rsa_key_b64'])
|
||||
|
||||
|
||||
# Resolve channel key (v4.0.0)
|
||||
resolved_channel_key = _resolve_channel_key(params.get('channel_key', 'auto'))
|
||||
|
||||
|
||||
# Call decode with correct parameter names
|
||||
result = decode(
|
||||
stego_image=stego_data,
|
||||
@@ -162,7 +162,7 @@ def decode_operation(params: dict) -> dict:
|
||||
embed_mode=params.get('embed_mode', 'auto'),
|
||||
channel_key=resolved_channel_key, # v4.0.0
|
||||
)
|
||||
|
||||
|
||||
if result.is_file:
|
||||
return {
|
||||
'success': True,
|
||||
@@ -182,10 +182,10 @@ def decode_operation(params: dict) -> dict:
|
||||
def compare_operation(params: dict) -> dict:
|
||||
"""Handle compare_modes operation."""
|
||||
from stegasoo import compare_modes
|
||||
|
||||
|
||||
carrier_data = base64.b64decode(params['carrier_b64'])
|
||||
result = compare_modes(carrier_data)
|
||||
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
'comparison': result,
|
||||
@@ -195,15 +195,15 @@ def compare_operation(params: dict) -> dict:
|
||||
def capacity_check_operation(params: dict) -> dict:
|
||||
"""Handle will_fit_by_mode operation."""
|
||||
from stegasoo import will_fit_by_mode
|
||||
|
||||
|
||||
carrier_data = base64.b64decode(params['carrier_b64'])
|
||||
|
||||
|
||||
result = will_fit_by_mode(
|
||||
payload=params['payload_size'],
|
||||
carrier_image=carrier_data,
|
||||
embed_mode=params.get('embed_mode', 'lsb'),
|
||||
)
|
||||
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
'result': result,
|
||||
@@ -213,10 +213,10 @@ def capacity_check_operation(params: dict) -> dict:
|
||||
def channel_status_operation(params: dict) -> dict:
|
||||
"""Handle channel status check (v4.0.0)."""
|
||||
from stegasoo import get_channel_status
|
||||
|
||||
|
||||
status = get_channel_status()
|
||||
reveal = params.get('reveal', False)
|
||||
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
'status': {
|
||||
@@ -234,13 +234,13 @@ def main():
|
||||
try:
|
||||
# Read all input
|
||||
input_text = sys.stdin.read()
|
||||
|
||||
|
||||
if not input_text.strip():
|
||||
output = {'success': False, 'error': 'No input provided'}
|
||||
else:
|
||||
params = json.loads(input_text)
|
||||
operation = params.get('operation')
|
||||
|
||||
|
||||
if operation == 'encode':
|
||||
output = encode_operation(params)
|
||||
elif operation == 'decode':
|
||||
@@ -253,7 +253,7 @@ def main():
|
||||
output = channel_status_operation(params)
|
||||
else:
|
||||
output = {'success': False, 'error': f'Unknown operation: {operation}'}
|
||||
|
||||
|
||||
except json.JSONDecodeError as e:
|
||||
output = {'success': False, 'error': f'Invalid JSON: {e}'}
|
||||
except Exception as e:
|
||||
@@ -263,7 +263,7 @@ def main():
|
||||
'error_type': type(e).__name__,
|
||||
'traceback': traceback.format_exc(),
|
||||
}
|
||||
|
||||
|
||||
# Write output as JSON
|
||||
print(json.dumps(output), flush=True)
|
||||
|
||||
|
||||
@@ -10,9 +10,9 @@ CHANGES in v4.0.0:
|
||||
|
||||
Usage:
|
||||
from subprocess_stego import SubprocessStego
|
||||
|
||||
|
||||
stego = SubprocessStego()
|
||||
|
||||
|
||||
# Encode with channel key
|
||||
result = stego.encode(
|
||||
carrier_data=carrier_bytes,
|
||||
@@ -23,13 +23,13 @@ Usage:
|
||||
embed_mode="dct",
|
||||
channel_key="auto", # or "none", or explicit key
|
||||
)
|
||||
|
||||
|
||||
if result.success:
|
||||
stego_bytes = result.stego_data
|
||||
extension = result.extension
|
||||
else:
|
||||
error_message = result.error
|
||||
|
||||
|
||||
# Decode
|
||||
result = stego.decode(
|
||||
stego_data=stego_bytes,
|
||||
@@ -38,19 +38,18 @@ Usage:
|
||||
pin="123456",
|
||||
channel_key="auto",
|
||||
)
|
||||
|
||||
|
||||
# Compare modes (capacity)
|
||||
result = stego.compare_modes(carrier_bytes)
|
||||
"""
|
||||
|
||||
import json
|
||||
import base64
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional, Dict, Any, Union
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
# Default timeout for operations (seconds)
|
||||
DEFAULT_TIMEOUT = 120
|
||||
@@ -63,14 +62,14 @@ WORKER_SCRIPT = Path(__file__).parent / 'stego_worker.py'
|
||||
class EncodeResult:
|
||||
"""Result from encode operation."""
|
||||
success: bool
|
||||
stego_data: Optional[bytes] = None
|
||||
filename: Optional[str] = None
|
||||
stats: Optional[Dict[str, Any]] = None
|
||||
stego_data: bytes | None = None
|
||||
filename: str | None = None
|
||||
stats: dict[str, Any] | None = None
|
||||
# Channel info (v4.0.0)
|
||||
channel_mode: Optional[str] = None
|
||||
channel_fingerprint: Optional[str] = None
|
||||
error: Optional[str] = None
|
||||
error_type: Optional[str] = None
|
||||
channel_mode: str | None = None
|
||||
channel_fingerprint: str | None = None
|
||||
error: str | None = None
|
||||
error_type: str | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -78,12 +77,12 @@ class DecodeResult:
|
||||
"""Result from decode operation."""
|
||||
success: bool
|
||||
is_file: bool = False
|
||||
message: Optional[str] = None
|
||||
file_data: Optional[bytes] = None
|
||||
filename: Optional[str] = None
|
||||
mime_type: Optional[str] = None
|
||||
error: Optional[str] = None
|
||||
error_type: Optional[str] = None
|
||||
message: str | None = None
|
||||
file_data: bytes | None = None
|
||||
filename: str | None = None
|
||||
mime_type: str | None = None
|
||||
error: str | None = None
|
||||
error_type: str | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -92,9 +91,9 @@ class CompareResult:
|
||||
success: bool
|
||||
width: int = 0
|
||||
height: int = 0
|
||||
lsb: Optional[Dict[str, Any]] = None
|
||||
dct: Optional[Dict[str, Any]] = None
|
||||
error: Optional[str] = None
|
||||
lsb: dict[str, Any] | None = None
|
||||
dct: dict[str, Any] | None = None
|
||||
error: str | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -107,38 +106,38 @@ class CapacityResult:
|
||||
usage_percent: float = 0.0
|
||||
headroom: int = 0
|
||||
mode: str = ""
|
||||
error: Optional[str] = None
|
||||
error: str | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
@dataclass
|
||||
class ChannelStatusResult:
|
||||
"""Result from channel status check (v4.0.0)."""
|
||||
success: bool
|
||||
mode: str = "public"
|
||||
configured: bool = False
|
||||
fingerprint: Optional[str] = None
|
||||
source: Optional[str] = None
|
||||
key: Optional[str] = None
|
||||
error: Optional[str] = None
|
||||
fingerprint: str | None = None
|
||||
source: str | None = None
|
||||
key: str | None = None
|
||||
error: str | None = None
|
||||
|
||||
|
||||
class SubprocessStego:
|
||||
"""
|
||||
Subprocess-isolated steganography operations.
|
||||
|
||||
|
||||
All operations run in a separate Python process. If jpegio or scipy
|
||||
crashes, only the subprocess dies - Flask keeps running.
|
||||
"""
|
||||
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
worker_path: Optional[Path] = None,
|
||||
python_executable: Optional[str] = None,
|
||||
worker_path: Path | None = None,
|
||||
python_executable: str | None = None,
|
||||
timeout: int = DEFAULT_TIMEOUT,
|
||||
):
|
||||
"""
|
||||
Initialize subprocess wrapper.
|
||||
|
||||
|
||||
Args:
|
||||
worker_path: Path to stego_worker.py (default: same directory)
|
||||
python_executable: Python interpreter to use (default: same as current)
|
||||
@@ -147,24 +146,24 @@ class SubprocessStego:
|
||||
self.worker_path = worker_path or WORKER_SCRIPT
|
||||
self.python = python_executable or sys.executable
|
||||
self.timeout = timeout
|
||||
|
||||
|
||||
if not self.worker_path.exists():
|
||||
raise FileNotFoundError(f"Worker script not found: {self.worker_path}")
|
||||
|
||||
def _run_worker(self, params: Dict[str, Any], timeout: Optional[int] = None) -> Dict[str, Any]:
|
||||
|
||||
def _run_worker(self, params: dict[str, Any], timeout: int | None = None) -> dict[str, Any]:
|
||||
"""
|
||||
Run the worker subprocess with given parameters.
|
||||
|
||||
|
||||
Args:
|
||||
params: Dictionary of parameters (will be JSON-encoded)
|
||||
timeout: Operation timeout in seconds
|
||||
|
||||
|
||||
Returns:
|
||||
Dictionary with results from worker
|
||||
"""
|
||||
timeout = timeout or self.timeout
|
||||
input_json = json.dumps(params)
|
||||
|
||||
|
||||
try:
|
||||
result = subprocess.run(
|
||||
[self.python, str(self.worker_path)],
|
||||
@@ -174,7 +173,7 @@ class SubprocessStego:
|
||||
timeout=timeout,
|
||||
cwd=str(self.worker_path.parent),
|
||||
)
|
||||
|
||||
|
||||
if result.returncode != 0:
|
||||
# Worker crashed
|
||||
return {
|
||||
@@ -182,16 +181,16 @@ class SubprocessStego:
|
||||
'error': f'Worker crashed (exit code {result.returncode})',
|
||||
'stderr': result.stderr,
|
||||
}
|
||||
|
||||
|
||||
if not result.stdout.strip():
|
||||
return {
|
||||
'success': False,
|
||||
'error': 'Worker returned empty output',
|
||||
'stderr': result.stderr,
|
||||
}
|
||||
|
||||
|
||||
return json.loads(result.stdout)
|
||||
|
||||
|
||||
except subprocess.TimeoutExpired:
|
||||
return {
|
||||
'success': False,
|
||||
@@ -210,29 +209,29 @@ class SubprocessStego:
|
||||
'error': str(e),
|
||||
'error_type': type(e).__name__,
|
||||
}
|
||||
|
||||
|
||||
def encode(
|
||||
self,
|
||||
carrier_data: bytes,
|
||||
reference_data: bytes,
|
||||
message: Optional[str] = None,
|
||||
file_data: Optional[bytes] = None,
|
||||
file_name: Optional[str] = None,
|
||||
file_mime: Optional[str] = None,
|
||||
message: str | None = None,
|
||||
file_data: bytes | None = None,
|
||||
file_name: str | None = None,
|
||||
file_mime: str | None = None,
|
||||
passphrase: str = "",
|
||||
pin: Optional[str] = None,
|
||||
rsa_key_data: Optional[bytes] = None,
|
||||
rsa_password: Optional[str] = None,
|
||||
pin: str | None = None,
|
||||
rsa_key_data: bytes | None = None,
|
||||
rsa_password: str | None = None,
|
||||
embed_mode: str = "lsb",
|
||||
dct_output_format: str = "png",
|
||||
dct_color_mode: str = "color",
|
||||
# Channel key (v4.0.0)
|
||||
channel_key: Optional[str] = "auto",
|
||||
timeout: Optional[int] = None,
|
||||
channel_key: str | None = "auto",
|
||||
timeout: int | None = None,
|
||||
) -> EncodeResult:
|
||||
"""
|
||||
Encode a message or file into an image.
|
||||
|
||||
|
||||
Args:
|
||||
carrier_data: Carrier image bytes
|
||||
reference_data: Reference photo bytes
|
||||
@@ -249,7 +248,7 @@ class SubprocessStego:
|
||||
dct_color_mode: 'grayscale' or 'color' (for DCT mode)
|
||||
channel_key: 'auto' (server config), 'none' (public), or explicit key (v4.0.0)
|
||||
timeout: Operation timeout in seconds
|
||||
|
||||
|
||||
Returns:
|
||||
EncodeResult with stego_data and extension on success
|
||||
"""
|
||||
@@ -265,18 +264,18 @@ class SubprocessStego:
|
||||
'dct_color_mode': dct_color_mode,
|
||||
'channel_key': channel_key, # v4.0.0
|
||||
}
|
||||
|
||||
|
||||
if file_data:
|
||||
params['file_b64'] = base64.b64encode(file_data).decode('ascii')
|
||||
params['file_name'] = file_name
|
||||
params['file_mime'] = file_mime
|
||||
|
||||
|
||||
if rsa_key_data:
|
||||
params['rsa_key_b64'] = base64.b64encode(rsa_key_data).decode('ascii')
|
||||
params['rsa_password'] = rsa_password
|
||||
|
||||
|
||||
result = self._run_worker(params, timeout)
|
||||
|
||||
|
||||
if result.get('success'):
|
||||
return EncodeResult(
|
||||
success=True,
|
||||
@@ -292,23 +291,23 @@ class SubprocessStego:
|
||||
error=result.get('error', 'Unknown error'),
|
||||
error_type=result.get('error_type'),
|
||||
)
|
||||
|
||||
|
||||
def decode(
|
||||
self,
|
||||
stego_data: bytes,
|
||||
reference_data: bytes,
|
||||
passphrase: str = "",
|
||||
pin: Optional[str] = None,
|
||||
rsa_key_data: Optional[bytes] = None,
|
||||
rsa_password: Optional[str] = None,
|
||||
pin: str | None = None,
|
||||
rsa_key_data: bytes | None = None,
|
||||
rsa_password: str | None = None,
|
||||
embed_mode: str = "auto",
|
||||
# Channel key (v4.0.0)
|
||||
channel_key: Optional[str] = "auto",
|
||||
timeout: Optional[int] = None,
|
||||
channel_key: str | None = "auto",
|
||||
timeout: int | None = None,
|
||||
) -> DecodeResult:
|
||||
"""
|
||||
Decode a message or file from a stego image.
|
||||
|
||||
|
||||
Args:
|
||||
stego_data: Stego image bytes
|
||||
reference_data: Reference photo bytes
|
||||
@@ -319,7 +318,7 @@ class SubprocessStego:
|
||||
embed_mode: 'auto', 'lsb', or 'dct'
|
||||
channel_key: 'auto' (server config), 'none' (public), or explicit key (v4.0.0)
|
||||
timeout: Operation timeout in seconds
|
||||
|
||||
|
||||
Returns:
|
||||
DecodeResult with message or file_data on success
|
||||
"""
|
||||
@@ -332,13 +331,13 @@ class SubprocessStego:
|
||||
'embed_mode': embed_mode,
|
||||
'channel_key': channel_key, # v4.0.0
|
||||
}
|
||||
|
||||
|
||||
if rsa_key_data:
|
||||
params['rsa_key_b64'] = base64.b64encode(rsa_key_data).decode('ascii')
|
||||
params['rsa_password'] = rsa_password
|
||||
|
||||
|
||||
result = self._run_worker(params, timeout)
|
||||
|
||||
|
||||
if result.get('success'):
|
||||
if result.get('is_file'):
|
||||
return DecodeResult(
|
||||
@@ -360,19 +359,19 @@ class SubprocessStego:
|
||||
error=result.get('error', 'Unknown error'),
|
||||
error_type=result.get('error_type'),
|
||||
)
|
||||
|
||||
|
||||
def compare_modes(
|
||||
self,
|
||||
carrier_data: bytes,
|
||||
timeout: Optional[int] = None,
|
||||
timeout: int | None = None,
|
||||
) -> CompareResult:
|
||||
"""
|
||||
Compare LSB and DCT capacity for a carrier image.
|
||||
|
||||
|
||||
Args:
|
||||
carrier_data: Carrier image bytes
|
||||
timeout: Operation timeout in seconds
|
||||
|
||||
|
||||
Returns:
|
||||
CompareResult with capacity information
|
||||
"""
|
||||
@@ -380,9 +379,9 @@ class SubprocessStego:
|
||||
'operation': 'compare',
|
||||
'carrier_b64': base64.b64encode(carrier_data).decode('ascii'),
|
||||
}
|
||||
|
||||
|
||||
result = self._run_worker(params, timeout)
|
||||
|
||||
|
||||
if result.get('success'):
|
||||
comparison = result.get('comparison', {})
|
||||
return CompareResult(
|
||||
@@ -397,23 +396,23 @@ class SubprocessStego:
|
||||
success=False,
|
||||
error=result.get('error', 'Unknown error'),
|
||||
)
|
||||
|
||||
|
||||
def check_capacity(
|
||||
self,
|
||||
carrier_data: bytes,
|
||||
payload_size: int,
|
||||
embed_mode: str = "lsb",
|
||||
timeout: Optional[int] = None,
|
||||
timeout: int | None = None,
|
||||
) -> CapacityResult:
|
||||
"""
|
||||
Check if a payload will fit in the carrier.
|
||||
|
||||
|
||||
Args:
|
||||
carrier_data: Carrier image bytes
|
||||
payload_size: Size of payload in bytes
|
||||
embed_mode: 'lsb' or 'dct'
|
||||
timeout: Operation timeout in seconds
|
||||
|
||||
|
||||
Returns:
|
||||
CapacityResult with fit information
|
||||
"""
|
||||
@@ -423,9 +422,9 @@ class SubprocessStego:
|
||||
'payload_size': payload_size,
|
||||
'embed_mode': embed_mode,
|
||||
}
|
||||
|
||||
|
||||
result = self._run_worker(params, timeout)
|
||||
|
||||
|
||||
if result.get('success'):
|
||||
r = result.get('result', {})
|
||||
return CapacityResult(
|
||||
@@ -442,19 +441,19 @@ class SubprocessStego:
|
||||
success=False,
|
||||
error=result.get('error', 'Unknown error'),
|
||||
)
|
||||
|
||||
|
||||
def get_channel_status(
|
||||
self,
|
||||
reveal: bool = False,
|
||||
timeout: Optional[int] = None,
|
||||
timeout: int | None = None,
|
||||
) -> ChannelStatusResult:
|
||||
"""
|
||||
Get current channel key status (v4.0.0).
|
||||
|
||||
|
||||
Args:
|
||||
reveal: Include full key in response
|
||||
timeout: Operation timeout in seconds
|
||||
|
||||
|
||||
Returns:
|
||||
ChannelStatusResult with channel info
|
||||
"""
|
||||
@@ -462,9 +461,9 @@ class SubprocessStego:
|
||||
'operation': 'channel_status',
|
||||
'reveal': reveal,
|
||||
}
|
||||
|
||||
|
||||
result = self._run_worker(params, timeout)
|
||||
|
||||
|
||||
if result.get('success'):
|
||||
status = result.get('status', {})
|
||||
return ChannelStatusResult(
|
||||
@@ -483,7 +482,7 @@ class SubprocessStego:
|
||||
|
||||
|
||||
# Convenience function for quick usage
|
||||
_default_stego: Optional[SubprocessStego] = None
|
||||
_default_stego: SubprocessStego | None = None
|
||||
|
||||
|
||||
def get_subprocess_stego() -> SubprocessStego:
|
||||
|
||||
@@ -1,90 +0,0 @@
|
||||
"""
|
||||
Minimal test to isolate the memory corruption crash.
|
||||
|
||||
Add this route to your app.py temporarily to test if the crash
|
||||
is in Flask/Pillow or in stegasoo code.
|
||||
|
||||
Usage:
|
||||
1. Add this code to app.py
|
||||
2. Restart the server
|
||||
3. Use the /test-capacity endpoint instead of /api/compare-capacity
|
||||
4. If it crashes: Flask or Pillow issue
|
||||
5. If it works: Stegasoo code issue
|
||||
"""
|
||||
|
||||
# Add these imports at the top of app.py if not present:
|
||||
# from PIL import Image
|
||||
# import io
|
||||
|
||||
# Add this route to app.py:
|
||||
|
||||
@app.route('/test-capacity', methods=['POST'])
|
||||
def test_capacity():
|
||||
"""
|
||||
Minimal capacity test - no stegasoo code, just PIL.
|
||||
"""
|
||||
carrier = request.files.get('carrier')
|
||||
if not carrier:
|
||||
return jsonify({'error': 'No carrier image provided'}), 400
|
||||
|
||||
try:
|
||||
# Read the file data
|
||||
carrier_data = carrier.read()
|
||||
|
||||
# Method 1: Just get size from PIL
|
||||
buffer = io.BytesIO(carrier_data)
|
||||
img = Image.open(buffer)
|
||||
width, height = img.size
|
||||
fmt = img.format
|
||||
mode = img.mode
|
||||
img.close()
|
||||
buffer.close()
|
||||
|
||||
# Simple capacity calculation (no scipy, no numpy)
|
||||
pixels = width * height
|
||||
lsb_bytes = (pixels * 3) // 8
|
||||
blocks = (width // 8) * (height // 8)
|
||||
dct_bytes = (blocks * 16) // 8 - 10
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'width': width,
|
||||
'height': height,
|
||||
'format': fmt,
|
||||
'mode': mode,
|
||||
'lsb': {
|
||||
'capacity_bytes': lsb_bytes,
|
||||
'capacity_kb': round(lsb_bytes / 1024, 1),
|
||||
},
|
||||
'dct': {
|
||||
'capacity_bytes': dct_bytes,
|
||||
'capacity_kb': round(dct_bytes / 1024, 1),
|
||||
}
|
||||
})
|
||||
except Exception as e:
|
||||
import traceback
|
||||
return jsonify({'error': str(e), 'trace': traceback.format_exc()}), 500
|
||||
|
||||
|
||||
# Alternative: completely bypass PIL too
|
||||
@app.route('/test-capacity-nopil', methods=['POST'])
|
||||
def test_capacity_nopil():
|
||||
"""
|
||||
Ultra-minimal test - no PIL, no stegasoo.
|
||||
"""
|
||||
carrier = request.files.get('carrier')
|
||||
if not carrier:
|
||||
return jsonify({'error': 'No carrier image provided'}), 400
|
||||
|
||||
try:
|
||||
carrier_data = carrier.read()
|
||||
|
||||
# Just return size info, no image processing at all
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'data_size': len(carrier_data),
|
||||
'first_bytes': carrier_data[:20].hex() if len(carrier_data) >= 20 else carrier_data.hex(),
|
||||
})
|
||||
except Exception as e:
|
||||
import traceback
|
||||
return jsonify({'error': str(e), 'trace': traceback.format_exc()}), 500
|
||||
Reference in New Issue
Block a user