Random hash in Python

后端 未结 9 1724
陌清茗
陌清茗 2020-12-23 13:11

What is the easiest way to generate a random hash (MD5) in Python?

9条回答
  •  滥情空心
    2020-12-23 13:24

    Yet another approach. You won't have to format an int to get it.

    import random
    import string
    
    def random_string(length):
        pool = string.letters + string.digits
        return ''.join(random.choice(pool) for i in xrange(length))
    

    Gives you flexibility on the length of the string.

    >>> random_string(64)
    'XTgDkdxHK7seEbNDDUim9gUBFiheRLRgg7HyP18j6BZU5Sa7AXiCHP1NEIxuL2s0'
    

提交回复
热议问题