CryptGetHashParam

Aus API-Wiki
Wechseln zu: Navigation, Suche

Mit der Funktion CryptGetHashParam lassen sich Eigenschaften eines Hash Objektes, nämlich der verwendete Algorithmus, Die Länge des erstellten Hashes, und der Hash Wert selbst ermitteln.

<vb>Declare Function CryptGetHashParam Lib "advapi32.dll" ( _

                ByVal hHash As Long, _
                ByVal dwParam As Long, _
                ByVal pbData As Long, _
                ByRef pdwDataLen As Long, _
                ByVal dwFlags As Long) As Long</vb>

Parameter

hHash

[in] Handle eines mit CryptCreateHash erstellten Hash Objekts.

dwParam

[in] In diesem Parameter wird die Eigenschaft, die ausgelesen werden soll angegeben:
HP_ALGID verwendeter Algorithmus
HP_HASHSIZE Grösse des Hashwertes in Bytes
HP_HASHVAL Hashwert

pbData

[out] In diesem Buffer wird der jeweilige Eigenschaftswert zurückgegeben. Der Buffer muss vor dem Aufruf reserviert werden. Um die benötigte Grösse des Buffers zu ermitteln kann für diesen Parameter NULL übergeben werden, pdwDataLen erhält dann die Anzahl der benötigten Bytes.

pdwDataLen

[in,out] Grösse des in pbData bereitgestellten Buffers in Bytes, nach der Rückkehr enthält diese Variable die tasächlich zurückgegebenen Bytes.

dwFlags

[in] derzeit nicht verwendet, muss 0 sein.

Rückgabe(n)

Bei Erfolg wird ein Wert ungleich 0 zurückgegeben.

Beispiel

<vb>Option Explicit

Private Declare Function CryptAcquireContext Lib "advapi32.dll" _

                Alias "CryptAcquireContextA" ( _
                ByRef phProv As Long, _
                ByRef pszContainer As Any, _
                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, _
                ByVal pbData As Long, _
                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 Long, _
                ByRef pdwDataLen As Long, _
                ByVal dwFlags As Long) As Long


Private Const MS_DEF_PROV As String = _

                   "Microsoft Base Cryptographic Provider v1.0"

Private Const PROV_RSA_FULL As Long = 1 Private Const CRYPT_VERIFYCONTEXT As Long = &HF0000000

Private Const HP_HASHVAL As Long = 2 Private Const HP_HASHSIZE As Long = 4

Const ALG_CLASS_KEY_EXCHANGE As Long = &HA000& Const ALG_CLASS_HASH As Long = &H8000& Const ALG_CLASS_DATA_ENCRYPT As Long = &H6000& Const ALG_CLASS_SIGNATURE As Long = &H2000& Const ALG_TYPE_STREAM As Long = &H800& Const ALG_TYPE_BLOCK As Long = &H600& Const ALG_TYPE_RSA As Long = &H400& Const ALG_TYPE_ANY As Long = 0 Const ALG_SID_RSA_ANY As Long = 0 Const ALG_SID_MD2 As Long = 1 Const ALG_SID_MD4 As Long = 2 Const ALG_SID_MD5 As Long = 3 Const ALG_SID_SHA As Long = 4 Const ALG_SID_SHA_256 As Long = 12 Const ALG_SID_SHA_384 As Long = 13 Const ALG_SID_SHA_512 As Long = 14 Const ALG_SID_RC4 As Long = 1 Const ALG_SID_DES As Long = 1 Const ALG_SID_RC2 As Long = 2 Const ALG_SID_3DES As Long = 3 Const ALG_SID_3DES_112 As Long = 9 Const ALG_SID_AES_128 As Long = 14 Const ALG_SID_AES_192 As Long = 15 Const ALG_SID_AES_256 As Long = 16 Const ALG_SID_AES As Long = 17

'Hash Algorithmen: Enum EnmHashAlgo

   CALG_MD2 = (ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD2)
   CALG_MD4 = (ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD4)
   CALG_MD5 = (ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD5)
   CALG_SHA = (ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_SHA)
   CALG_SHA256 = (ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_SHA_256)
   CALG_SHA384 = (ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_SHA_384)
   CALG_SHA512 = (ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_SHA_512)

End Enum

Private Sub Command1_Click()

   Dim sTestString As String
   Dim btData() As Byte
   Dim hCryptProv As Long
   Dim hHash As Long
   Dim cbHash As Long
   Dim x As Long
   Dim sHash As String
   
   sTestString = "Hello World!"
   
   'in ANSI Bytefolge umwandeln:
   btData = StrConv(sTestString, vbFromUnicode)
   
   'Provider Kontext anfordern:
   If CryptAcquireContext(hCryptProv, ByVal 0, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) = 0 Then
       MsgBox "Kann MS Base Provider nicht finden!", vbExclamation, "Fehler:"
   
   'Hash Objekt erstellen:
   ElseIf CryptCreateHash(hCryptProv, CALG_SHA, 0, 0, hHash) = 0 Then
       MsgBox "Kann Hash Objekt nicht erstellen!", vbExclamation, "Fehler:"
   
   'daten ins Hash Objekt schreiben:
   ElseIf CryptHashData(hHash, VarPtr(btData(0)), UBound(btData) + 1, 0) = 0 Then
       MsgBox "Kann Daten nicht in Hash schreiben!", vbExclamation, "Fehler:"
   
   'Grösse des Hashwertes ermitteln:
   ElseIf CryptGetHashParam(hHash, HP_HASHVAL, 0, cbHash, 0) = 0 Then
       MsgBox "Kann Daten nicht aus Hash auslesen!", vbExclamation, "Fehler:"
   Else
       'Speicherreservieren und Hash abholen:
       ReDim btData(cbHash - 1)
       If CryptGetHashParam(hHash, HP_HASHVAL, VarPtr(btData(0)), cbHash, 0) = 0 Then
           MsgBox "Kann Daten nicht aus Hash auslesen!", vbExclamation, "Fehler:"
       Else
           'Der Hash liegt nun in Byteform vor und wird nun in leserliche HEX Darstellung gebracht:
           For x = 0 To cbHash - 1
               sHash = sHash & Right("0" & Hex(btData(x)), 2)
           Next
           MsgBox "Hash von " & sTestString & " ist: " & sHash, vbInformation, "Erfolgreich:"
       End If
   End If
   If hHash <> 0 Then CryptDestroyHash hHash
   If hCryptProv <> 0 Then CryptReleaseContext hCryptProv, 0

End Sub</vb>