lundi 27 juin 2016

binary outputs are different in hash SHA256

SHA256 hashing output is different in two codes, input is same so outputs should be identical, the code seems to be fine, I can't understand why it produces different results. What is wrong with it?

VB.NET CODE

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim temphashstring As String = "1a"
        temphashstring = hashed(temphashstring)
        T7.Text = StringToAsciiBits(temphashstring)

End Sub

Public Function StringToAsciiBits(ByVal inputstring As String) 'Convert string text to binary text

        Dim bitsAsString As String
        Dim Re As New System.Text.StringBuilder
        For Each Character As Byte In System.Text.ASCIIEncoding.ASCII.GetBytes(inputstring)
            Re.Append(Convert.ToString(Character, 2).PadLeft(8, "0"))
            'Re.Append(" ") 'put one space (" ") after every 8 bits to view easily by human eye
        Next
        bitsAsString = Re.ToString.Substring(0, Re.ToString.Length - 0)


        Return bitsAsString
    End Function

Public Function hashed(ByVal inputstring As String) As String

        'Encode by UTF8
        Dim pt As New System.Text.UTF8Encoding
        Dim ptBytes() As Byte = pt.GetBytes(inputstring)
        Dim ptSBytes() As Byte = New Byte(ptBytes.Length - 1) {}
        'Copy plaintext bytes to array.
        Dim num As Integer
        Dim hash As HashAlgorithm
        'hashing algorithm class.
        hash = New SHA256Managed()
        ' Compute hash

        For num = 0 To ptBytes.Length - 1
            ptSBytes(num) = ptBytes(num)
        Next num

        Dim hashBytes As Byte()
        hashBytes = hash.ComputeHash(ptSBytes)
        ' Convert into a base64-encoded string.

        Dim hashValue As String
        hashValue = Convert.ToBase64String(hashBytes)
        hashed = hashValue 'return hashvalue
    End Function

Input: 1a

Output in binary: 0111000001111010001011110101000001001101001101010101101001000001011010110111000001001001010010000100101101000010001010110011010000110100010001000110100101001001010100110100000101100010011010010011011001110111011010000100000100111000011010010101001001010111011011000100111001110101001101100100100001010110011110100100100101101110011011010101010100111101

PYTHON CODE

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

Input: 1a

Output in binary: '0b10100111', '0b111111', '0b11001111', '0b110011', '0b10010110', '0b1000000', '0b10010010', '0b10010010', '0b111', '0b101000', '0b11111', '0b10111000', '0b11100000', '0b111000', '0b10001000', '0b1001000', '0b110', '0b11100010', '0b11101011', '0b1000', '0b1000000', '0b11110010', '0b100100', '0b1010110', '0b10010100', '0b11011011', '0b10111010', '0b11101', '0b1011100', '0b11001000', '0b10011110', '0b1100101'

Aucun commentaire:

Enregistrer un commentaire