-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.py
67 lines (58 loc) · 2.04 KB
/
generator.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
import boto3
import json
import time
from utils import hashers, reductors
from db import DB
q_name = 'rcha-queue-hackerz'
db = DB('rainbows')
def generate(chain_len, hash_f, reduc_f, alphabet, max_word_len, start_word):
db.set_rainbow_parameters(
chain_len, hash_f, reduc_f, alphabet, max_word_len)
if hash_f not in hashers.available():
raise NameError("Hashing function not found")
hf = getattr(hashers, hash_f)
if reduc_f not in reductors.available():
raise NameError("Reduction function not found")
rf = getattr(reductors, reduc_f)
if len(start_word) > max_word_len:
raise ValueError("Starting word can't be longer than the max_word_len")
alphabet = list(alphabet)
if '' not in alphabet:
alphabet.append('')
password = start_word
for i in xrange(chain_len):
hashed = hf(password)
password = rf(hashed, alphabet, max_word_len, i)
return hashed, start_word
if __name__ == '__main__':
sqs = boto3.resource('sqs')
queue = sqs.get_queue_by_name(QueueName=q_name)
while True:
for msg in queue.receive_messages(
MessageAttributeNames=[
'string',
],
MaxNumberOfMessages=1,
VisibilityTimeout=300,
WaitTimeSeconds=5
):
start_t = time.time()
chains = {}
request = json.loads(msg.body)
for start_word in request['start_words']:
hashed, start_word = generate(
request['chain_len'],
request['hash_f'],
request['reduc_f'],
request['alphabet'],
request['max_word_len'],
start_word
)
chains[hashed] = start_word
db.batch_set(chains)
print "Requested chains: {}, Generated: {} in {}s".format(
len(request['start_words']),
len(chains),
(time.time() - start_t)
)
msg.delete()