473,408 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Accessing a hash API

I am trying to make a function to hash passwords with. Here is the
code:
Option Compare Database
Option Explicit

'function declarations
Private Declare Function CryptAcquireContext Lib "advapi32.dll" Alias
"CryptAcquireContextA" (ByRef phProv As Long, ByVal pszContainer As
Any, ByVal pszProvider As Any, ByVal dwProvType As Long, ByVal dwFlags
As Long) As Long
Private Declare Function CryptCreateHash Lib "advapi32.dll" (ByVal
hProv As Long, ByVal Algid As Long, ByVal mhSessionKey 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, ByRef pbData As Any, ByVal
dwDataLen As Long, ByVal dwFlags As Long) As Long
Private Declare Function CryptDestroyHash Lib "advapi32.dll" (ByVal
hHash As Long) As Long
Private Declare Function CryptReleaseContext Lib "advapi32.dll" (ByVal
hProv As Long, ByVal dwFlags As Long) As Long

'hash type constants
Const PROV_RSA_FULL As Long = 1
Const CALG_MD5 As Long = 32771 'ALG_CLASS_HASH Or ALG_TYPE_ANY Or
ALG_SID_MD5
Const ALG_CLASS_HASH As Long = 32768
Const ALG_TYPE_ANY As Long = 0
Const ALG_SID_MD5 As Long = 3

'parameter retrieval types
Const HP_HASHVAL As Long = 2
Const HP_HASHSIZE As Long = 4

'errors
Const ERROR_INVALID_HANDLE As Long = 6
Const ERROR_INVALID_PARAMETER As Long = 87
Const ERROR_MORE_DATA As Long = 234
Const NTE_BAD_FLAGS As Long = &H80090009
Const NTE_BAD_TYPE As Long = &H8009000A
Const NTE_BAD_UID As Long = &H80090001
Const NTE_BAD_HASH As Long = &H80090002

Function Hash(strIn As String) As String

Dim hCryptProv As Long
Dim hHash As Long
Dim dwHashLength As Long
Dim pbHashedData As String

If CryptAcquireContext(hCryptProv, 0&, 0&, PROV_RSA_FULL, 0) = 0
Then GoTo ExitHash 'get a context for hashes
If CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, hHash) = 0 Then
GoTo ExitHash 'create an empty hash
If CryptHashData(hHash, strIn, Len(strIn) + 1, 0) = 0 Then GoTo
ExitHash 'put the password into the hash

dwHashLength = 0
If CryptGetHashParam(hHash, HP_HASHSIZE, 0&, dwHashLength, 0) = 0
Then GoTo ExitHash 'retrieve the length
pbHashedData = String$(dwHashLength, vbNullChar) 'make a string
filled w/ null chars

If CryptGetHashParam(hHash, HP_HASHVAL, pbHashedData,
dwHashLength, 0) = 0 Then GoTo ExitHash 'retrieve the hashed value

ExitHash:
If hHash Then CryptDestroyHash hHash
If hCryptProv Then CryptReleaseContext hCryptProv, 0

End Function
Anyhow, when I try to get the hashed value's length, the function
returns false, indicating an error. I checked the Err.LastDllError in
the debug window, and I got 87 (ERROR_INVALID_PARAMETER). Can someone
tell me what I did wrong?

Jul 25 '07 #1
1 5484
On Jul 25, 5:40 pm, TheCite <ceo....@hotmail.comwrote:
I am trying to make a function to hash passwords with. Here is the
code:
Option Compare Database
Option Explicit

'function declarations
Private Declare Function CryptAcquireContext Lib "advapi32.dll" Alias
"CryptAcquireContextA" (ByRef phProv As Long, ByVal pszContainer As
Any, ByVal pszProvider As Any, ByVal dwProvType As Long, ByVal dwFlags
As Long) As Long
Private Declare Function CryptCreateHash Lib "advapi32.dll" (ByVal
hProv As Long, ByVal Algid As Long, ByVal mhSessionKey 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, ByRef pbData As Any, ByVal
dwDataLen As Long, ByVal dwFlags As Long) As Long
Private Declare Function CryptDestroyHash Lib "advapi32.dll" (ByVal
hHash As Long) As Long
Private Declare Function CryptReleaseContext Lib "advapi32.dll" (ByVal
hProv As Long, ByVal dwFlags As Long) As Long

'hash type constants
Const PROV_RSA_FULL As Long = 1
Const CALG_MD5 As Long = 32771 'ALG_CLASS_HASH Or ALG_TYPE_ANY Or
ALG_SID_MD5
Const ALG_CLASS_HASH As Long = 32768
Const ALG_TYPE_ANY As Long = 0
Const ALG_SID_MD5 As Long = 3

'parameter retrieval types
Const HP_HASHVAL As Long = 2
Const HP_HASHSIZE As Long = 4

'errors
Const ERROR_INVALID_HANDLE As Long = 6
Const ERROR_INVALID_PARAMETER As Long = 87
Const ERROR_MORE_DATA As Long = 234
Const NTE_BAD_FLAGS As Long = &H80090009
Const NTE_BAD_TYPE As Long = &H8009000A
Const NTE_BAD_UID As Long = &H80090001
Const NTE_BAD_HASH As Long = &H80090002

Function Hash(strIn As String) As String

Dim hCryptProv As Long
Dim hHash As Long
Dim dwHashLength As Long
Dim pbHashedData As String

If CryptAcquireContext(hCryptProv, 0&, 0&, PROV_RSA_FULL, 0) = 0
Then GoTo ExitHash 'get a context for hashes
If CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, hHash) = 0 Then
GoTo ExitHash 'create an empty hash
If CryptHashData(hHash, strIn, Len(strIn) + 1, 0) = 0 Then GoTo
ExitHash 'put the password into the hash

dwHashLength = 0
If CryptGetHashParam(hHash, HP_HASHSIZE, 0&, dwHashLength, 0) = 0
Then GoTo ExitHash 'retrieve the length
pbHashedData = String$(dwHashLength, vbNullChar) 'make a string
filled w/ null chars

If CryptGetHashParam(hHash, HP_HASHVAL, pbHashedData,
dwHashLength, 0) = 0 Then GoTo ExitHash 'retrieve the hashed value

ExitHash:
If hHash Then CryptDestroyHash hHash
If hCryptProv Then CryptReleaseContext hCryptProv, 0

End Function
Anyhow, when I try to get the hashed value's length, the function
returns false, indicating an error. I checked the Err.LastDllError in
the debug window, and I got 87 (ERROR_INVALID_PARAMETER). Can someone
tell me what I did wrong?
This newsgroup is for questions about the Access database program sold
by Microsoft. Anyway,

In CryptGetHashParam you should use ByRef instead of ByVal here:

ByRef dwDataLen As Long

because CryptGetHashParam needs to retrieve this value when pdwDataLen
(dwHashLength) is too small. ByVal forces the 0 value to remain
static.

Note: Be sure to check out the entry for CryptGetHashParam in the
Win32 SDK Reference Help.

James A. Fortune
CD********@FortuneJames.com

Jul 25 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: sachin_mzn | last post by:
Hi, Why I am not getting any run time error while accessing a freed memory in following code. This is printing h in std output. #include<stdio.h> main() { char* buffer = (char*)malloc(6);...
1
by: Stephan Zaubzer | last post by:
Hi I relatively new to C# and at the moment I am having troubles accessing com objects within C#. I am working in VS.net. I add the com library I want to access to my references. Accessing...
2
by: EP | last post by:
I'm looking for a method by which to access Windows files metadata and have not been able to find anything in the standard modules or via Google - what is the standard approach? Shamefully I...
12
by: Arash Partow | last post by:
Hi all, I've ported various hash functions to python if anyone is interested: def RSHash(key): a = 378551 b = 63689 hash = 0
38
by: djhulme | last post by:
Hi, I'm using GCC. Please could you tell me, what is the maximum number of array elements that I can create in C, i.e. char* anArray = (char*) calloc( ??MAX?? , sizeof(char) ) ; I've...
21
by: Hallvard B Furuseth | last post by:
Is the code below valid? Generally a value must be accessed through the same type it was stored as, but there is an exception for data stored through a character type. I'm not sure if that...
13
by: Iris83 | last post by:
Hi, I have a question about converting some of the data in my dataset but leave some data the way it is. I have a hash and if the key is present and the Value of the Hash equals B it should...
2
by: gurmeet07 | last post by:
hey guys i m new to .net and facing one problem on click of a button (saving the data to the hash table) like this : there are 7 textboxes and first textbox1.text is the key for hash table n...
0
by: gurmeet07 | last post by:
hi guys if i m navigating among the records saved in hash table...........i.e. first record , previous record , next record and last record as navigating buttons can be there on a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.