kelly mort
Well-known Member
- Joined
- Apr 10, 2017
- Messages
- 2,169
- Office Version
- 2016
- Platform
- 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?
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