PublicFunction SHA1Hash(sIn AsString, Optional bB64 AsBoolean = 0) AsString Dim oT AsObject, oSHA1 AsObject Dim TextToHash() AsByte Dim bytes() AsByte Set oT = CreateObject("System.Text.UTF8Encoding") Set oSHA1 = CreateObject("System.Security.Cryptography.SHA1Managed") TextToHash = oT.Getbytes_4(sIn) bytes = oSHA1.ComputeHash_2((TextToHash)) If bB64 = TrueThen SHA1Hash = ConvToBase64String(bytes) Else SHA1Hash = ConvToHexString(bytes) EndIf Set oT = Nothing Set oSHA1 = Nothing EndFunction
2. 加入巨集測試
1 2 3 4 5 6 7 8
Sub Test() Dim sIn AsString, b64 AsBoolean Dim sH AsString sIn = "123456" b64 = True'output base-64 sH = SHA1Hash(sIn, b64) Debug.Print vbNewLine & vbNewLine & sH & vbNewLine & Len(sH) & " characters in length" EndSub