VBAでSHA256の値を取得する

Public Function GetSHA256(str As String) As String

    Dim utf8 As Object
    Dim sha256 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 sha256 = CreateObject("System.Security.Cryptography.SHA256Managed")

    bytes = utf8.GetBytes_4(str)
    hash = sha256.ComputeHash_2((bytes))
 
    For i = LBound(hash) To UBound(hash)
        res = res & LCase(Right("0" & Hex(hash(i)), 2))
    Next i
 
    GetSHA256 = LCase(res)
	
End Function

コメント