Hashing strings in general is useful. It is one of the good things, that we get from granted by the various .Net libraries and we should not try to do on our own, as the HMACSHA1 is quite a powerful algorithm, providing good hashes.
If, by any chance you are wondering how to get a HMACSHA1, encoded in BASE64, then you are on the right place. The two functions below will show you how:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
Public Function Base64Sha1(inputText As String, Optional secretKey = "") As String Dim asc As Object Dim enc As Object Dim textToHash() As Byte Dim SharedSecretKey() As Byte Dim bytes() As Byte If secretKey = "" Then secretKey = inputText Set asc = CreateObject("System.Text.UTF8Encoding") Set enc = CreateObject("System.Security.Cryptography.HMACSHA1") textToHash = asc.GetBytes_4(inputText) SharedSecretKey = asc.GetBytes_4(secretKey) enc.Key = SharedSecretKey bytes = enc.ComputeHash_2((textToHash)) Base64Sha1 = EncodeBase64(bytes) End Function Private Function EncodeBase64(arrData() As Byte) As String Dim objXML As Object Dim objNode As Object Set objXML = CreateObject("MSXML2.DOMDocument") Set objNode = objXML.createElement("b64") objNode.DataType = "bin.base64" objNode.nodeTypedValue = arrData EncodeBase64 = objNode.text End Function Sub TestMe() Debug.Print Base64Sha1("asdf", "ThisIsTheSecretKey") = "DSmGEC8dUW9xRs+YfAPji59dxCM=" Debug.Print Base64Sha1("asdf") = "qIQmNGgreJRqJroWUUu0MxLq2oo=" Debug.Print Base64Sha1("asdf", "asdf") = "qIQmNGgreJRqJroWUUu0MxLq2oo=" End Sub |
That’s all! Quite ok for Excel.