-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrandtest.py
executable file
·80 lines (65 loc) · 1.4 KB
/
randtest.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/python
'''
given a stream of ascii integers on stdin, test for entropy and
generate a bitmap for visual pattern recognition
# test rolling hashes
dd if=/dev/urandom bs=1k count=320 | ./nieve1.py | ./randtest.py
# test chunk sizes
dd if=/dev/urandom bs=9k count=320000 | ./cdc1.py | ./randtest.py
'''
import Image
import numpy as np
import os
import struct
import sys
fn = "/tmp/image.png"
def save(image):
img = Image.fromarray(image)
img.save(fn)
def show(image):
save(image)
os.system('xzgv -z %s' % fn)
def main():
lst = read()
img = lst2img(lst)
buf = lst2buf(lst)
ent(buf)
show(img)
def read():
# accepts one integer per line
lst = []
for line in sys.stdin.readlines():
lst.append(int(line))
# truncate the end to make it a square root
sqrt = int(len(lst)**.5)
sqr = sqrt**2
print 'truncating', len(lst) - sqr, 'bytes'
lst = lst[:sqr]
return lst
def lst2img(lst):
image_size = int(len(lst)**.5)
assert image_size == len(lst)**.5
img = np.array(lst)
img = img.reshape((image_size, image_size))
return img
def lst2buf(lst):
lst = lst[:]
buf = ''
while len(lst):
v = lst.pop(0)
try:
packed = struct.pack('=H', v)
except:
print repr(v)
raise
buf += packed
return buf
def ent(buf):
'''
Depends on John Walker's entropy testing package 'ent':
http://www.fourmilab.ch/random/
'''
entin = os.popen('random/ent -t', 'w')
entin.write(buf)
entin.close()
main()