-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpick
More file actions
executable file
·99 lines (89 loc) · 3.07 KB
/
pick
File metadata and controls
executable file
·99 lines (89 loc) · 3.07 KB
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
91
92
93
94
95
96
97
98
99
#! /usr/bin/env python3
'''
Use -h for help
'''
import argparse
import hashlib
from math import log
# Argument parsing
parser = argparse.ArgumentParser(description='Do RFC3797 randomization')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-s', '--seedfile',
help='File containing the seeds',
metavar='seeds', type=open, required=False)
group.add_argument('-t', '--seedtext',
help='Text string to use as seed',
metavar='seedtext', type=ascii, required=False)
parser.add_argument('-n', '--names',
help='File containing the list of names',
metavar='names', type=open, required=True)
parser.add_argument('-c', '--count',
help='Number of entries to print',
default=12, metavar='N', type=int)
parser.add_argument('-d', '--debug',
action='store_true', help='Print some debugging information')
parser.add_argument('-x', '--extra',
required=False, help='An additional seed for the input')
args = parser.parse_args()
# How many bits of entropy needed to select N from P
# P!
# log ( ----------------- )
# 2 N! * (P-N)!
def entropy_needed(P, N):
# Not picking anything, or picking everyone?
if N < 1 or N >= P: return 0
result = 0.0
for i in range(P, P - N, -1):
result += log(i, 2)
for i in range(N, 1, -1):
result -= log(i, 2)
return int(result) + 0.5
# Read the names, print the count
names = [ line.strip() for line in args.names if line.strip() != "" ]
numnames = len(names)
if args.debug: print('Read', numnames, 'names')
if numnames > 0xFFFF:
raise SystemError("Too many names (limit is 0xFFFF)")
if args.debug:
print('Need approximately',
entropy_needed(numnames, args.count),
'bits of entropy');
# We could directly shrink the names list (such as by
# using `names.pop(k)`) but then the indices would vary.
selected = [ i + 1 for i in range(0, numnames) ]
# Read the seeds or get the seed text.
keystring = ''
if args.seedfile:
seeds = []
for line in args.seedfile:
if line.strip() == "" or line.strip()[0:1] == '#': continue
values = [ int(x) for x in line.split() ]
values.sort()
seeds.append(values)
# Generate the key string and convert to bytes
for s in seeds:
keystring += '.'.join([ str(n) for n in s ])
keystring += './'
else:
keystring = args.seedtext
if args.extra is not None: keystring += args.extra + './'
if args.debug: print('Key string is', keystring, '\n')
keybytes = keystring.encode()
# Do the work
prefix = 0
div = numnames
print(' index hex value of MD5 div selected')
for i in range(0, args.count):
bracket = prefix.to_bytes(2, 'big')
result = hashlib.md5(bracket + keybytes + bracket).hexdigest().upper()
k = int(result, 16) % div
for j in range(0, numnames):
if selected[j]:
k -= 1
if k < 0: break
assert k < 0
print(' %4d %s %2d -> %2d <- %s' % \
(i + 1, result, div, selected[j], names[j]))
selected[j] = 0
div -= 1
prefix += 1