VBAでMD5の値を取得する

Public Function GetMD5(str As String) As String

    Dim utf8 As Object
    Dim md5 as Object
    Dim bytes() As Byte
    Dim hash() As Byte
    Dim i As Integer
    Dim res As String
 
    Set utf8 = CreateObject("System.Text.UTF8Encoding")
    Set md5 = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
    
    bytes = utf8.GetBytes_4(str)
    hash = md5.ComputeHash_2(bytes)
 
    For i = LBound(hash) To UBound(hash)
        res = res & LCase(Right("0" & Hex(hash(i)), 2))
    Next i
 
    GetMD5 = LCase(res)
    
End Function

コメント