def main(): print("10 random ints seeded with 0") print("----------------------------") rand1 = MT19937(0) for i in range(0, 10): print(rand1.rand()) print("10 random ints seeded with system time") print("----------------------------") rand2 = MT19937(int(time())) for i in range(0, 10): print(rand2.rand())
def break_super_secure_random(simulate=False): result = super_secure_random(simulate) print("RNG output: %d" % result) timestamp = simulated_time if simulate else int(time()) while timestamp != 0: rng = MT19937(timestamp) res = rng.rand() if res == result: return timestamp timestamp -= 1 raise Exception("Your code is bad and you should feel bad")
def crack_pwreset_token(token): ''' find seed for password reset token. throw exception if token wasn't generated by a timestamp-seeded MT19937 PRNG ''' timestamp = int(time()) # check if timestamp is +12 or -12 hours from current time timerange = 12 * 60 * 60 for ts in range(timestamp-timerange, timestamp+timerange): if MT19937(seed=ts).read(len(token)) == token: return ts else: raise Exception("This pw token is probably not generated by a\ timestamp-seeded MT19937 PRNG")
def super_secure_random(simulate=False): global simulated_time print("sleeping for random delay") if simulate: ts = int(time()) + delay() simulated_time = ts else: sleep(delay()) ts = int(time()) print("timestamp (secret seed): %d" % ts) rng = MT19937(ts) print("sleeping for random delay again") if simulate: simulated_time += delay() else: sleep(delay()) return rng.rand()
def main(): target_rng = MT19937(int(time())) reverse_rng(target_rng) reversed_rng = reverse_rng(target_rng) print("Next value of target RNG: %d" % target_rng.rand()) print("Next value of reversed RNG: %d" % reversed_rng.rand())
def reverse_rng(rng): untempered = [untemper(rng.rand()) for i in range(624)] return MT19937(state=untempered)
def get_pwreset_token(): # generate a 16-byte password reset token seeded with current time return MT19937(seed=int(time())).read(16)
def __init__(self, key): self.rng = MT19937(seed=key)
def delay(minsec=40, maxsec=1000): rng = MT19937(int(time())) return rng.rand() % (maxsec - minsec) + minsec