hi there,
I found this code on your forum for which I am glad because I can use it pretty well IF there's a way to alter it and get a 32-character hashcode instead of 16! Anybody knows what's wrong here?
Thnx
------------------------------------------------------------------------------------------------------------
Working Code:
----------------------------------------------------------------------------------------------
RETURNS:
kÍF!ÓsÊÞNƒ&'´ö
16
I found this code on your forum for which I am glad because I can use it pretty well IF there's a way to alter it and get a 32-character hashcode instead of 16! Anybody knows what's wrong here?
Thnx
------------------------------------------------------------------------------------------------------------
Working Code:
Code:
Private Declare Function CryptAcquireContext Lib "advapi32.dll" _ Alias "CryptAcquireContextA" ( _ ByRef phProv As Long, _ ByVal pszContainer As String, _ ByVal pszProvider As String, _ ByVal dwProvType As Long, _ ByVal dwFlags As Long) As Long Private Declare Function CryptReleaseContext Lib "advapi32.dll" ( _ ByVal hProv As Long, _ ByVal dwFlags As Long) As Long Private Declare Function CryptCreateHash Lib "advapi32.dll" ( _ ByVal hProv As Long, _ ByVal Algid As Long, _ ByVal hKey As Long, _ ByVal dwFlags As Long, _ ByRef phHash As Long) As Long Private Declare Function CryptDestroyHash Lib "advapi32.dll" ( _ ByVal hHash As Long) As Long Private Declare Function CryptHashData Lib "advapi32.dll" ( _ ByVal hHash As Long, _ pbData As Any, _ ByVal dwDataLen As Long, _ ByVal dwFlags As Long) As Long Private Declare Function CryptGetHashParam Lib "advapi32.dll" ( _ ByVal hHash As Long, _ ByVal dwParam As Long, _ pbData As Any, _ pdwDataLen As Long, _ ByVal dwFlags As Long) As Long Private Const PROV_RSA_FULL As Long = 1 Private Const ALG_CLASS_HASH = 32768 Private Const ALG_TYPE_ANY = 0 Private Const ALG_SID_MD2 = 1 Private Const ALG_SID_MD4 = 2 Private Const ALG_SID_MD5 = 3 Private Const ALG_SID_SHA1 = 4 Enum HashAlgorithm2 md2 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD2 md4 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD4 MD5 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD5 SHA1 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_SHA1 End Enum 'The other block of code that has the delcare statements Private Const HP_HASHVAL = 2 Private Const HP_HASHSIZE = 4 Private Const CRYPT_VERIFYCONTEXT As Long = &HF0000000 Function HashString(ByVal Str As String, Optional ByVal Algorithm As HashAlgorithm = MD5) As String '' THIS IS THE PASSWORD CRYPTO MODULE Dim hCtx As Long Dim hHash As Long Dim lRes As Long Dim lLen As Long Dim lIdx As Long Dim abData() As Byte lRes = CryptAcquireContext(hCtx, vbNullString, _ vbNullString, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) If lRes <> 0 Then lRes = CryptCreateHash(hCtx, Algorithm, 0, 0, hHash) If lRes <> 0 Then lRes = CryptHashData(hHash, ByVal Str, Len(Str), 0) If lRes <> 0 Then lRes = CryptGetHashParam(hHash, HP_HASHSIZE, lLen, 4, 0) If lRes <> 0 Then ReDim abData(0 To lLen - 1) lRes = CryptGetHashParam(hHash, HP_HASHVAL, abData(0), lLen, 0) If lRes <> 0 Then HashString = StrConv(abData, vbUnicode) End If End If End If CryptDestroyHash hHash End If End If CryptReleaseContext hCtx, 0 If lRes = 0 Then Err.Raise Err.LastDllError End Function
Code:
Sub test() a = "test" Debug.Print HashString(a) Debug.Print Len(HashString(a)) End Sub
kÍF!ÓsÊÞNƒ&'´ö
16
Comment