I'm trying to hash the public key with SHA256 and then ripemed160 and get the hash. but it gives the error "System.ArgumentOutOfRangeException: 'Index and length must refer to a location within the string. Parameter name: length'" I think it's because of the odd number of characters of the public key.
Public key: 2b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737
Hash should be: 93ce48570b55c42c2af816aeaba06cfee1224fae
Imports System.Security.Cryptography
Imports System.Text
Imports System.Globalization
Imports System.Numerics
Private Function HexStringToByteArray(ByVal shex As String) As Byte()
Dim B As Byte() = Enumerable.Range(0, shex.Length).Where(Function(x) x Mod 2 = 0).[Select](Function(x) Convert.ToByte(shex.Substring(x, 2), 16)).ToArray()
Return Enumerable.Range(0, shex.Length).Where(Function(x) x Mod 2 = 0).[Select](Function(x) Convert.ToByte(shex.Substring(x, 2), 16)).ToArray()
End Function
Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
Dim public_key as String = "2b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737"
Dim pubkey_hash As String
Dim sha256 As SHA256 = SHA256Managed.Create()
Dim bytes As Byte() = HexStringToByteArray(public_key)
Dim hash As Byte() = sha256.ComputeHash(bytes)
Dim stringBuilder1 As New StringBuilder()
For i As Integer = 0 To hash.Length - 1
stringBuilder1.Append(hash(i).ToString("X2"))
Next
bytes = HexStringToByteArray(stringBuilder1.ToString())
Dim ripemd160 As RIPEMD160 = RIPEMD160Managed.Create()
Dim hash_160 As Byte() = ripemd160.ComputeHash(bytes)
Dim stringBuilder2 As New StringBuilder()
For i As Integer = 0 To hash_160.Length - 1
stringBuilder2.Append(hash_160(i).ToString("X2"))
Next
Dim arr() As Char = stringBuilder2.ToString
pubkey_hash = arr.tostring
TextBox14.Text = pubkey_hash
End Sub
what I want to achieve is : bitcoin Public key --> SHA256 --> RIPEMD160 = final hash