I am trying to write a simple python script to hash a list of passwords and compare the resulting hashes against a list. I know that these 4 hashes match 4 of the listed passwords hashed with sha1 and salted with either md5(1), md5(2), md5(3), md5(4) or md5(5). My code prints 50 unique hashes however none of them match and I can't figure out why. Any help is appreciated.
import hashlib
possibleHashes = ["9537935389a343a4fbd831b621b00661b91a5172", "576b1b134cb89b0f8a6c4dd1698479a1151b0e63", "5d7196b530fdd24b8faaaecbaa6b08a29daa1304", "d6acda01abcfe8afd510b96c1d0a1645ea4c40b8"]
possiblePasswords = ["123456", "123456789", "qwerty", "password", "12345", "qwerty123", "1q2w3e", "12345678", "111111", "1234567890"]
hashesFound = 0
def hashPassword(saltNum, password):
password = password.encode('utf-8')
encryptedPassword = hashlib.sha1(hashlib.md5(int.to_bytes(saltNum)).digest() + password).hexdigest()
return encryptedPassword
i = 0
while (i < len(possiblePasswords)):
j = 0
print("I: ", i)
while (j < 5):
j += 1
print("J: ", j, " " + hashPassword(j, possiblePasswords[i]))
if (hashPassword(j, possiblePasswords[i]) in possibleHashes):
print(possiblePasswords[i], "\n" + hashPassword(j, possiblePasswords[i]))
i += 1
if i == 10:
print("\nHash Matches Found: ", hashesFound, "\n")
quit()
Edit:
I have tried making saltNum a string and encoding it before it's used in the md5 call however it has made no difference.
I am making a code in python to make password and save in the text file, if the user enters the same password in the file, the error code will appear. The problem is that although I made a two statement to check the password is valid and there is no same password in the text file, the statement of the one that will check text file to find same file. Can you tell me where I get mistake?
def InputString():
String = str(input("Enter your password. :"))
return String
def CheckPassword(Password):
##I declared "Index","Upper","Lower","Digit" and "Other" as Integer.
##In python, there is no data type for
##"char", so We use string instead.
##I use "variable" as boolean for checking password.
Upper = 0
Lower = 0
Digit = 0
Other = 0
variable = False
for index in range(len(Password)):
NextChar = (Password[index:index+1])
if NextChar >= "A" and NextChar <= "Z":
Upper = Upper + 1
elif NextChar >= "a" and NextChar <= "z":
Lower = Lower + 1
elif NextChar >= "0" and NextChar <= "9":
Digit = Digit + 1
else :
Other = Other + 1
if Upper > 1 and Lower >= 5 and (Digit - Other) > 0:
variable = True
else :
variable = False
return variable
def CheckThrough (Password):
FileP = open("Password.txt","r")
if FileP.mode == 'r':
contents = FileP.read()
if contents == Password:
usable = False
else :
usable = True
FileP.close()
return usable
###The main programs starts here###
print("Enter the password.")
print("You must add at least 1 upper character.")
print("5 lower character")
print("and the difference between numeric character and symbols must be more than 1.")
variable = False
usable = False
while variable == False and usable == False:
Password = InputString()
variable = CheckPassword(Password)
if variable == True:
usable = CheckThrough(Password)
if usable == True:
print("You can use the password.")
elif usable ==False :
print("The password you entered is already used.")
else :
print("The error is ocuured.")
else :
print("You can't use this password, try again.")
print("Your password is",Password)
FileP = open("Password.txt","a+")
FileP.write(Password)
FileP.write("\n")
FileP.close()
you need to add .strip() so it has no spaces e.g String = str(input("Enter your password. :").strip()). When you enter in the password there's a space but that doesn't get saved to the file thus it always reads it as a new password.
my input = Enter your password. :Kingdom1hearTs
The input into the program = " Kingdom1hearTs"
(used print to see what was being entered into the system)
and the if contents == Password: needs to be if Password in contents:otherwise your seeing if Password is the same as the whole txt file.
NextChar = (Password[index:index+1])
if NextChar >= "A" and NextChar <= "Z":
Upper = Upper + 1
elif NextChar >= "a" and NextChar <= "z": #had the wrong indentation
Lower = Lower + 1
elif NextChar >= "0" and NextChar <= "9":
Digit = Digit + 1
else :
Other = Other + 1
I would like to create a strong password in groovy with alphabets both small and Capital and numbers and special chars.
Following are the required special chars:
~`!##$%^&*()-_=+[{]}\|;:'",<.>/?
I am using the below code but I would also like to at least one Special Character in my password.
def pass_length = 15;
def pool = ['a'..'z','A'..'Z',0..9,'_'].flatten();
Random rand = new Random(System.currentTimeMillis());
def passChars = (0..pass_length - 1).collect { pool[rand.nextInt(pool.size())] };
def PASSWORD = passChars.join();
Currently its creating a alphanumeric password only. Any quick changes I can make to the code? Help me as I am new to use groovy.
You can pick a random special character, and put it a random position in the generated password. This will make sure there's at least one special character in the password.
Also, why not adding the special characters to your dictionary as well? In this way there are more possibilities that a special character will end up in the final string.
def pass_length = 15;
def special = ['~' ,'`', ...] // you get the idea...
def pool = ['a'..'z','A'..'Z',0..9,'_'].flatten().plus(special);
Random rand = new Random(System.currentTimeMillis());
def passChars = (0..pass_length - 1).collect { pool[rand.nextInt(pool.size)] };
def specialChar = special[rand.nextInt(special.size)]
passChars[rand.nextInt(passChars.size)] = specialChar
def PASSWORD = passChars.join();
The below code generates a random password of a specified length using the specified base alphabet and specific number of special characters from a "special characters alphabet":
def alphabet = ('a'..'z') + ('A'..'Z') + ('0'..'9')
def specials = /~`!##$%^&*()-_=+[{]}\|;:'",<.>\/?/
def random = new Random()
def r = { max -> random.nextInt(max) } // utility closure
// to test - generate 10 passwords and print them on stdout
10.times {
// generate a 15 character password with the given alphabet and specials
// set with one or two special characters randomly inserted
def password = randomPassword(r, alphabet, specials, 15, (1..2))
println password
}
def randomPassword(r, alphabet, specials, passwordLen, specialsRange) {
// generate non-specials password
def password = randomChars(r, alphabet, passwordLen)
// cointoss to determine number of specials
def nSpecials = specialsRange.from + r(specialsRange.to-specialsRange.from+1)
// insert that number of random specials picked from the specials set
nSpecials.times {
password[r(password.size())] = specials[r(specials.size())]
}
password.join()
}
def randomChars(r, alphabet, length) {
(1..length).collect { alphabet[r(alphabet.size())] }
}
and a sample run:
~> groovy solution.groovy
06HT.Q5J4OOvN}S
U1h28bRcu{o)0l2
VxhMSD#6H7Qt(vH
9Bt!G2v.LT0u4ES
r1=RyWJO6R}x2Gl
sOBimChqOY3P1#x
5A]V4iiUQXkZdXv
R1iwlkZT-Ou;BrO
HbNAF>W4NRFSYRF
evuLb~ma%fzcSuA
~>
Just sharing this here for the record. Creates passwords from length 6 to 100 (these values can be changed in the code).
//Author: Rinaldi Michael
//Last Modified: 14th Dec 2022
//Password from length 6 to 100
import java.io.*
import java.lang.*
String passwords=""
Random rnd = new Random()
int randomNumber = rnd.nextInt(100)
if(randomNumber==0 || randomNumber<6)
randomNumber+=6
String[] passwordCharacters=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"
,"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
,"!","#","%","^","&","*","(",")","-","+","=",".","/",">","<",";",":","|"]
for(int p=0;p<randomNumber;)
{
try
{
passwords = passwords+passwordCharacters[rnd.nextInt(passwordCharacters.size())]
p++
}
catch(Exception ex)
{
p++
}
}
return passwords
I need to write a program that prompts a user for a word, then generates random strings of letters of that length until the word is generated and show how many tries it took. here is the code i have so far:
System.out.println("(Match a word) Enter a word to be matched randomly: ");
String userWord = input.next();
String lowerUserWord = userWord.toLowerCase();
int wordLength = lowerUserWord.length();
System.out.println("Your word has " + wordLength + " letters");
while (lowerUserWord != ) {
//somehow generate random words of the length 'wordLength'
}
self.nanokdp_p.sendline(cmd)
if self.nanokdp_p.expect(match, timeout = timeout)==0:
print ("Device Sleep")
Right now i run a command on an interactive output using self.nanokdp_p.sendline(cmd)
Now i want to expect occourance of "match" string but dont want to stop at first occourence, instead i want expect or some custom function to run for a particular time and count occourances of match.
Also, this thing works if i just look and stop for first occourance.
self.thread = pexpect.spawn(cmd, logfile = my_log_file, maxread = 1)
def expectMultipleOccurrencesOfanExp(thread, expToExpect, count, timeout=120, wait = 5):
count += 1 # to match prints
for i in range(1, count):
if thread.expect(expToExpect, timeout)==0:
if i == count:
return True
elif i < count:
print (expToExpect + " found for " + str(i) + "th time")
time.sleep(wait)
continue
return False