|
| 1 | +#!/usr/bin/env python2 |
| 2 | + |
| 3 | +""" |
| 4 | +Copyright (c) 2006-2019 sqlmap developers (http://sqlmap.org/) |
| 5 | +See the file 'LICENSE' for copying permission |
| 6 | +""" |
| 7 | + |
| 8 | +import binascii |
| 9 | +import os |
| 10 | +import random |
| 11 | + |
| 12 | +class WichmannHill(random.Random): |
| 13 | + """ |
| 14 | + Reference: https://svn.python.org/projects/python/trunk/Lib/random.py |
| 15 | + """ |
| 16 | + |
| 17 | + VERSION = 1 # used by getstate/setstate |
| 18 | + |
| 19 | + def seed(self, a=None): |
| 20 | + """Initialize internal state from hashable object. |
| 21 | +
|
| 22 | + None or no argument seeds from current time or from an operating |
| 23 | + system specific randomness source if available. |
| 24 | +
|
| 25 | + If a is not None or an int or long, hash(a) is used instead. |
| 26 | +
|
| 27 | + If a is an int or long, a is used directly. Distinct values between |
| 28 | + 0 and 27814431486575L inclusive are guaranteed to yield distinct |
| 29 | + internal states (this guarantee is specific to the default |
| 30 | + Wichmann-Hill generator). |
| 31 | + """ |
| 32 | + |
| 33 | + if a is None: |
| 34 | + try: |
| 35 | + a = int(binascii.hexlify(os.urandom(16)), 16) |
| 36 | + except NotImplementedError: |
| 37 | + import time |
| 38 | + a = int(time.time() * 256) # use fractional seconds |
| 39 | + |
| 40 | + if not isinstance(a, int): |
| 41 | + a = hash(a) |
| 42 | + |
| 43 | + a, x = divmod(a, 30268) |
| 44 | + a, y = divmod(a, 30306) |
| 45 | + a, z = divmod(a, 30322) |
| 46 | + self._seed = int(x)+1, int(y)+1, int(z)+1 |
| 47 | + |
| 48 | + self.gauss_next = None |
| 49 | + |
| 50 | + def random(self): |
| 51 | + """Get the next random number in the range [0.0, 1.0).""" |
| 52 | + |
| 53 | + # Wichman-Hill random number generator. |
| 54 | + # |
| 55 | + # Wichmann, B. A. & Hill, I. D. (1982) |
| 56 | + # Algorithm AS 183: |
| 57 | + # An efficient and portable pseudo-random number generator |
| 58 | + # Applied Statistics 31 (1982) 188-190 |
| 59 | + # |
| 60 | + # see also: |
| 61 | + # Correction to Algorithm AS 183 |
| 62 | + # Applied Statistics 33 (1984) 123 |
| 63 | + # |
| 64 | + # McLeod, A. I. (1985) |
| 65 | + # A remark on Algorithm AS 183 |
| 66 | + # Applied Statistics 34 (1985),198-200 |
| 67 | + |
| 68 | + # This part is thread-unsafe: |
| 69 | + # BEGIN CRITICAL SECTION |
| 70 | + x, y, z = self._seed |
| 71 | + x = (171 * x) % 30269 |
| 72 | + y = (172 * y) % 30307 |
| 73 | + z = (170 * z) % 30323 |
| 74 | + self._seed = x, y, z |
| 75 | + # END CRITICAL SECTION |
| 76 | + |
| 77 | + # Note: on a platform using IEEE-754 double arithmetic, this can |
| 78 | + # never return 0.0 (asserted by Tim; proof too long for a comment). |
| 79 | + return (x/30269.0 + y/30307.0 + z/30323.0) % 1.0 |
| 80 | + |
| 81 | + def getstate(self): |
| 82 | + """Return internal state; can be passed to setstate() later.""" |
| 83 | + return self.VERSION, self._seed, self.gauss_next |
| 84 | + |
| 85 | + def setstate(self, state): |
| 86 | + """Restore internal state from object returned by getstate().""" |
| 87 | + version = state[0] |
| 88 | + if version == 1: |
| 89 | + version, self._seed, self.gauss_next = state |
| 90 | + else: |
| 91 | + raise ValueError("state with version %s passed to " |
| 92 | + "Random.setstate() of version %s" % |
| 93 | + (version, self.VERSION)) |
| 94 | + |
| 95 | + def jumpahead(self, n): |
| 96 | + """Act as if n calls to random() were made, but quickly. |
| 97 | +
|
| 98 | + n is an int, greater than or equal to 0. |
| 99 | +
|
| 100 | + Example use: If you have 2 threads and know that each will |
| 101 | + consume no more than a million random numbers, create two Random |
| 102 | + objects r1 and r2, then do |
| 103 | + r2.setstate(r1.getstate()) |
| 104 | + r2.jumpahead(1000000) |
| 105 | + Then r1 and r2 will use guaranteed-disjoint segments of the full |
| 106 | + period. |
| 107 | + """ |
| 108 | + |
| 109 | + if not n >= 0: |
| 110 | + raise ValueError("n must be >= 0") |
| 111 | + x, y, z = self._seed |
| 112 | + x = int(x * pow(171, n, 30269)) % 30269 |
| 113 | + y = int(y * pow(172, n, 30307)) % 30307 |
| 114 | + z = int(z * pow(170, n, 30323)) % 30323 |
| 115 | + self._seed = x, y, z |
| 116 | + |
| 117 | + def __whseed(self, x=0, y=0, z=0): |
| 118 | + """Set the Wichmann-Hill seed from (x, y, z). |
| 119 | +
|
| 120 | + These must be integers in the range [0, 256). |
| 121 | + """ |
| 122 | + |
| 123 | + if not type(x) == type(y) == type(z) == int: |
| 124 | + raise TypeError('seeds must be integers') |
| 125 | + if not (0 <= x < 256 and 0 <= y < 256 and 0 <= z < 256): |
| 126 | + raise ValueError('seeds must be in range(0, 256)') |
| 127 | + if 0 == x == y == z: |
| 128 | + # Initialize from current time |
| 129 | + import time |
| 130 | + t = int(time.time() * 256) |
| 131 | + t = int((t&0xffffff) ^ (t>>24)) |
| 132 | + t, x = divmod(t, 256) |
| 133 | + t, y = divmod(t, 256) |
| 134 | + t, z = divmod(t, 256) |
| 135 | + # Zero is a poor seed, so substitute 1 |
| 136 | + self._seed = (x or 1, y or 1, z or 1) |
| 137 | + |
| 138 | + self.gauss_next = None |
| 139 | + |
| 140 | + def whseed(self, a=None): |
| 141 | + """Seed from hashable object's hash code. |
| 142 | +
|
| 143 | + None or no argument seeds from current time. It is not guaranteed |
| 144 | + that objects with distinct hash codes lead to distinct internal |
| 145 | + states. |
| 146 | +
|
| 147 | + This is obsolete, provided for compatibility with the seed routine |
| 148 | + used prior to Python 2.1. Use the .seed() method instead. |
| 149 | + """ |
| 150 | + |
| 151 | + if a is None: |
| 152 | + self.__whseed() |
| 153 | + return |
| 154 | + a = hash(a) |
| 155 | + a, x = divmod(a, 256) |
| 156 | + a, y = divmod(a, 256) |
| 157 | + a, z = divmod(a, 256) |
| 158 | + x = (x + a) % 256 or 1 |
| 159 | + y = (y + a) % 256 or 1 |
| 160 | + z = (z + a) % 256 or 1 |
| 161 | + self.__whseed(x, y, z) |
0 commit comments