Created
January 16, 2018 22:19
-
-
Save fbatroni/b065fe9631012ef1a9de35561968b434 to your computer and use it in GitHub Desktop.
Example of python implementation of /dev/random
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import argparse | |
import binascii | |
from sys import argv, stdout | |
# default values | |
DEFAULT_ENTROPY = 4096*16 | |
# helper functions | |
# get list of passed command line arguments | |
def getopts(argv): | |
parser = argparse.ArgumentParser(description=' pseudorandom generator') | |
parser.add_argument('--entropy', help="override default entropy of {}".format(DEFAULT_ENTROPY)) | |
opts = parser.parse_args() | |
return opts | |
class DevRandom: | |
def __init__(self, args): | |
self.entropy = args.entropy or DEFAULT_ENTROPY | |
def get_entropy(self): | |
return self.entropy | |
def generate_random(self): | |
#print () | |
stdout.buffer.write(os.urandom(int(self.entropy))) | |
if __name__ == '__main__': | |
myargs = getopts(argv) | |
devRandom = DevRandom(myargs) | |
devRandom.generate_random() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment