0

I am trying to create a hash of a string using the UDF BASE64SHA1.

Public Function BASE64SHA1(ByVal sTextToHash As String)

    Dim asc As Object
    Dim enc As Object
    Dim TextToHash() As Byte
    Dim SharedSecretKey() As Byte
    Dim bytes() As Byte
    Const cutoff As Integer = 5

    Set asc = CreateObject("System.Text.UTF8Encoding")
    Set enc = CreateObject("System.Security.Cryptography.HMACSHA1")

    TextToHash = asc.GetBytes_4(sTextToHash)
    SharedSecretKey = asc.GetBytes_4(sTextToHash)
    enc.Key = SharedSecretKey

    bytes = enc.ComputeHash_2((TextToHash))
    BASE64SHA1 = EncodeBase64(bytes)
    BASE64SHA1 = Left(BASE64SHA1, cutoff)

    Set asc = Nothing
    Set enc = Nothing

End Function

Private Function EncodeBase64(ByRef 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

    Set objNode = Nothing
    Set objXML = Nothing

End Function

When I execute the function, however, I receive a #VALUE! error. When I execute a simpler UDF, it executes properly.

The only similar problem I was able to find online is from mrexcel.com, but the problem wasn't resolved.

I am using a Microsoft Excel 365 for Mac v16.56. My macros are enabled. What am I missing?

astromonerd
  • 907
  • 1
  • 15
  • 32
  • 1
    Is MSXML2 available on Mac? I don't think so. – BigBen Jan 14 '22 at 19:48
  • Added the code for the UDF under the link. – astromonerd Jan 14 '22 at 20:03
  • 1
    Just put `? BASE64SHA1(Sheet1.Range("A2").Value)` in the Immediate Window and hit Enter. – BigBen Jan 14 '22 at 20:19
  • From the SO FAQ: [Please do not upload images of code/errors when asking a question.](//meta.stackoverflow.com/q/285551). – Ken White Jan 14 '22 at 20:23
  • 1
    @BigBen The output is `Run-time error 429: ActiveX component can't create object` and the Debug points to`Set asc = CreateObject("System.Text.UTF8Encoding")` – astromonerd Jan 14 '22 at 20:29
  • @BigBen Googling the Debug error points me to this [post](https://stackoverflow.com/questions/28988123/error-in-hash-implementation-in-vba-runtime-error-2146232576-80131700). I'm not able to understand all of it, but is the solution to download and install [Microsoft .NET](https://dotnet.microsoft.com/en-us/download) ? – astromonerd Jan 14 '22 at 20:39
  • 1
    I'm unable to help further (don't have a Mac to test on). But my hunch would be that this would need a significant rewrite to work on Mac. Could be wrong. – BigBen Jan 14 '22 at 20:49

0 Answers0