Advertisement
Al-Azif

Untitled

Jul 30th, 2018
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.22 KB | None | 0 0
  1. class RandomGenerator:
  2.     # self.DEFAULT_CAPACITY
  3.     DEFAULT_CAPACITY = 48
  4.  
  5.     # self.WORD_SIZE
  6.     WORD_SIZE = 4
  7.  
  8.     # Initialize the object, takes 2 args, "seed" and "capacity", capacity is optional and is set to self.DEFAULT_CAPACITY if not specified
  9.     def __init__(self, seed, capacity=DEFAULT_CAPACITY):
  10.  
  11.         # Set "self.generator" to a Mersenne Twister Algorithm Class (?)
  12.         self.generator = MTRandom()
  13.  
  14.         # Arg input (for "setup_with_array" function) is an array from "seed" of 8 unsigned ints (big-endian)
  15.         self.generator.setup_with_array(struct.unpack('>IIIIIIII', seed))
  16.  
  17.         # Create empty (null) "buffer" variable
  18.         self.buffer = None
  19.  
  20.         # Set "self.capacity" to capacity from the arg
  21.         self.capacity = capacity
  22.  
  23.         # Set "self.count" to 0
  24.         self.count = 0
  25.  
  26.     # Function "fill", which can access this object's functions and variables, takes no args
  27.     def fill(self):
  28.  
  29.         # If "self.count" is not 0
  30.         if self.count != 0:
  31.             # Close Function
  32.             return
  33.  
  34.         # #### Equivalent below in -------------'s
  35.         # self.buffer = ''.join(struct.pack('>I', self.generator.generate_int()) for i in xrange(self.capacity / self.WORD_SIZE))
  36.  
  37.  
  38.         # ---------------------------------------------------------------------
  39.  
  40.         # For loop that runs "self.capacity"/"self.WORD_SIZE" times
  41.         for i in xrange(self.capacity / self.WORD_SIZE):
  42.  
  43.             # Append binary of "self.generator.generate_int()" as unsigned int (big-endian) to "self.buffer"
  44.             self.buffer += struct.pack('>I', self.generator.generate_int())
  45.  
  46.         # ---------------------------------------------------------------------
  47.  
  48.         # Set "self.size" to length of "self.buffer" array (Number of entries)
  49.         self.size = len(self.buffer)
  50.  
  51.         # Set "self.count" to "self.capacity"
  52.         self.count = self.capacity
  53.  
  54.     # Function "generate", which can access this object's functions and variables, takes 1 arg "size"
  55.     def generate(self, size):
  56.  
  57.         # Initialize "output" variable as a blank string
  58.         output = ''
  59.  
  60.         # Using this functions only arg, "size", loop while "size" remains greater than 0
  61.         while size > 0:
  62.  
  63.             # Run "self.fill" function
  64.             self.fill()
  65.  
  66.             # If "size" is greater than "self.count"
  67.             if size > self.count:
  68.  
  69.                 # Append "self.buffer"[from array index("self.size" - "self.count") to array index("self.size")] to "output"
  70.                 output += self.buffer[self.size - self.count:self.size]
  71.  
  72.                 # Subtract "self.count" from "size"
  73.                 size -= self.count
  74.  
  75.                 # Set "self.count" to 0
  76.                 self.count = 0
  77.  
  78.             # If "size" is not greater than "self.count"
  79.             else:
  80.  
  81.                 # Append "self.buffer"[from array index("self.size" - "self.count") to array index("self.size" - "self.count" + "size")] to "output"
  82.                 output += self.buffer[self.size - self.count:self.size - self.count + size]
  83.  
  84.                 # Subtract "size" from "self.count"
  85.                 self.count -= size
  86.  
  87.                 # Set "size" to 0
  88.                 size = 0
  89.  
  90.         # Return "output" variable
  91.         return output
  92.  
  93. # Function "generate_secure", which can access this object's functions and variables, takes 1 arg "size"
  94. def generate_secure(self, size):
  95.  
  96.     # Initialize "output" variable as a blank string
  97.     output = ''
  98.  
  99.     # Using this functions only arg, "size", loop while "size" remains greater than 0
  100.     while size > 0:
  101.  
  102.         # 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"
  103.         buffer = sha256(self.generate(self.DEFAULT_CAPACITY))
  104.  
  105.         # Loop through each char in the "buffer" variable
  106.         for c in buffer:
  107.  
  108.             # If char not null terminator
  109.             if ord(c) != 0:
  110.  
  111.                 # Append char to output string
  112.                 output += c
  113.  
  114.                 # Subtract 1 from "size" variable
  115.                 size -= 1
  116.  
  117.     # Return "output" variable
  118.     return output
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement