-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
61 lines (44 loc) · 1.88 KB
/
utils.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
import os
import time
from decimal import Decimal
import conversor
import rockyou
class Estimator:
def __init__(self, args: list) -> None:
self.path = args.path
self.chars = args.character_set
self.max = args.max_length
self.min = args.min_length
self.separator = args.separator
self.prefix = args.prefix
self.suffix = args.suffix
def estimate(self) -> str:
return f'Total size: {self.estimate_size()}\nTotal time (rough estimate): {self.estimate_time()}\nTotal ammount: {self.estimate_ammount()}'
def estimate_size(self) -> str:
# Couting separator characters.
# Removing 1 because the last combination won't have a leading separator character.
n = self.estimate_ammount() - 1
_len = len(self.chars)
for i in range(self.min, self.max + 1):
n += _len ** i * (i + len(self.prefix + self.suffix))
return conversor.to_readable_size(Decimal(n))
def estimate_time(self) -> str:
n = self.estimate_ammount(1, 1)
path = f'{self.path}/temp.txt'
start = time.time()
with open(path, 'wb+') as f:
[f.write(bytes(join(c, self.separator, self.prefix, self.suffix), 'utf-8'))
for c in rockyou.rockyou(1, 1, self.chars)]
f.seek(-1, os.SEEK_END)
f.truncate()
end = time.time()
os.remove(path)
return conversor.to_readable_time(Decimal(end - start) * Decimal(self.estimate_ammount()) / Decimal(n))
def estimate_ammount(self, min: int = None, max: int = None) -> int:
n = 0
_len = len(self.chars)
for i in range(min or self.min, (max or self.max) + 1):
n += _len ** i
return n
def join(combination: tuple, separator: str, prefix: str, suffix: str) -> str:
return '{}{}'.format(f"{prefix}{''.join(combination)}{suffix}", separator)