title: Base64 HMAC SHA1 String in VBA
body: <p>I'm trying to convert an ASP/VBScript OAuth library to VBA. One of the challenges is this line of code:</p>
  
  <pre><code>Get_Signature = b64_hmac_sha1(strSecret, strBaseSignature)
  </code></pre>
  
  <p>This function, b64_hmac_sha1 is actually a function contained in a JavaScript library. It appears to me that calling a JavaScript function from VBA is fairly impractical.</p>
  
  <p>Because I know so little about encryption, it's not even clear to me what this b64_hmac_sha1 function does. Is HMAC SHA1 different from SHA1?</p>
  
  <p>I half suspect I might be able to find some VBA code online to do what I need to do if I just understood what this function is actually doing. If I do not find an existing function, I could possibly write one that would use the .NET Cryptography library (you can actually call the .NET cryptography libraries from VBA if you know how).</p>
  
  <p>I'm not looking for someone to convert this JavaScript to VBA. I'm only trying to understand what it is that this b64_hmac_sha1 function is outputting so I can try to find ways to achieve the same output in VBA if possible.</p>
  
  <p>A copy of this JavaScript library is visible on this website. You'll have to scroll down past the VBScript to the JavaScript section.
  <a href="http://solstice.washington.edu/solstice/ASP_Signing_REST_Example" rel="noreferrer">http://solstice.washington.edu/solstice/ASP_Signing_REST_Example</a></p>
  
  <p><strong>Edit1:</strong><br>
  OK, so here's the functions I ended up writing and using:  </p>
  
  <pre><code>Public Function Base64_HMACSHA1(ByVal sTextToHash As String, ByVal sSharedSecretKey As String)
  
      Dim asc As Object, enc As Object
      Dim TextToHash() As Byte
      Dim SharedSecretKey() As Byte
      Set asc = CreateObject("System.Text.UTF8Encoding")
      Set enc = CreateObject("System.Security.Cryptography.HMACSHA1")
  
      TextToHash = asc.Getbytes_4(sTextToHash)
      SharedSecretKey = asc.Getbytes_4(sSharedSecretKey)
      enc.Key = SharedSecretKey
  
      Dim bytes() As Byte
      bytes = enc.ComputeHash_2((TextToHash))
      Base64_HMACSHA1 = EncodeBase64(bytes)
      Set asc = Nothing
      Set enc = Nothing
  
  End Function
  
  Private Function EncodeBase64(ByRef arrData() As Byte) As String
  
      Dim objXML As MSXML2.DOMDocument
      Dim objNode As MSXML2.IXMLDOMElement
  
      Set objXML = New MSXML2.DOMDocument
  
      ' byte array to base64
      Set objNode = objXML.createElement("b64")
      objNode.DataType = "bin.base64"
      objNode.nodeTypedValue = arrData
      EncodeBase64 = objNode.Text
  
      Set objNode = Nothing
      Set objXML = Nothing
  
  End Function
  </code></pre>
  
  <p>Using this function:</p>
  
  <pre><code>Debug.Print Base64_HMACSHA1("abc", "123")
  VAsMU9SSWDe9krP3Gr56nXC2dsQ=
  </code></pre>