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
 
  • First, I used once a simple numeric hash from the common Dictionary library :
VBA Code:
Sub DemoNumHash()
    With CreateObject("Scripting.Dictionary")
        MsgBox .HashVal("money") & vbLf & vbLf & .HashVal("asdf")
    End With
End Sub
  • For crypto it depends on what you exactly need, if you use worksheet cells to store encrypted data, …
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Cool.

I have a question though:

money is giving out 298 while asdf is giving out 1173 - why is it so?
What is actually taking place inside the code?
 
Upvote 0
Okay you can share it for me to have a look. I love to learn new things.
Kelly, I like hashing and encryption too.

It's not something that can be covered properly in this forum, but if you google those terms, there are thousands of videos and webpages explaining how they work, and which are most reliable and secure. After that, when you are ready to use some code, I would google,
VB hash or
VB encryption
and you will get both VB (Visual Basic) and VBA code. VB code is very similar (but ignore VB.Net code, that is very different)

Then it's a case of trying it out. You may have to google more to get things working, but if you're keen, you'll get there.
 
Upvote 0
money is giving out 298 while asdf is giving out 1173 - why is it so?
It's the way it works, maybe more like a SRC than a hashing algorhithm …​
Maybe I have some hashing way but I have to first inspect if it can be converted and without being too slow under VBA …​
 
Upvote 0
In fact I wanna know what you really need : some 'one way encryption' hashing algorithm which can not be decrypted​
or a 'two way encryption' more like a cryptography algorithm ? And an answer to my post #11 …​
 
Upvote 0
In fact I wanna know what you really need : some 'one way encryption' hashing algorithm which can not be decrypted​
or a 'two way encryption' more like a cryptography algorithm ? And an answer to my post #11 …​
What I really need is to understand how things work out with the hashing and/or cryptography algorithms.

I am new to these stuffs so I can't tell for sure whether I needed A or B.

So maybe the two examples would help.
 
Upvote 0
Easy to understand : each character is transformed to another one through an algorithm …​
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,264
Members
449,075
Latest member
staticfluids

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