Understanding hashing algorithms in VBA and how to avoid collisions.

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
I found this code at stackoverflow.com and I am falling in love with the code. I am now learning about the hashing algorithms in VBA and I wish someone with a deeper understanding could help guide me and advise me on a few things:
1. Is there a possibility that two or more different strings or input will produce same output?
2. If the above can occur, is there a way to prevent that and how?
Code:
Sub tester()
    Dim myInput$
    myInput = "money"
    'MsgBox hash4(myInput)
    MsgBox hash12(myInput)
End Sub


Function hash12(s$)
    Dim l%, l3%, s1$, s2$, s3$
    l = Len(s)
    l3 = Int(l / 3)
    s1 = Mid(s, 1, l3)
    s2 = Mid(s, l3 + 1, l3)
    s3 = Mid(s, 2 * l3 + 1)
    hash12 = hash4(s1) + hash4(s2) + hash4(s3)
End Function

Function hash4(txt)
    Dim x&, mask, i, j, nC, crc%, c$
    crc = &HFFFF
    For nC = 1 To Len(txt)
        j = Asc(Mid(txt, nC))
        crc = crc Xor j
        For j = 1 To 8
            mask = 0
            If crc / 2 <> Int(crc / 2) Then mask = &HA001
            crc = Int(crc / 2) And &H7FFF: crc = crc Xor mask
        Next j
    Next nC
    
    c = Hex$(crc)

    While Len(c) < 4
      c = "0" & c
    Wend
    
    hash4 = c
End Function
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Any hashing algorithm can create duplicate hashes for different input values. The best algorithms manage to minimize the number of collisions, but it's always a possibility. Typically a website might store the hash value of a password instead of the password itself, to prevent saving the password in a readable form. But if the hashing algorithm is weak, then there could actually be many passwords with the same hash value, and the security on that site is weak too.

If you're using the hash value for something else, like creating a lookup key in an internal table, there are several options. One of which is chaining. If multiple records generate the same hash, create a list of matching records, and if you create a hash that has multiple records, go down the list until you find the one you want.

Anyway, this is a huge subject, too big to really cover here. Google "Hashing algorithms collisions" and you should find a lot of links.
 
Upvote 0
@Eric W,

Thanks for the quick and timely reply.

I have done a few readings already and have come across a few examples.

Example :

Code:
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


This code has appeared in multiple searches that I have conducted.

My question now is, can this be implemented in Excel VBA and how does it work?

A little bit of expert advise will be great.
 
Upvote 0
It appears to be standard VBA code, so it should work. It relies on some system libraries, which may or may not be present on your PC. To use it from a worksheet,

Excel Formula:
=Base64Sha1("clear text to be hashed","hash key")

or from another procedure:

HashValue = Base64Sha1("clear text to be hashed","hash key")
 
Upvote 0
It appears to be standard VBA code, so it should work. It relies on some system libraries, which may or may not be present on your PC. To use it from a worksheet,

Excel Formula:
=Base64Sha1("clear text to be hashed","hash key")

or from another procedure:

HashValue = Base64Sha1("clear text to be hashed","hash key")

I am getting "automation error"

So how do I get those libraries on my pc?
 
Upvote 0
Hi,​
maybe using some 'hashing algorithm' in pure VBA rather than using any external library …​
 
Upvote 0

I have only some cryptographing samples rather than hashing ones …​
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top