如何在 python 中使用 sha256 原始位?
How do I sha256 raw bits in python?
h = '''0011001010000101011111010000101111111111101000001001000001001010110100010101111001001011000100111100011110001001111011110111011010010100110011001110111001100010111011010010101101010011110100100110101111110001100101011001000110100010000110110001100101110001'''
a = binascii.hexlify(hashlib.sha256(bytes(h,'utf-8')).digest()).decode()
>>> a
44f6dafa3d7a1720b5ebbf2adc1663df4dab03776eed48d2cda775237a547e59
所以我有一个代表一些二进制数据的字符串。写完上面的代码,才知道这个方法输出的是字符串的ascii的sha256。相反,我希望它输出类似于以下内容的原始位的 sha256:
$ echo 0011001010000101011111010000101111111111101000001001000001001010110100010101111001001011000100111100011110001001111011110111011010010100110011001110111001100010111011010010101101010011110100100110101111110001100101011001000110100010000110110001100101110001 | shasum -a 256 -0
所以sha256应该是
>>> a
f3f06d74b794b20645460aa0b17d4e7a77eaaea283ee55344adbfcece4a63432
我试过的每一种方法都给我错误,而且我似乎无法在网上找到答案。
有人知道这是怎么做到的吗?
import binascii
import hashlib
s = '''0011001010000101011111010000101111111111101000001001000001001010110100010101111001001011000100111100011110001001111011110111011010010100110011001110111001100010111011010010101101010011110100100110101111110001100101011001000110100010000110110001100101110001'''
h=int(s, 2).to_bytes((len(s) + 7) // 8, byteorder='big')
a = binascii.hexlify(hashlib.sha256(h).digest()).decode()
输出:
'f3f06d74b794b20645460aa0b17d4e7a77eaaea283ee55344adbfcece4a63432'
2 Return 表示整数的字节数组。
如果 byteorder 为 "big"
,则最高有效字节位于字节数组的开头。如果 byteorder 是 "little"
,最高有效字节在字节数组的末尾。
hash.digest() 和 hash.hexdigest()
类似,只是摘要作为双倍长度的字符串对象返回,在后一种情况下仅包含十六进制数字
h = '''0011001010000101011111010000101111111111101000001001000001001010110100010101111001001011000100111100011110001001111011110111011010010100110011001110111001100010111011010010101101010011110100100110101111110001100101011001000110100010000110110001100101110001'''
a = binascii.hexlify(hashlib.sha256(bytes(h,'utf-8')).digest()).decode()
>>> a
44f6dafa3d7a1720b5ebbf2adc1663df4dab03776eed48d2cda775237a547e59
所以我有一个代表一些二进制数据的字符串。写完上面的代码,才知道这个方法输出的是字符串的ascii的sha256。相反,我希望它输出类似于以下内容的原始位的 sha256:
$ echo 0011001010000101011111010000101111111111101000001001000001001010110100010101111001001011000100111100011110001001111011110111011010010100110011001110111001100010111011010010101101010011110100100110101111110001100101011001000110100010000110110001100101110001 | shasum -a 256 -0
所以sha256应该是
>>> a
f3f06d74b794b20645460aa0b17d4e7a77eaaea283ee55344adbfcece4a63432
我试过的每一种方法都给我错误,而且我似乎无法在网上找到答案。
有人知道这是怎么做到的吗?
import binascii
import hashlib
s = '''0011001010000101011111010000101111111111101000001001000001001010110100010101111001001011000100111100011110001001111011110111011010010100110011001110111001100010111011010010101101010011110100100110101111110001100101011001000110100010000110110001100101110001'''
h=int(s, 2).to_bytes((len(s) + 7) // 8, byteorder='big')
a = binascii.hexlify(hashlib.sha256(h).digest()).decode()
输出:
'f3f06d74b794b20645460aa0b17d4e7a77eaaea283ee55344adbfcece4a63432'
"big"
,则最高有效字节位于字节数组的开头。如果 byteorder 是 "little"
,最高有效字节在字节数组的末尾。
hash.digest() 和 hash.hexdigest()
类似,只是摘要作为双倍长度的字符串对象返回,在后一种情况下仅包含十六进制数字