-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseeker.py
74 lines (63 loc) · 2.27 KB
/
seeker.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
import boto3
import json
from utils import hashers, reductors
from db import DB, TaskDB
q_name = 'rcha-queue-seekerz'
db = DB('rainbows')
tasks_db = TaskDB('tasks')
def check(start_word, hash_target, chain_len,
hash_f, reduc_f, alphabet, max_word_len):
password = start_word
for i in xrange(chain_len):
hashed = hash_f(password)
if hashed == hash_target:
return password
password = reduc_f(hashed, alphabet, max_word_len, i)
return None
def seek(hash_target, chain_len, hash_f, reduc_f, alphabet, max_word_len):
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")
hash_f = getattr(hashers, hash_f)
if reduc_f not in reductors.available():
raise NameError("Reduction function not found")
reduc_f = getattr(reductors, reduc_f)
alphabet = list(alphabet)
if '' not in alphabet:
alphabet.append('')
false_alarms = 0
for i in xrange(chain_len, 0, -1):
hashed = hash_target
for j in xrange(i - 1, chain_len - 1):
password = reduc_f(hashed, alphabet, max_word_len, j)
hashed = hash_f(password)
if db.exists(hashed):
start_word = db.get(hashed)
res = check(start_word, hash_target, chain_len,
hash_f, reduc_f, alphabet, max_word_len)
if res is not None:
return res, false_alarms
else:
false_alarms += 1
return None, false_alarms
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=100,
WaitTimeSeconds=5
):
request = json.loads(msg.body)
tid = request.pop('tid', None)
print 'Got task with id = {}'.format(tid)
tasks_db.set(tid, 'executing')
res = seek(**request)
print 'Done with result: {}'.format(res)
tasks_db.set(tid, 'done', res[0])
msg.delete()