Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class RandomGenerator:
- # self.DEFAULT_CAPACITY
- DEFAULT_CAPACITY = 48
- # self.WORD_SIZE
- WORD_SIZE = 4
- # Initialize the object, takes 2 args, "seed" and "capacity", capacity is optional and is set to self.DEFAULT_CAPACITY if not specified
- def __init__(self, seed, capacity=DEFAULT_CAPACITY):
- # Set "self.generator" to a Mersenne Twister Algorithm Class (?)
- self.generator = MTRandom()
- # Arg input (for "setup_with_array" function) is an array from "seed" of 8 unsigned ints (big-endian)
- self.generator.setup_with_array(struct.unpack('>IIIIIIII', seed))
- # Create empty (null) "buffer" variable
- self.buffer = None
- # Set "self.capacity" to capacity from the arg
- self.capacity = capacity
- # Set "self.count" to 0
- self.count = 0
- # Function "fill", which can access this object's functions and variables, takes no args
- def fill(self):
- # If "self.count" is not 0
- if self.count != 0:
- # Close Function
- return
- # #### Equivalent below in -------------'s
- # self.buffer = ''.join(struct.pack('>I', self.generator.generate_int()) for i in xrange(self.capacity / self.WORD_SIZE))
- # ---------------------------------------------------------------------
- # For loop that runs "self.capacity"/"self.WORD_SIZE" times
- for i in xrange(self.capacity / self.WORD_SIZE):
- # Append binary of "self.generator.generate_int()" as unsigned int (big-endian) to "self.buffer"
- self.buffer += struct.pack('>I', self.generator.generate_int())
- # ---------------------------------------------------------------------
- # Set "self.size" to length of "self.buffer" array (Number of entries)
- self.size = len(self.buffer)
- # Set "self.count" to "self.capacity"
- self.count = self.capacity
- # Function "generate", which can access this object's functions and variables, takes 1 arg "size"
- def generate(self, size):
- # Initialize "output" variable as a blank string
- output = ''
- # Using this functions only arg, "size", loop while "size" remains greater than 0
- while size > 0:
- # Run "self.fill" function
- self.fill()
- # If "size" is greater than "self.count"
- if size > self.count:
- # Append "self.buffer"[from array index("self.size" - "self.count") to array index("self.size")] to "output"
- output += self.buffer[self.size - self.count:self.size]
- # Subtract "self.count" from "size"
- size -= self.count
- # Set "self.count" to 0
- self.count = 0
- # If "size" is not greater than "self.count"
- else:
- # Append "self.buffer"[from array index("self.size" - "self.count") to array index("self.size" - "self.count" + "size")] to "output"
- output += self.buffer[self.size - self.count:self.size - self.count + size]
- # Subtract "size" from "self.count"
- self.count -= size
- # Set "size" to 0
- size = 0
- # Return "output" variable
- return output
- # Function "generate_secure", which can access this object's functions and variables, takes 1 arg "size"
- def generate_secure(self, size):
- # Initialize "output" variable as a blank string
- output = ''
- # Using this functions only arg, "size", loop while "size" remains greater than 0
- while size > 0:
- # Generate sha256 hash based on output from the "self.generate" function, using "self.DEFAULT_CAPACITY" as the arg, and store it in a new "buffer" variable. This is not "self.buffer"
- buffer = sha256(self.generate(self.DEFAULT_CAPACITY))
- # Loop through each char in the "buffer" variable
- for c in buffer:
- # If char not null terminator
- if ord(c) != 0:
- # Append char to output string
- output += c
- # Subtract 1 from "size" variable
- size -= 1
- # Return "output" variable
- return output
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement