-
Notifications
You must be signed in to change notification settings - Fork 1
/
accumulator.py
50 lines (40 loc) · 1.31 KB
/
accumulator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from hashlib import sha256
from Crypto.Cipher import AES
import math
from generator import Generator
MINPOOLSIZE = 64
class Accumulator(object):
def __init__(self):
self.P = [b''] * 32
self.reseedcnt = 0
self.generator = Generator()
self.last_seed = 0
def randomdata(self, n):
# n: Number of bytes of random data to generate
if len(self.P[0]) >= MINPOOLSIZE and self.last_seed > 100:
self.reseedcnt += 1
s = b''
for i in range(32):
if 2**i % self.reseedcnt == 0:
# XXX: Is this correct?
s += sha256(self.P[i]).digest()
self.P[i] = b''
self.generator.reseed(s)
# XXX: FIX here
# if self.reseedcnt == 0:
# print("Generate error, PRNG not seeded yet")
# raise
return self.generator.pseudo_randomdata(n)
def write_seedfile(self, f):
with open(f, 'wb') as fp:
fp.write(self.randomdata(64))
def update_seedfile(self, f):
s = open(f).read()
assert len(s) == 64
self.generator.reseed(s)
with open(f, 'wb') as fp:
fp.write(self.randomdata(64))
a = Accumulator()
print(a.randomdata((4)))
print(a.randomdata((4)))
print(a.randomdata((4)))