Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a salt in python

How would I create a random, 16-character base-62 salt in python? I need it for a protocol and I'm not sure where to start. Thanks.

like image 378
pajm Avatar asked Mar 14 '11 01:03

pajm


People also ask

What is a salt in Python?

Salt (sometimes referred to as SaltStack) is a Python-based, open-source software for event-driven IT automation, remote task execution, and configuration management.

What is a salt in hashing?

A cryptographic salt is made up of random bits added to each password instance before its hashing. Salts create unique passwords even in the instance of two users choosing the same passwords. Salts help us mitigate hash table attacks by forcing attackers to re-compute them using the salts for each user.


2 Answers

>>> import random >>> ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" >>> chars=[] >>> for i in range(16):     chars.append(random.choice(ALPHABET))  >>> "".join(chars) 'wE9mg9pu2KSmp5lh' 

This should work.

like image 124
Utku Zihnioglu Avatar answered Sep 19 '22 06:09

Utku Zihnioglu


You shouldn't use UUIDs, they are unique, not random: Is using a CreateUUID() function as salt a good idea?

Your salts should use a cryptographically secure random numbers, in python 2.4+, os.urandom is the source of these (if you have a good timing source).

# for some given b62encode function  salt = b62encode(os.urandom(16)) 

you could also use a generator from bcrypt or other awesome crypto/hashing library that is well known and vetted by the people much more expert than I am.

import bcrypt salt = bcrypt.gensalt() # will be 29 chars you can then encode it however you want. 
like image 33
yarbelk Avatar answered Sep 20 '22 06:09

yarbelk