Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python password hashing

def encrypt(self, raw):
	raw = self._pad(raw)
    iv = Random.new().read(AES.block_size)
    cipher = AES.new(self.key, AES.MODE_CBC, iv)
    return base64.b85encode(iv + cipher.encrypt(raw.encode()))
Comment

How to store password in hashlib in python

import hashlib
hash = hashlib.sha256(b"alixaprodev.com")
print('Output:', hash.digest())

# Output: b'x9dxe9xc9xc6x93x0blxd3
#    xbax18Wx02x8dxedxfdx0ex14x96hG
#    x99Sxe6xe7xecxf4rQ-x16x91xd8'
    
Comment

hash password python

# Example of adding user and then verifying him/her
import hashlib
import os

users = {} # A simple demo storage
# Add a user
username = 'Brent' # The users username
password = 'mypassword' # The users password
salt = os.urandom(32) # A new salt for this user
key = hashlib.pbkdf2_hmac('sha256', password.encode('utf-8'), salt, 100000)
users[username] = { # Store the salt and key
    'salt': salt,
    'key': key
}

# Verification attempt 1 (incorrect password)
username = 'Brent'
password = 'notmypassword'

salt = users[username]['salt'] # Get the salt
key = users[username]['key'] # Get the correct key
new_key = hashlib.pbkdf2_hmac('sha256', password.encode('utf-8'), salt, 100000)
assert key != new_key # The keys are not the same thus the passwords were not the same

# Verification attempt 2 (correct password)
username = 'Brent'
password = 'mypassword'
salt = users[username]['salt']
key = users[username]['key']
new_key = hashlib.pbkdf2_hmac('sha256', password.encode('utf-8'), salt, 100000)
assert key == new_key # The keys are the same thus the passwords were the same

# Adding a different user
username = 'Jarrod'
password = 'my$ecur3p@$$w0rd'

salt = os.urandom(32) # A new salt for this user
key = hashlib.pbkdf2_hmac('sha256', password.encode('utf-8'), salt, 100000)
users[username] = {
    'salt': salt,
    'key': key
}

# Checking the other users password
username = 'Jarrod'
password = 'my$ecur3p@$$w0rd'

salt = users[username]['salt']
key = users[username]['key']
new_key = hashlib.pbkdf2_hmac('sha256', password.encode('utf-8'), salt, 100000)

assert key == new_key # The keys are the same thus the passwords were the same for this user also
Comment

PREVIOUS NEXT
Code Example
Python :: sqlite query in python 
Python :: python tar a directory 
Python :: convert decimal to hex python 
Python :: after groupby how to add values in two rows to a list 
Python :: how to convert cost to float in python 
Python :: remove unnamed 0 column pandas 
Python :: python extend list 
Python :: how to check type inside a list in python 
Python :: standard scaler vs min max scaler 
Python :: download image from url python 3 
Python :: iso date convert in python 
Python :: post to instagram from pc python 
Python :: write a python program to find table of a number using while loop 
Python :: split string and convert to int python 
Python :: format number in python 
Python :: python regex tester 
Python :: How to remove all characters after character in python? 
Python :: make pickle file python 
Python :: python class 
Python :: make a nested list flat python 
Python :: LoginRequiredMixin 
Python :: how to add rows to empty dataframe 
Python :: pandas rows count 
Python :: python binary remove 0b 
Python :: python ftplib get list of directories 
Python :: python telethon 
Python :: list to dataframe 
Python :: how to rotate screen with python 
Python :: how to merge two dictionaries 
Python :: select rows from a list of indices pandas 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =