initial scripts

This commit is contained in:
Radek
2026-03-11 15:45:22 +00:00
parent 3dc3affb46
commit dda1ecf619
3 changed files with 154 additions and 2 deletions

47
entropy-rng-cam.py Normal file
View File

@@ -0,0 +1,47 @@
import hashlib
import requests
from io import BytesIO
from PIL import Image
import secrets
def fetch_single_frame(url):
response = requests.get(url, stream=True)
boundary = b'--myboundary'
data = b''
in_frame = False
frame_data = b''
for chunk in response.iter_content(chunk_size=1024):
data += chunk
if not in_frame and boundary in data:
in_frame = True
data = data.split(boundary, 1)[1]
if in_frame and b'Content-Type: image/jpeg' in data:
parts = data.split(b'\r\n\r\n', 1)
if len(parts) > 1:
frame_part = parts[1]
if b'\r\n--myboundary' in frame_part:
frame_data = frame_part.split(b'\r\n--myboundary', 1)[0]
try:
return Image.open(BytesIO(frame_data))
except Exception as e:
print(f"Error processing frame: {e}")
return None
raise ValueError("No valid frame found in the stream.")
def generate_random_numbers(frame, bit_lengths=[16, 32, 64, 128, 256]):
img_bytes = frame.resize((100, 100)).tobytes()
entropy = hashlib.sha256(img_bytes).digest()
# Use secrets.randbits directly
return {length: secrets.randbits(length) for length in bit_lengths}
url = "http://192.168.0.200/mjpg/video.mjpg"
try:
frame = fetch_single_frame(url)
if frame:
random_numbers = generate_random_numbers(frame)
for length, number in random_numbers.items():
print(f"{length}-bit random number: {hex(number)}")
else:
print("Failed to extract a valid frame.")
except Exception as e:
print(f"Error: {e}")