Skip to content

Instantly share code, notes, and snippets.

@KEINOS
Last active June 12, 2017 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KEINOS/a3499350b20cf6be80b6e49d53e6110c to your computer and use it in GitHub Desktop.
Save KEINOS/a3499350b20cf6be80b6e49d53e6110c to your computer and use it in GitHub Desktop.
Sample VB script function to get MD5 hash value from a string.
public class compiler
' See sample work at
' https://paiza.io/projects/uoJ2-cB4Os76jMK-O11Csw
shared function Main as integer
' MD5ハッシュ値を計算したい文字列
Dim s As String = "sample text"
' MD5の結果を表示
Console.WriteLine ( md5( s ) )
return 0
End function
' =========================================================
' MD5ハッシュ値を計算する関数
' 参考URL http://dobon.net/vb/dotnet/string/md5.html
'
' @param string $s MD5ハッシュ値を計算したい文字列
' @return string MD5ハッシュ値
' =========================================================
shared Private function md5( ByVal s As String)
'文字列をbyte型配列に変換する
Dim data As Byte() = System.Text.Encoding.UTF8.GetBytes(s)
'MD5CryptoServiceProviderオブジェクトを作成
Dim md5 As New System.Security.Cryptography.MD5CryptoServiceProvider()
'上記は次のようにもできる
'Dim md5 As System.Security.Cryptography.MD5 = _
' System.Security.Cryptography.MD5.Create()
'ハッシュ値を計算する
Dim bs As Byte() = md5.ComputeHash(data)
'リソースを解放する
md5.Clear()
'byte型配列を16進数の文字列に変換
Dim result As New System.Text.StringBuilder()
Dim b As Byte
For Each b In bs
result.Append(b.ToString("x2"))
Next b
'上記は次のようにもできる
'Dim result As String = BitConverter.ToString(bs).ToLower().Replace("-", "")
'結果を表示
return result
End function
end class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment