在Python中生成序列为1和0的SHA 1哈希的任何方法
我想生成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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过其中之一:
1。 uuid
doc:
http://docs.python.org/2/library/library/uuid.uid.html
1.1
2。秘密( python 3.6+)
doc:
https://docs.python.org/3/library/library/secrets.htmls.html
3。 binascii( python 2.x和3.x )
doc:
//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
Doc:
http://docs.python.org/2/library/uuid.html
1.1
2. Secrets (Python 3.6+)
Doc:
https://docs.python.org/3/library/secrets.html
3. binascii (python 2.x and 3.x)
Doc: https://docs.python.org/3/library/binascii.html
4. hashlib
Doc: https://docs.python.org/3/library/hashlib.html
Should be enough for now ;)