md5とSHA256でハッシュ化 - VB.NET
それぞれのクラスで定義されているComputeHash()
メソッドの引数と戻り値はバイト配列になっているため、注意
md5でハッシュ化
Module Module1 Sub Main() Dim origByte As Byte() = System.Text.Encoding.UTF8.GetBytes("Hello") Dim md5 As New System.Security.Cryptography.MD5CryptoServiceProvider() Dim hashValue As Byte() = md5.ComputeHash(origByte) 'byte型配列を16進数の文字列に変換 Dim result As New System.Text.StringBuilder() Dim b As Byte For Each b In hashValue result.Append(b.ToString("x2")) Next Console.Write(result) Console.ReadLine() End Sub End Module
出力
8b1a9953c4611296a827abf8c47804d7
SHA256でハッシュ化
Module Module1 Sub Main() Dim origByte As Byte() = System.Text.Encoding.UTF8.GetBytes("Hello") Dim sha256 As System.Security.Cryptography.SHA256 = New System.Security.Cryptography.SHA256CryptoServiceProvider() Dim hashValue As Byte() = sha256.ComputeHash(origByte) 'byte型配列を16進数の文字列に変換 Dim result As New System.Text.StringBuilder() Dim b As Byte For Each b In hashValue result.Append(b.ToString("x2")) Next Console.Write(result) Console.ReadLine() End Sub End Module
実行結果
185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969