Skip to content

Instantly share code, notes, and snippets.

@andrew-stclair
Last active August 10, 2023 06:46
Show Gist options
  • Save andrew-stclair/b4fe73bada18764b36dd0e3797f738a8 to your computer and use it in GitHub Desktop.
Save andrew-stclair/b4fe73bada18764b36dd0e3797f738a8 to your computer and use it in GitHub Desktop.
WIP Python Script to check the random devices
"""Randomness Validator"""
# How many bytes to read
BYTES_READ = 50000000
# Devices to check
DEVICES = [
"/dev/random",
"/dev/urandom"
]
# Cool code stuff
def test_offset(device: str):
"""Function to test a random number generator device's bit likelihood"""
rand = ''
count_1 = 0
count_0 = 0
with open(device, 'rb') as file:
rand = file.read(BYTES_READ)
for byte in rand:
byte = f"{bin(byte)[2:]:>08}" # Convert byte into 8 character string representing bits
for bit in byte:
if bit == "1":
count_1 += 1
else:
count_0 += 1
total = count_0 + count_1
return(count_1 / total,count_0 / total)
def main():
"""Main function"""
for device in DEVICES:
offset_1, _ = test_offset(device)
print(device, f"{offset_1*100:.6f}%")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment