Generate password in python

前端 未结 9 2016
既然无缘
既然无缘 2020-12-12 21:51

I\'dl like to generate some alphanumeric passwords in python. Some possible ways are:

import string
from random import sample, choice
chars = string.letters          


        
9条回答
  •  忘掉有多难
    2020-12-12 22:20

    For the crypto-PRNG folks out there:

    def generate_temp_password(length):
        if not isinstance(length, int) or length < 8:
            raise ValueError("temp password must have positive length")
    
        chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"
        from os import urandom
        return "".join(chars[ord(c) % len(chars)] for c in urandom(length))
    

    Note that for an even distribution, the chars string length ought to be an integral divisor of 128; otherwise, you'll need a different way to choose uniformly from the space.

提交回复
热议问题