forked from benadida/scantegrity-audit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.py
90 lines (70 loc) · 2.01 KB
/
base.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
81
82
83
84
85
86
87
88
89
90
"""
The core of every meeting verification
not called directly
data path should NOT have a trailing slash
"""
import sys, hashlib
from xml.etree import ElementTree
if len(sys.argv) > 1:
DATA_PATH = sys.argv[1]
else:
DATA_PATH = "testdata"
FINGERPRINTS = []
def add_fingerprint(filename, hash_value):
FINGERPRINTS.append([filename, hash_value])
def fingerprint_report():
report = ""
for filename, fingerprint in FINGERPRINTS:
report += filename + ": " + fingerprint + "\n"
return report
##
## loading files and adding fingerprints
##
def file_in_dir(dir, file, filename, xml = True, correct_windows= False):
path = dir + "/" + file
try:
f = open(path, "r")
except:
print "could not find file %s" % path
sys.exit(1)
contents = f.read()
f.close()
# must do windows style verification loading of newlines
if correct_windows and contents.find('\r') == -1:
print "fixing windows"
contents = contents.replace('\n','\r\n')
add_fingerprint(filename, hashlib.sha1(contents).hexdigest())
if xml:
return ElementTree.fromstring(contents)
else:
return contents
##
## Pseudorandom Number Generation
##
# reverse-engineered from
# https://scantegrity.org/svn/data/takoma-nov3-2009/PUBLIC/PUBLIC/pre_election_audit.py
def prng(seed,index,modulus):
"""
Generate a random integer modulo the modulus, given a seed and an index
"""
# concatenate seed and index
hash_input = "%s%d"%(seed,index)
# get the SHA1 hash in hex, convert to int
hash_int= int(hashlib.sha1(hash_input).hexdigest(), 16)
# modulo the modulus
return hash_int % modulus
def generate_random_int_list(seed, modulus, num_ints):
"""
generate a random list of num_ints integers modulo modulus, with the given seed.
"""
output_list = []
counter = 0
while True:
new_index = prng(seed, counter, modulus)
counter += 1
if new_index in output_list:
continue
output_list.append(new_index)
if len(output_list) == num_ints:
break
return output_list