[COLOR=blue]
Option Explicit
' =====================================================================================
' All declares necessary for MS implementation of RSA MD5<function> in cryptdll library
' Requires XP/2000/2003
Private Type MD5_CTX
i(1 To 2) As Long
buf(1 To 4) As Long
inp(1 To 64) As Byte
digest(1 To 16) As Byte
End Type
Private Declare Sub MD5Init Lib "cryptdll" (Context As MD5_CTX)
Private Declare Sub MD5Update Lib "cryptdll" (Context As MD5_CTX, ByVal strInput As String, ByVal lLen As Long)
Private Declare Sub MD5Final Lib "cryptdll" (Context As MD5_CTX)
' =====================================================================================
' =====================================================================================
' All declares neccessary for CryptoAPI version of MD5 example
Private Const MS_DEFAULT_PROVIDER As String = "Microsoft Base Cryptographic Provider v1.0"
Private Const PROV_RSA_FULL As Long = 1
Private Const CALG_MD5 As Long = &H8003&
Private Const CRYPT_VERIFYCONTEXT = &HF0000000
Private Const HP_HASHVAL As Long = 2
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 CryptHashData Lib "advapi32.dll" (ByVal hHash As Long, ByVal pbData As String, 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, ByVal pbData As String, pdwDataLen As Long, ByVal dwFlags As Long) As Long
' =====================================================================================
Private Sub Command1_Click()
Debug.Print HashVersion1("secret")
Debug.Print HashVersion2("secret")
End Sub
' RSA MD5 Variant
Private Function HashVersion1(ByVal strPassword As String) As String
Dim myContext As MD5_CTX
MD5Init myContext
MD5Update myContext, strPassword, Len(strPassword)
MD5Final myContext
HashVersion1 = StrConv(myContext.digest, vbUnicode)
End Function
' CryptoAPI variant
' Lifted from my encryption/decryption code in thread
Private Function HashVersion2(ByVal strPassword As String) As String
Dim hProv As Long
Dim strprovider As String
Dim hHash As Long
Dim strHash As String
' Grab an RSA-based cryptoapi context using Microsoft's base provider
strprovider = MS_DEFAULT_PROVIDER & vbNullChar
Call CryptAcquireContext(hProv, vbNullString, strprovider, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) ' final param could be 0&
' Generate a hash of the password
Call CryptCreateHash(hProv, CALG_MD5, 0, 0, hHash)
Call CryptHashData(hHash, strPassword, Len(strPassword), 0)
strHash = Space(16) ' We're shortcutting, since we know we are using MD5 we know we are getting 16 bytes back
Call CryptGetHashParam(hHash, HP_HASHVAL, strHash, 16, 0)
HashVersion2 = strHash
CryptReleaseContext hProv, 0&
End Function
[/color]