Skip to content

Instantly share code, notes, and snippets.

@jrdmb
Last active June 5, 2020 05:24
Show Gist options
  • Save jrdmb/b81622a2b10449c6be99 to your computer and use it in GitHub Desktop.
Save jrdmb/b81622a2b10449c6be99 to your computer and use it in GitHub Desktop.
VBA Code for SHA-512 hash using mscorlib.dll

Other links to creating SHA-512 hashes in various Windows apps:

The code below is adapted from: http://stackoverflow.com/questions/11394811/compute-sha512-on-vba-excel-2003

64-bit MS Access VBA code to calculate an SHA-512 or SHA-256 hash in VBA.  This requires a VBA reference to the .Net Framework 4.0 mscorlib.dll.  The hashed strings are calculated using calls to encryption methods built into mscorlib.dll.  The calculated hash strings are the same values as those calculated with jsSHA, a Javascript SHA implementation (see https://caligatio.github.io/jsSHA/ for an online calculator and the jsSHA code).

The mscorlib.dll is intended for .Net Framework managed applications, but the stackoverflow.com post showed how it could be used with MS Access VBA.  This technique is not documented anywhere in MS Access documentation that I could find, so the stackoverflow.com post was very helpful in this regard.  

Public Sub to_SHA512()
'Requires a reference to mscorlib 4.0 64-bit
Dim text As Object
Dim SHA512 As Object

Set text = CreateObject("System.Text.UTF8Encoding")
Set SHA512 = CreateObject("System.Security.Cryptography.SHA512Managed")
Set SHA256 = CreateObject("System.Security.Cryptography.SHA256Managed")
'Debug.Print ToBase64String(SHA512.ComputeHash_2((text.GetBytes_4("mypassword"))))
Debug.Print ToHexString(SHA512.ComputeHash_2((text.GetBytes_4("mypassword"))))
End Sub

Public Sub to_SHA256()
'Requires a reference to mscorlib 4.0 64-bit, which is part of the .Net Framework 4.0
Dim text As Object
Dim SHA256 As Object

Set text = CreateObject("System.Text.UTF8Encoding")
Set SHA256 = CreateObject("System.Security.Cryptography.SHA256Managed")
debug.print ToHexString(SHA256.ComputeHash_2((text.GetBytes_4("mypassword")))) 
End Sub

Function ToBase64String(rabyt)

  'Ref: http://stackoverflow.com/questions/1118947/converting-binary-file-to-base64-string
  With CreateObject("MSXML2.DOMDocument")
    .LoadXML "<root />"
    .DocumentElement.DataType = "bin.base64"
    .DocumentElement.nodeTypedValue = rabyt
    ToBase64String = Replace(.DocumentElement.text, vbLf, "")
  End With
End Function

Function ToHexString(rabyt)

  'Ref: http://stackoverflow.com/questions/1118947/converting-binary-file-to-base64-string
  With CreateObject("MSXML2.DOMDocument")
    .LoadXML "<root />"
    .DocumentElement.DataType = "bin.Hex"
    .DocumentElement.nodeTypedValue = rabyt
    ToHexString = Replace(.DocumentElement.text, vbLf, "")
  End With
End Function

#VBA #Encryption #sqlserver #tsql

@TSAFACKM
Copy link

TSAFACKM commented Jun 5, 2020

Hi guys,

Please, I found this code: [VBA SHA-512 using Windows API calls with advapi32.dll, 32 or 64 bit] (https://gist.githubusercontent.com/jermity/b12e26bc1adb38840099/raw/vba_advapi32_64 -bit.vb) which encrypts data on Github. But it only allows encryption and I would like to know how to do the opposite, ie decrypt.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment