473,788 Members | 2,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 CryptAcquireCon text Lib "advapi32.d ll" Alias
"CryptAcquireCo ntextA" (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.d ll" (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.d ll" (ByVal hHash
As Long, ByVal pbData As String, ByVal dwDataLen As Long, ByVal
dwFlags As Long) As Long
Private Declare Function CryptGetHashPar am Lib "advapi32.d ll" (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 CryptDestroyHas h Lib "advapi32.d ll" (ByVal
hHash As Long) As Long
Private Declare Function CryptReleaseCon text Lib "advapi32.d ll" (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_H ANDLE As Long = 6
Const ERROR_INVALID_P ARAMETER 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 CryptAcquireCon text(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(h Hash, strIn, Len(strIn) + 1, 0) = 0 Then GoTo
ExitHash 'put the password into the hash

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

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

ExitHash:
If hHash Then CryptDestroyHas h hHash
If hCryptProv Then CryptReleaseCon text 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.LastDllErro r in
the debug window, and I got 87 (ERROR_INVALID_ PARAMETER). Can someone
tell me what I did wrong?

Jul 25 '07 #1
1 5517
On Jul 25, 5:40 pm, TheCite <ceo....@hotmai l.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 CryptAcquireCon text Lib "advapi32.d ll" Alias
"CryptAcquireCo ntextA" (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.d ll" (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.d ll" (ByVal hHash
As Long, ByVal pbData As String, ByVal dwDataLen As Long, ByVal
dwFlags As Long) As Long
Private Declare Function CryptGetHashPar am Lib "advapi32.d ll" (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 CryptDestroyHas h Lib "advapi32.d ll" (ByVal
hHash As Long) As Long
Private Declare Function CryptReleaseCon text Lib "advapi32.d ll" (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_H ANDLE As Long = 6
Const ERROR_INVALID_P ARAMETER 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 CryptAcquireCon text(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(h Hash, strIn, Len(strIn) + 1, 0) = 0 Then GoTo
ExitHash 'put the password into the hash

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

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

ExitHash:
If hHash Then CryptDestroyHas h hHash
If hCryptProv Then CryptReleaseCon text 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.LastDllErro r 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 CryptGetHashPar am you should use ByRef instead of ByVal here:

ByRef dwDataLen As Long

because CryptGetHashPar am 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 CryptGetHashPar am in the
Win32 SDK Reference Help.

James A. Fortune
CD********@Fort uneJames.com

Jul 25 '07 #2

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

Similar topics

14
1645
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); strcpy(buffer,"hello");
1
1445
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 classes exported in this com interfaces will throw a System.Security.SecurityException. (System.Security.Permissions.SecurityPermission) Status of failed permission: <IPermission class="System.Security.Permissions.SecurityPermission,
2
8802
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 really do not understand Windows file system - e.g. is properties metadata attached to the file? if I change that metadata do I change the file's hash? how is the metadata structured? or is the "properties" metadata simply derived upon access? ...
12
7019
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
3015
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 managed to create arrays using DOUBLE data types, but when I try to access the array, the compiler complains that the number is not an INT, i.e.
21
3908
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 applies in this case though: #include <limits.h> unsigned foo(void) { static const union { unsigned char str;
13
1798
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 convert these number but if B is not present the data should remain the same. my output is that all the data is converted not in the only the cases were B is present. This is the script i have
2
1211
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 all other textbox.text are values which are stored in an object type array........... object obj = { textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text, textBox6.Text }; ht.Add(textBox1.Text,obj);
0
798
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 form.................. if i want to access the first key , previous key , next key and last key from the hash table how to access that ?????? and then accordingly record will be displayed.................... i have tried like : ICollection key =...
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10366
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10173
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8993
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3674
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.