initial scripts
This commit is contained in:
47
entropy-rng-cam.py
Normal file
47
entropy-rng-cam.py
Normal 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}")
|
||||
Reference in New Issue
Block a user