forked from Chavjoh/DictionaryGeneratorPython
/
DictionaryGenerator.py
165 lines (131 loc) · 6.57 KB
/
DictionaryGenerator.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/python
# -*- coding: utf-8 -*-
#------------------------------------------------------------------------------#
# Security - Dictionary Generator #
# ============================================================================ #
# Developer: Chavaillaz Johan #
# Filename: DictionaryGenerator.py #
# Version: 1.0 #
# #
# Licensed to the Apache Software Foundation (ASF) under one #
# or more contributor license agreements. See the NOTICE file #
# distributed with this work for additional information #
# regarding copyright ownership. The ASF licenses this file #
# to you under the Apache License, Version 2.0 (the #
# "License"); you may not use this file except in compliance #
# with the License. You may obtain a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, #
# software distributed under the License is distributed on an #
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY #
# KIND, either express or implied. See the License for the #
# specific language governing permissions and limitations #
# under the License. #
# #
#------------------------------------------------------------------------------#
#------------------------------------------------------------------------------#
# #
# LIBRARIES IMPORT #
# #
#------------------------------------------------------------------------------#
import sys
import argparse
import math
#------------------------------------------------------------------------------#
# #
# UTILITIES FUNCTIONS #
# #
#------------------------------------------------------------------------------#
def updateProgress(percent, barLength = 20):
"""Update progress bar in command line"""
hashes = '#' * int(round(percent * barLength / 100 ))
spaces = ' ' * (barLength - len(hashes))
sys.stdout.write("\rGenerating ... [{0}] {1}%".format(hashes + spaces, int(round(percent))))
sys.stdout.flush()
def sizeDictionary(alphabetSize, minSize, maxSize):
"""Calculate dictionary total size with indicated parameters"""
sum = 0
for currentSize in range(minSize, maxSize+1):
sum += pow(alphabetSize, currentSize)
return sum
def generateDictionary(alphabet, minSize, maxSize):
"""Generate dictionary with indicated parameters"""
alphabetSize = len(alphabet);
currentAlphabetIndex = {}
# Needed to update progress bar
totalGeneration = sizeDictionary(alphabetSize, minSize, maxSize)
currentGeneration = 0
# Open or create database file
with open('dictionary.txt', 'w+') as file:
# Foreach size needed
for currentSize in range(minSize, maxSize+1):
# Current possibility
currentIndex = 0
# Calculate the number of possibilities
currentIndexMax = pow(alphabetSize, currentSize)
# While until the number of possibilities is reached
while (currentIndex < currentIndexMax):
word = ""
# Current position in current size to be generated
for currentPosition in range(0, currentSize):
# Initialization of alphabet index for each position
currentAlphabetIndex[currentPosition] = 0
# Calculate reverse position to calculate indexValue
# Reverse because we begin to change on the right of the word
reversePosition = currentSize - 1 - currentPosition
# Calculate the index of the current character to use in alphabet
indexValue = math.floor((currentIndex / pow(alphabetSize, reversePosition))) % alphabetSize
# Add the character of the alphabet to the word
word += alphabet[indexValue]
file.write(word + '\n')
currentGeneration += 1
updateProgress((currentGeneration / totalGeneration) * 100)
currentIndex += 1
#------------------------------------------------------------------------------#
# #
# CLASSES #
# #
#------------------------------------------------------------------------------#
#------------------------------------------------------------------------------#
# #
# "MAIN" FUNCTION #
# #
#------------------------------------------------------------------------------#
# If this is the main module, run this
if __name__ == '__main__':
argsCount = len(sys.argv)
# Create argument parser to help user
parser = argparse.ArgumentParser(
description='Dictionary generator.'
)
parser.add_argument(
'alphabet',
type=str,
help='Alphabet to use to generate all dictionary words.'
)
parser.add_argument(
'minSize',
type=int,
help='Minimum size of the generated words.'
)
parser.add_argument(
'maxSize',
type=int,
help='Maximum size of the generator words.'
)
# Show help if one of the arguments is missing
if argsCount != 4:
parser.print_help()
sys.exit()
# Get configuration
alphabet = sys.argv[1]
minSize = int(sys.argv[2])
maxSize = int(sys.argv[3])
if minSize > maxSize:
print("Minimum size can't be superior to maximum size.")
elif (minSize < 1) or (maxSize < 1):
print("Minimum and maximum size must be greater than 0.")
else:
generateDictionary(alphabet, minSize, maxSize)