Random hash in Python

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

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

相关标签:
9条回答
  • 2020-12-23 13:38

    Another approach to this specific question:

    import random, string
    
    def random_md5like_hash():
        available_chars= string.hexdigits[:16]
        return ''.join(
            random.choice(available_chars)
            for dummy in xrange(32))
    

    I'm not saying it's faster or preferable to any other answer; just that it's another approach :)

    0 讨论(0)
  • 2020-12-23 13:39
    from hashlib import md5
    plaintext = input('Enter the plaintext data to be hashed: ') # Must be a string, doesn't need to have utf-8 encoding
    ciphertext = md5(plaintext.encode('utf-8').hexdigest())
    print(ciphertext)
    

    It should also be noted that MD5 is a very weak hash function, also collisions have been found (two different plaintext values result in the same hash) Just use a random value for plaintext.

    0 讨论(0)
  • 2020-12-23 13:43
    import os, hashlib
    hashlib.md5(os.urandom(32)).hexdigest()
    
    0 讨论(0)
提交回复
热议问题