在Python中生成序列为1和0的SHA 1哈希的任何方法

发布于 2025-02-04 08:41:50 字数 2711 浏览 3 评论 0原文

我想生成1s和0s的随机序列,并将其输入到Python中的SHA1哈希计算器中。

hashlib库( doc link 在其update()函数中输入。 我尝试使用andand.getrandbits(64)生成一个随机序列,但是当我尝试使用.to_bytes()将其转换为字节时,它会出现“ UTF-8”编解码器无法解码的错误。

代码:

x = random.getrandbits(64)
print(x)
print(format(x, 'b'))

binary_int = int(format(x, 'b'), 2)
  
# Getting the byte number
byte_number = (binary_int.bit_length() + 7) // 8
  
# Getting an array of bytes
binary_array = binary_int.to_bytes(byte_number, "big")
  
# Converting the array into ASCII text
ascii_text = binary_array.decode()
  
# Getting the ASCII value
print(ascii_text)

错误:

17659976144931976749
1111010100010100110101101011110010111100100010101111011000101101
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
/var/folders/9s/msn7k8q55yn6t6br55830hc40000gn/T/ipykernel_33103/157314006.py in <module>
     12 
     13 # Converting the array into ASCII text
---> 14 ascii_text = binary_array.decode()
     15 
     16 # Getting the ASCII value

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf5 in position 0: invalid start byte

我意识到错误意味着生成的随机位序列对于UTF-8/ASCII代码无效,但是我如何处理该序列以创建SHA1函数的有效输入?

我还尝试了提到的建议在这里使用“ ISO-8859-1”编码:

binary_int = random.getrandbits(64)

# Getting the byte number
byte_number = (binary_int.bit_length() + 7) // 8
  
# Getting an array of bytes
binary_array = binary_int.to_bytes(byte_number, "big")
  
# Converting the array into ASCII text
text = binary_array.decode(encoding='ISO-8859-1')
  
print(text)

print(type(text))

print(len(text))

import sys
print(sys.getsizeof(text.encode('ISO-8859-1')))

print(hash_sha1(text.encode('ISO-8859-1')))

输出:

¦—u¦9}5É
<class 'str'>
8
41
bc25cb6cb34c2b7c73bbba610e0388386c2e70b2

但是sys.getSizeof()prints for text.encode('iso-8859-1'),而不是64位,因为它应该是。

在上面的代码中,我尝试64位数据,以进行测试。但是,最终,我只想确保将恒定大小的随机生成的512位数据输入到SHA1发电机中。我希望有什么办法。谢谢。

编辑:使它起作用,这要归功于Drakax的答案

最终代码:

import os, hashlib
k = os.urandom(64)
# print random no.
print(k)

# print it in bit format (64 bits)
for byte in k:
    print(f'{byte:0>8b}', end='')
print()

# print the sha1 hash 
print(hashlib.sha1(k).hexdigest())

I want to generate a random sequence of 1s and 0s and input it into the SHA1 hash calculator in Python.

The hashlib library (doc link) for generating hashes accepts byte-like objects as input in its update() function.
I have tried using random.getrandbits(64) to generate a random sequence, but when I try and convert it into bytes using .to_bytes(), it gives the error that the 'utf-8' codec can't decode it.

Code:

x = random.getrandbits(64)
print(x)
print(format(x, 'b'))

binary_int = int(format(x, 'b'), 2)
  
# Getting the byte number
byte_number = (binary_int.bit_length() + 7) // 8
  
# Getting an array of bytes
binary_array = binary_int.to_bytes(byte_number, "big")
  
# Converting the array into ASCII text
ascii_text = binary_array.decode()
  
# Getting the ASCII value
print(ascii_text)

Error:

17659976144931976749
1111010100010100110101101011110010111100100010101111011000101101
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
/var/folders/9s/msn7k8q55yn6t6br55830hc40000gn/T/ipykernel_33103/157314006.py in <module>
     12 
     13 # Converting the array into ASCII text
---> 14 ascii_text = binary_array.decode()
     15 
     16 # Getting the ASCII value

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf5 in position 0: invalid start byte

I realize that the error means that the generated random bit sequence is invalid for UTF-8/ASCII code, but how do I work around that to create valid inputs for the SHA1 function?

I have also tried the suggestion mentioned here to use the "ISO-8859-1" encoding:

binary_int = random.getrandbits(64)

# Getting the byte number
byte_number = (binary_int.bit_length() + 7) // 8
  
# Getting an array of bytes
binary_array = binary_int.to_bytes(byte_number, "big")
  
# Converting the array into ASCII text
text = binary_array.decode(encoding='ISO-8859-1')
  
print(text)

print(type(text))

print(len(text))

import sys
print(sys.getsizeof(text.encode('ISO-8859-1')))

print(hash_sha1(text.encode('ISO-8859-1')))

Output:

¦—u¦9}5É
<class 'str'>
8
41
bc25cb6cb34c2b7c73bbba610e0388386c2e70b2

But sys.getsizeof() prints 81 Bytes for text.encode('ISO-8859-1') and not 64 bits as it is supposed to be.

In the above codes, I try 64 bit data, for testing purposes. But, ultimately, I just want to ensure that I am inputting constant-sized randomly generated 512-bit data into the SHA1 generator. Is there any way for that, I hope so. Thanks.

Edit: made it work, thanks to answer by Drakax

Final code:

import os, hashlib
k = os.urandom(64)
# print random no.
print(k)

# print it in bit format (64 bits)
for byte in k:
    print(f'{byte:0>8b}', end='')
print()

# print the sha1 hash 
print(hashlib.sha1(k).hexdigest())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各空 2025-02-11 08:41:50

您是否尝试过其中之一:

1。 uuid

import uuid
uuid.uuid4().hex

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

1.1

import uuid
from md5 import md5

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

2。秘密( python 3.6+

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'

doc:
https://docs.python.org/3/library/library/secrets.htmls.html

3。 binascii( python 2.x和3.x

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

doc:

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

​//docs.python.org/3/library/hashlib.html“ rel =” nofollow noreferrer“> https://docs.python.org/3/library/hashlib.html.html

现在应该足够; )

Have you tried one of those:

1. UUID

import uuid
uuid.uuid4().hex

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

1.1

import uuid
from md5 import md5

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

2. Secrets (Python 3.6+)

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'

Doc:
https://docs.python.org/3/library/secrets.html

3. binascii (python 2.x and 3.x)

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

Doc: https://docs.python.org/3/library/binascii.html

4. hashlib

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

Doc: https://docs.python.org/3/library/hashlib.html

Should be enough for now ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文