Advertisement
areyesram

VB6 Hash

Jan 17th, 2024
1,707
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.  
  3. Private Const CALG_SHA_256 As Long = 0x0000800C
  4. Private Const HP_HASHVAL As Long = 2
  5. Private Const PROV_RSA_FULL As Long = 1
  6.  
  7. Private Declare Function CryptAcquireContext Lib "advapi32.dll" Alias "CryptAcquireContextA" _
  8.     (ByRef phProv As Long, ByVal pszContainer As String, ByVal pszProvider As String, _
  9.     ByVal dwProvType As Long, ByVal dwFlags As Long) As Long
  10.  
  11. Private Declare Function CryptCreateHash Lib "advapi32.dll" _
  12.     (ByVal hProv As Long, ByVal Algid As Long, ByVal hKey As Long, ByVal dwFlags As Long, _
  13.     ByRef phHash As Long) As Long
  14.  
  15. Private Declare Function CryptHashData Lib "advapi32.dll" _
  16.     (ByVal hHash As Long, ByVal pbData As String, ByVal dwDataLen As Long, ByVal dwFlags As Long) As Long
  17.  
  18. Private Declare Function CryptGetHashParam Lib "advapi32.dll" _
  19.     (ByVal hHash As Long, ByVal dwParam As Long, ByRef pbData As Any, ByRef pdwDataLen As Long, _
  20.     ByVal dwFlags As Long) As Long
  21.  
  22. Private Declare Function CryptReleaseContext Lib "advapi32.dll" _
  23.     (ByVal hProv As Long, ByVal dwFlags As Long) As Long
  24.  
  25. Public Function CalculateSHA256Hash(ByVal inputString As String) As String
  26.     Dim hCryptProv As Long
  27.     Dim hHash As Long
  28.     Dim hashValue() As Byte
  29.     Dim i As Long
  30.  
  31.     ' Acquire a cryptographic context
  32.    If CryptAcquireContext(hCryptProv, vbNullString, vbNullString, PROV_RSA_FULL, 0) = 0 Then
  33.         MsgBox "Error acquiring cryptographic context."
  34.         Exit Function
  35.     End If
  36.  
  37.     ' Create a hash object
  38.    If CryptCreateHash(hCryptProv, CALG_SHA_256, 0, 0, hHash) = 0 Then
  39.         MsgBox "Error creating hash object."
  40.         CryptReleaseContext hCryptProv, 0
  41.         Exit Function
  42.     End If
  43.  
  44.     ' Hash the input data
  45.    If CryptHashData(hHash, ByVal inputString, Len(inputString), 0) = 0 Then
  46.         MsgBox "Error hashing data."
  47.         CryptReleaseContext hCryptProv, 0
  48.         CryptDestroyHash hHash
  49.         Exit Function
  50.     End If
  51.  
  52.     ' Get the hash value
  53.    ReDim hashValue(31)
  54.     If CryptGetHashParam(hHash, HP_HASHVAL, hashValue(0), 32, 0) = 0 Then
  55.         MsgBox "Error retrieving hash value."
  56.         CryptReleaseContext hCryptProv, 0
  57.         CryptDestroyHash hHash
  58.         Exit Function
  59.     End If
  60.  
  61.     ' Release resources
  62.    CryptDestroyHash hHash
  63.     CryptReleaseContext hCryptProv, 0
  64.  
  65.     ' Convert the byte array to a hex string
  66.    Dim hexHash As String
  67.     For i = 0 To UBound(hashValue)
  68.         hexHash = hexHash & Right("0" & Hex(hashValue(i)), 2)
  69.     Next i
  70.  
  71.     CalculateSHA256Hash = hexHash
  72. End Function
  73.  
  74. Private Sub Form_Load()
  75.     ' Example usage
  76.    MsgBox "SHA-256 Hash: " & CalculateSHA256Hash("Hello, World!")
  77. End Sub
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement