Create a Python Module named piphash_rainbow.py that Create a rainbow table named sixdigithash_rainbow.csv that stores all possible six-digit pins and their corresponding md5, sha1, sha256, and sha512 hashes.
lettersLower = 'abcdefghijklmnopqrstuvwxyz' | |
lettersUpper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | |
numbers = "0123456789" | |
allChars = lettersLower + lettersUpper + numbers | |
from hashlib import md5, sha1,sha256,sha512 | |
def generatePasswords(nbChar, chars=lettersLower): | |
"""Generates a list of all the possible passwords given certain parameters. | |
nbChar: Number of characters in the password. | |
chars: List of covered characters. | |
Note: only for testing purposes. | |
""" | |
results = [] | |
nbDifferentChars = len(chars) | |
for i in range(nbDifferentChars**nbChar): | |
word = "" | |
for j in range(nbChar): | |
word = chars[i % nbDifferentChars] + word | |
i //= nbDifferentChars | |
results.append(word) | |
return results | |
class RainbowTable: | |
"""Rainbow table: Structure to crack hashed passwords. | |
""" | |
# Dictionary for finding hash functions by their name. | |
hashFunctions = {'': None, | |
sha1.__name__ : sha1, | |
'sha1' : sha1, | |
md5.__name__ : md5, | |
'md5' : md5, sha256.__name__:sha256, 'sha256':sha256, sha512.__name__:sha512, 'sha512':sha512} |
|
def __init__(self, columns=0, chars="", pwdLength=0, func='', rows=1000): | |
"""Initializes the rainbow table. | |
columns: Length of a chain, i.e. number of times the password at the start of the chain is hashed and reduced. | |
chars: List of characters covered. | |
pwdLength: Length of the passwords. | |
func: Name of the hashing function. | |
rows: Number of chains. | |
""" | |
from RB import RBTree, rbnode | |
self.table = RBTree() | |
if columns > 0: | |
self.columns = columns | |
self.chars = chars | |
self.pwdLength = pwdLength | |
self.func = RainbowTable.hashFunctions[func] | |
for i in range(rows): | |
pwd = self.randomPassword() | |
hashV = self.createChain(pwd) | |
self.table.insert(hashV, pwd) | |
def __repr__(self): | |
"""Prints the content of the table. | |
""" | |
return repr(self.table._root) | |
def writeToFile(self, output): | |
"""Writes rainbow table into a file, so that it can be recovered at a different time later. | |
output: Name of the file to write to. | |
""" | |
f = open(output, 'w') | |
data = [self.columns, self.chars, self.pwdLength, self.func.__name__] | |
data = [str(x) for x in data] | |
f.write(" ".join(data)) | |
f.write("\n") | |
f.write(repr(self)) | |
f.close() | |
def readFromFile(self, input): | |
"""Read a rainbow table from a file. | |
input: Name of the file to read from. | |
""" | |
f = open(input, "r") | |
line = f.readline() | |
line = line.strip().split(sep=" ", maxsplit=3) | |
self.columns, self.chars, self.pwdLength, self.func = line | |
self.columns = int(self.columns) | |
self.pwdLength = int(self.pwdLength) | |
self.func = RainbowTable.hashFunctions[self.func] | |
line = f.readline() | |
while line != '': | |
pwd, hashV = line.strip().split(sep=" ", maxsplit=1) | |
self.table.insert(hashV, pwd) | |
line = f.readline() | |
f.close() | |
def _find(self, hashV): | |
"""Find the passwords in the table corresponding to the given hash. | |
hashV: Hash to find. | |
Returns a list of corresponding starting passwords. | |
""" | |
return self.table.search(hashV) | |
def hashWord(self, word): | |
"""Hash a word. | |
word: Word to hash. | |
Returns the hash of the word. | |
""" | |
word = word.encode('utf-8') | |
return self.func(word).hexdigest() | |
def reduce(self, hashV, column): | |
"""Reduces a hash. | |
hashV: Hash to reduce. | |
column: Column to hash at. | |
Returns a valid password. | |
""" | |
results = [] | |
# Cast hash from str to int then decompose into bytes | |
byteArray = self.getBytes(hashV) | |
for i in range(self.pwdLength): | |
index = byteArray[(i + column) % len(byteArray)] | |
newChar = self.chars[index % len(self.chars)] | |
results.append(newChar) | |
return "".join(results) | |
def getBytes(self, hashV): | |
"""Transforms a hash into a list of bytes. | |
hashV: Hash to transform into bytes. | |
Returns a list of bytes. | |
""" | |
results = [] | |
remaining = int(hashV, 16) | |
while remaining > 0: | |
results.append(remaining % 256) | |
remaining //= 256 | |
return results | |
def createChain(self, pwd): | |
"""Creates a chain. | |
pwd: Password to start the chain with. | |
Returns the hash at the end of the chain. | |
""" | |
for col in range(self.columns): | |
hashV = self.hashWord(pwd) | |
pwd = self.reduce(hashV, col) | |
return hashV | |
def randomPassword(self): | |
"""Generates a random password. | |
Returns the generated password. | |
""" | |
from random import randrange | |
pwd = "" | |
charsLength = len(self.chars) | |
for i in range(self.pwdLength): | |
pwd += self.chars[randrange(charsLength)] | |
return pwd | |
def crackHash(self, startHash): | |
"""Tries to crack a hash. | |
startHash: Hash to crack. | |
Returns the resulting password, if one is found, '' otherwise. | |
""" | |
for col in range(self.columns, -1, -1): | |
hashV = self._getFinalHash(startHash, col) | |
pwdList = self._find(hashV) | |
for pwd in pwdList: | |
resPwd = self._findHashInChain(pwd, startHash) | |
if resPwd != None: | |
return resPwd | |
return '' | |
def _getFinalHash(self, startHash, startCol): | |
"""Returns the hash at the end of a chain, starting from a hash and at a given column. | |
startHash: Hash to start with. | |
startCol: Column to start from. | |
Returns the hash at the end of a chain. | |
""" | |
hashV = startHash | |
for col in range(startCol, self.columns-1): | |
pwd = self.reduce(hashV, col) | |
hashV = self.hashWord(pwd) | |
return hashV | |
def _findHashInChain(self, startPwd, startHash): | |
"""Tries to find a hash in a chain. | |
startPwd: Password at the beginning of a chain. | |
startHash: Hash to find. | |
Returns the corresponding password if one is found, None otherwise. | |
""" | |
hashV = self.hashWord(startPwd) | |
if hashV == startHash: | |
return startPwd | |
col = 0 | |
# hash and reduce until the password has been found or the end of the chain has been reached. | |
while col < self.columns: | |
pwd = self.reduce(hashV, col) | |
hashV = self.hashWord(pwd) | |
if hashV == startHash: | |
# If the password has been, return it | |
return pwd | |
col += 1 | |
# The password hasn't been found. | |
return None | |
def allPasswords(self): | |
"""Returns the list of all password this table could and should cover. | |
Note: only for testing purposes. | |
""" | |
res = [] | |
length = len(self.chars) | |
for i in range(length**self.pwdLength): | |
pwd = "" | |
for j in range(self.pwdLength): | |
pwd += self.chars[i % length] | |
i //= length | |
res.append(pwd) | |
return res | |
def testWord(self, word): | |
"""Tries to find a password from its hash. | |
word: Word to find. | |
Return the result of trying to crack the password of the hash. | |
Note: only for testing purposes. | |
""" | |
return self.crackHash(self.hashWord(word)) | |
def testWords(self, words): | |
"""Tests a list of words from their hash. | |
words: Lists of words to find. | |
Note: only for testing purposes. | |
""" | |
for word in words: | |
print(word, self.crackHash(rain.hashWord(word))) | |
from testRainbow import * | |
#rain = RainbowTable() ; rain.readFromFile("D:/Coding/Python/RainbowTable/sixdigithash_rainbow.csv") | |
#rain = RainbowTable(100, lettersLower, 3, 'md5') |
Create a Python Module named piphash_rainbow.py that Create a rainbow table named sixdigithash_rainbow.csv that stores all...
Use Python: Dice Rolls Create a function named build_roll_permutations which returns all 36 permutations of rolling two six sided dice. That is, each dice has a number 1 through 6 on one of its sides. The return value is a list which contains tuples. Each tuple represents a possible dice roll of two dice. Card Deck Create a function named build_deck that returns a full deck of cards. The cards are created by a rank and a suit (e.g. 2♡)....
"PYTHON" 1. a. Create a file (module) named camera_picture.py. b. Write a function, take_picture_effect, that takes two parameters, the filename and an effect. c. Take a picture with the filename and effect. Use camera.effect to set the effect. Use help(PiCamera) to find the effects which you can apply. d. Create a second file use_camera.py. e. Import the take_picture_effect function from the camera_picture module. f. Prompt the user for the filename. g. Prompt the user for the image_effect. help(PiCamera) to see...
1. Create a table that is named test_table. This table should have two columns named test_id and test_description. These columns should be defined to store the following type of data, respectively: test_id stores numeric data that is a maximum of 3 characters in size; test_description stores variable character data that is a maximum of 25 characters in size.(Hint: Refer to SQL Example 2.1) 2. Insert two rows into the test_table as follows: INSERT INTO test_table VALUES (1, 'Test1'); INSERT INTO...
Create a python code named LetterCount with a text file named words.txt that contains at least 100 words in any format - some words on the same line, some alone (this program involves no writing so you should not be erasing this file). Then create a program in the main.py that prompts the user for a file name then iterates through all the letters in words.txt, counting the frequency of each letter, and then printing those numbers at the end...
Python Question
Task 4
Create a function named poker_table that uses
Turtle Graphics to create a single poker table to accommodate all
players. There should be four parameters:
num_players indicates the number of
players; side_length indicates the length
in pixels of each side of the table; and
x and y, which
give the location where you should start drawing the table.
The poker table should be a simple regular polygon (that is,
with sides of equal length). The number of...
Python Language Create a module (1 mark) a) Create a function that takes three (3) arguments (1 mark) i) Name it whatever you’d like (1 mark) ii) It will return a dictionary with the following keys (1 mark) (1) Error => gives error message or empty string if not (1 mark) (2) Result => gives result or empty string if no result (1 mark) b) Ensure that all three arguments are of String (str) datatype (1 mark) i) Return an...
FOR JAVA: Summary: Create a program that stores info on textbooks. The solution should be named TextBookSort.java. Include these steps: Create a class titled TextBook that contains fields for the author, title, page count, ISBN, and price. This TextBook class will also provide setter and getter methods for all fields. Save this class in a file titled TextBook.java. Create a class titled TextBookSort with an array that holds 5 instances of the TextBook class, filled without prompting the user for...
Paperweight using Python 3 Create a dictionary named weight, initialised with the following values: "pencil": 10 "pen": 20 "paper": 4 "eraser": 80 Create another dictionary named available, initialised with the following values: "pen": 3 "pencil": 5 "eraser": 2 "paper": 10 If the weight dictionary gives the weight of an individual item of each type, and the available dictionary specifies the number of each item that is available, write code that determines the total weight of all available items (i.e. what...
Create two tuples named months1 and months2, including first six months and last six months, respectively. Create a new tuple from concatenation of the two tuples created previously. Print the tuple including all months. Print the name of the 6th month using tuple index. Query the index of the one of months. (ex.‘November’) python IDL #WORKLAB4.3 month1=('January','February','March','April','May','June') month2=('July','August','September','October','November','December') allMonths = 'months1'+ 'months2' print('allMonths') print(allMonths[6]) print(allMonths.index('November'))
Create a Python file (book.py) that has a class named Book. Book class has four attributes, title (string), author(string), isbn(string), and year(int). Title - the name of the book. Author - the name of the persons who wrote the book. ISBN - is a unique 13 digit commercial book identifier. Year - the year the title was printed. Your class should have the following methods: (1) __init__(self, title, author, isbn, year): This method called when an object (book) is created...