Add SHA1 Module to Excel

Rem Function source: https://stackoverflow.com/a/52276018
Function SHA(MyRange As Range)
    Dim aBytes: aBytes = MyRange.Value
    Dim oBytes: oBytes = CreateObject("System.Text.UTF8Encoding").GetBytes_4(aBytes)

    Dim oSHA1: Set oSHA1 = CreateObject("System.Security.Cryptography.SHA1CryptoServiceProvider")
    oBytes = oSHA1.ComputeHash_2((oBytes))
    
    Dim hexStr, x
    For x = 1 To LenB(oBytes)
        hexStr = LCase(Hex(AscB(MidB((oBytes), x, 1))))
        If Len(hexStr) = 1 Then hexStr = "0" & hexStr
        SHA = SHA & hexStr
    Next
End Function

Leave a comment