Python中的随机哈希

问题:Python中的随机哈希

在Python中生成随机哈希(MD5)的最简单方法是什么?

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


回答 0

md5-hash只是一个128位的值,因此如果您想要一个随机的值:

import random

hash = random.getrandbits(128)

print("hash value: %032x" % hash)

不过,我并不真正明白这一点。也许您应该详细说明为什么需要这个…

A md5-hash is just a 128-bit value, so if you want a random one:

import random

hash = random.getrandbits(128)

print("hash value: %032x" % hash)

I don’t really see the point, though. Maybe you should elaborate why you need this…


回答 1

我认为您正在寻找的是通用唯一标识符。然后python中的模块UUID就是您正在寻找的东西。

import uuid
uuid.uuid4().hex

UUID4为您提供了一个随机的唯一标识符,其长度与md5和的长度相同。十六进制将以十六进制字符串形式表示,而不是返回uuid对象。

http://docs.python.org/2/library/uuid.html

I think what you are looking for is a universal unique identifier.Then the module UUID in python is what you are looking for.

import uuid
uuid.uuid4().hex

UUID4 gives you a random unique identifier that has the same length as a md5 sum. Hex will represent is as an hex string instead of returning a uuid object.

http://docs.python.org/2/library/uuid.html


回答 2

这适用于python 2.x和3.x

import os
import binascii
print(binascii.hexlify(os.urandom(16)))
'4a4d443679ed46f7514ad6dbe3733c3d'

This works for both python 2.x and 3.x

import os
import binascii
print(binascii.hexlify(os.urandom(16)))
'4a4d443679ed46f7514ad6dbe3733c3d'

回答 3

secrets模块是在Python 3.6+中添加的。它通过一次调用提供加密安全的随机值。这些函数带有一个可选nbytes参数,默认值为32(字节* 8位= 256位令牌)。MD5具有128位哈希,因此请为“类似于MD5”的令牌提供16个哈希。

>>> import secrets

>>> secrets.token_hex(nbytes=16)
'17adbcf543e851aa9216acc9d7206b96'

>>> secrets.token_urlsafe(16)
'X7NYIolv893DXLunTzeTIQ'

>>> secrets.token_bytes(128 // 8)
b'\x0b\xdcA\xc0.\x0e\x87\x9b`\x93\\Ev\x1a|u'

The secrets module was added in Python 3.6+. It provides cryptographically secure random values with a single call. The functions take an optional nbytes argument, default is 32 (bytes * 8 bits = 256-bit tokens). MD5 has 128-bit hashes, so provide 16 for “MD5-like” tokens.

>>> import secrets

>>> secrets.token_hex(nbytes=16)
'17adbcf543e851aa9216acc9d7206b96'

>>> secrets.token_urlsafe(16)
'X7NYIolv893DXLunTzeTIQ'

>>> secrets.token_bytes(128 // 8)
b'\x0b\xdcA\xc0.\x0e\x87\x9b`\x93\\Ev\x1a|u'

回答 4

还有另一种方法。您无需格式化int即可获取它。

import random
import string

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

给您字符串长度的灵活性。

>>> random_string(64)
'XTgDkdxHK7seEbNDDUim9gUBFiheRLRgg7HyP18j6BZU5Sa7AXiCHP1NEIxuL2s0'

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'

回答 5

解决此特定问题的另一种方法:

import random, string

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

我并不是说它比任何其他答案都更快或更可取。只是这是另一种方法:)

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 :)


回答 6

import uuid
from md5 import md5

print md5(str(uuid.uuid4())).hexdigest()
import uuid
from md5 import md5

print md5(str(uuid.uuid4())).hexdigest()

回答 7

import os, hashlib
hashlib.md5(os.urandom(32)).hexdigest()
import os, hashlib
hashlib.md5(os.urandom(32)).hexdigest()

回答 8

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)

还应注意,MD5是一个非常弱的哈希函数,还发现了冲突(两个不同的纯文本值导致相同的哈希),只需对使用随机值即可plaintext

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.