Python's safest method to store and retrieve passwords from a database

后端 未结 5 1290

Looking to store usernames and passwords in a database, and am wondering what the safest way to do so is. I know I have to use a salt somewhere, but am not sure how to gene

5条回答
  •  粉色の甜心
    2020-12-04 10:48

    Here is a simpler way (taken from effbot), provided passwords with a length greater than 8 will not be a problem*:

    import crypt
    
    import random, string
    
    def getsalt(chars = string.letters + string.digits):
        # generate a random 2-character 'salt'
        return random.choice(chars) + random.choice(chars)
    

    for generate the password :

    crypt.crypt("password", getsalt())
    

    *: A password with a length greater than 8 is stripped from the right down to 8 chars long

提交回复
热议问题