Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 二进制输出在哈希SHA256中不同_Python_Vb.net - Fatal编程技术网

Python 二进制输出在哈希SHA256中不同

Python 二进制输出在哈希SHA256中不同,python,vb.net,Python,Vb.net,SHA256哈希输出在两个代码中是不同的,输入是相同的,所以输出应该是相同的,代码似乎很好,我不明白为什么它会产生不同的结果。怎么了 VB.NET代码 import hashlib, struct tempheader = "1a" print map(bin, map(ord,hashlib.sha256(tempheader).digest())) Private子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击 Dim temphashstring为String=“1a”

SHA256哈希输出在两个代码中是不同的,输入是相同的,所以输出应该是相同的,代码似乎很好,我不明白为什么它会产生不同的结果。怎么了

VB.NET代码

import hashlib, struct
tempheader = "1a"
print map(bin, map(ord,hashlib.sha256(tempheader).digest()))
Private子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
Dim temphashstring为String=“1a”
temphashstring=散列(temphashstring)
T7.Text=stringtoasibits(temphashstring)
端接头
公共函数StringToAsciiBits(ByVal inputstring作为字符串)'将字符串文本转换为二进制文本
Dim bitsAsString作为字符串
Dim Re作为新System.Text.StringBuilder
对于System.Text.AscienceODing.ASCII.GetBytes(inputstring)中的每个字节
Re.Append(Convert.ToString(字符,2.PadLeft(8,“0”))
'Re.Append(“”)在每8位后面加一个空格(“”),以便于肉眼查看
下一个
bitsAsString=Re.ToString.Substring(0,Re.ToString.Length-0)
返回位字符串
端函数
作为字符串散列的公共函数(ByVal inputstring作为字符串)
'通过UTF8编码
将pt调暗为新System.Text.UTF8Encoding
Dim ptBytes()的形式为Byte=pt.GetBytes(inputstring)
Dim ptSBytes()作为字节=新字节(ptBytes.Length-1){
'将明文字节复制到数组。
Dim num作为整数
作为哈希算法的Dim散列
'哈希算法类。
hash=New SHA256Managed()
'计算哈希
对于num=0到ptBytes.Length-1
ptSBytes(num)=ptBytes(num)
下一个数字
Dim hashBytes作为Byte()
hashBytes=hash.ComputeHash(ptSBytes)
'转换为base64编码的字符串。
将哈希值设置为字符串
hashValue=Convert.ToBase64String(hashBytes)
hashed=hashValue'返回hashValue
端函数
输入:
1a

二进制输出:
0111000001110100010111110101000000110100110101010110100100100001011010111000000010010100100010010010010010000100100010110010000101010001100101001001001001001001001000110001000101001001001010100000100111000100100010010010010010100100101011101100010001001001010010010010010010110001001001001001001001001001001000100100101011001100010010010010010010010010010010010101010010101001001001001110011001100010010010010010010010101010101010101101101101010101010101010101101010101101101110011001100010010101010101100111101

PYTHON代码

import hashlib, struct
tempheader = "1a"
print map(bin, map(ord,hashlib.sha256(tempheader).digest()))
输入:
1a


二进制输出:
“0b10100111”、“0b111111”、“0b11001111”、“0b110011”、“0b10010110”、“0b1000000”、“0B10010”、“0B10010”、“0b111”、“0b101000”、“0b111111”、“0B1011100”、“0B1110000”、“0B1110000”、“0b10001000”、“0b1001000”、“0b11100010”、“0b11101011”、“0b1000”、“0b100100”、“0b100100”、“0B111100”、“0b1010110”、“0B10100010”,“0b11011011”、“0B1011010”、“0b11101”、“0b1011100”、“0b11001000”、“0b10011110”、“0b1100101”

我不清楚为什么在VB版本中使用Base64字符串。在Python版本中不会这样做,因此应该预期结果会有所不同

下面是不使用Base64的类似代码:

Function SHA256BytesAsBinary(s As String) As String
    Dim pt As New System.Text.UTF8Encoding
    Dim ptBytes() As Byte = pt.GetBytes(s)
    Dim hash = New SHA256Managed()
    Dim bb = hash.ComputeHash(ptBytes)

    Return String.Join(" ", bb.Select(Function(x) Convert.ToString(x, 2).PadLeft(8, "0")))

End Function
所以

产出:

10100111 00111111 11001111 00110011 10010110 0100000010010010 10010010 00000111 00101000 00011111111 10111000 1110000000111000 10001000 01000 00000110 111000010 11101011 000010000 0100000011110010000100 01010110 10010100 11011 10111010 0001101 01011100 110011110 011001100101