Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/pytrng/DataCollector.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from bitarray import bitarray
from functools import wraps
from typing import Callable
import os
import time
import psutil
Expand All @@ -11,6 +13,11 @@
print(e)
print("WARNING: Skip mouse input.")

def require_gui(func: Callable) -> Callable:
"""Returns None if `gui=False`."""
@wraps(func)
def wrapper(*args, **kwargs) -> Any:
return func(*args, **kwargs) if gui else None

class DataCollector:
"""The DataCollector collects input data and hashes it."""
Expand All @@ -30,6 +37,7 @@ def __init__(self, bit_length: int):
self.bit_length = bit_length
self.byte_length = bit_length // 8

@require_gui
def get_mouse_position(self) -> bytes | None:
"""Reads the current mouse x and y position and multiplies them

Expand All @@ -41,14 +49,12 @@ def get_mouse_position(self) -> bytes | None:
if pynput couldn't been imported
"""

if not gui:
return

mouse = Controller()
x = mouse.position[0] % ((2 ^ self.bit_length) - 1)
y = mouse.position[1] % ((2 ^ self.bit_length) - 1)
return (y * x).to_bytes(length=self.byte_length, byteorder="big")

@require_gui
def get_mouse_pos_pool(self, strength=5) -> bytes | None:
"""Calls the get_mouse_position function multiple times and links their outputs using XOR.

Expand All @@ -65,9 +71,6 @@ def get_mouse_pos_pool(self, strength=5) -> bytes | None:
if pynput couldn't been imported
"""

if not gui:
return

mouse_pos = bitarray()
for i in range(0, strength):
pos1 = bitarray()
Expand Down