-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlsb-randomness-b.py
executable file
·63 lines (38 loc) · 1.11 KB
/
lsb-randomness-b.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
#!/usr/bin/python
# -*- coding: utf8 -*-
"""
SYNOPSIS
DESCRIPTION
EXAMPLES
EXIT STATUS
AUTHOR
luca.mella@studio.unibo.it
LICENSE
Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
VERSION
0.3
"""
import sys
from bitstring import bits
from optparse import OptionParser
def estimateRandomness(infile):
zeroes = 0
ones = 0
char = infile.read(1)
while char != '':
for x in bits(ord(char)):
if x == '1':
ones+=1
else:
zeroes+=1
char = infile.read(1)
return [ones,zeroes]
#-------------------------------------------------------------------------------
# MAIN
#-------------------------------------------------------------------------------
if __name__ == "__main__":
parser = OptionParser("usage: %prog [OPTIONS] ARGS \nBitstring will be picked from STDIN")
(options, args) = parser.parse_args()
[ones,zeroes]=estimateRandomness(sys.stdin)
print "Perc zeroes : %f" % ( zeroes / float(ones+zeroes) )
print "Perc ones : %f" % ( ones / float(ones+zeroes) )