Click here to Skip to main content
13,354,885 members (72,467 online)
Click here to Skip to main content
Add your own
alternative version


42 bookmarked
Posted 22 Jul 2005

Hashing using the Win32 Crypto API

, 22 Jul 2005
Rate this:
Please Sign up or sign in to vote.
How to make hashing using the Win32 Crypto API.


Hashing is one of the basic and often used features when working with crypto systems. It is used widely and can be used with different purposes. When a clear text is hashed, a relative unique “finger print” (the hash value) of the clear text is made. The hash value will often be shorter than the text which has been hashed. This will of course introduce some contradictions to the uniqueness that must be avoided as much as possible when making the (one-way) hashing algorithm. This is a problem, which can be minimized (but impossible to eliminate) mathematically only. This is one of the main reasons why you should not make a hashing algorithm yourself unless you completely master the mathematics behind. A rule of thumb is that it should be easy to evaluate H (x1) but difficult to find x1 (and x2).

H (x1) = H (x2), x1 != x2

Any contradictions to the uniqueness are basically a security risk because it will make it easier to find a text that will match the hashed value. When using the hash value to store passwords, it is even more clear how the disadvantages of the contradictions can be abused.

Birthday attack

A birthday attack is a type of cryptographic attack which exploits the mathematics behind the birthday paradox, making use of a space-time tradeoff. Specifically, if a function yields any of n different outputs with equal probability and n is sufficiently large, then after evaluating the function for about 1.2Ön different arguments, we expect to have found a pair of different arguments x1 and x2 with H (x1) = H (x2), known as a collision. If the outputs of the function are distributed unevenly, then a collision can be found even faster.

The paradox itself

The birthday paradox states that if there are 23 people in a room then there is a chance of more than 50% that at least two of them will have the same birthday. This means that a higher probability applies to a typical school class size of thirty, where the 'paradox' is often cited. For 60 or more people, the probability is greater than 99%. This is not a paradox in the sense of leading to a logical contradiction; it is a paradox in the sense that it is a mathematical truth that contradicts common intuition. Most people estimate that the chance is much lower than 50:50. Calculating this probability (and related ones) is the birthday problem. The mathematics behind it has been used to devise a well-known cryptographic attack named the birthday attack.

  1. H (x) outputs k bit.
  2. Choose x randomly and evaluate H (x) t times then.
  3. x1 and x2 => h(x1) = h(x2) where x1 != x2.
  4. It can then be expected that t is about 2^k/2.

When t(t-1)/2 pairs each with the probability of 2^-k, the probability for a collision will be:

(t/(t-1)) / (2^(k+1)) = t^2 / 2^k
t^2 = 2^k <=> T = sqr (2^k) = 2^k/2

To avoid this attack, the output length of the hash function used for a signature scheme can be chosen large enough so that the birthday attack becomes computationally infeasible, i.e. about twice as large as needed to prevent an ordinary brute force attack. In cryptanalysis, a brute force attack is a method of defeating a cryptographic scheme by trying a large number of possibilities.

Practical example

At a UNIX system a hashing value is generated of the users password, which is stored into the local system database / the file system. Every time the user enters the password it generates a hash and compares it to the local stored hash value. If these two matches, the user is authenticated otherwise the password is incorrect. With a badly implemented hashing algorithm, it is easier to find another password that will match the exact same hash value. In practice this means that more than one password can be used for the user accounts.

This is why it is very important to choose an aggressive cryptographic hashing algorithm and maintain a high complexity of passwords, as well as a short lifecycle of them to ensure that they are changed regularly. When choosing the time of the lifecycle, it is important that the lifecycle is shorter than the time it takes to find another value that matches the stored hash value.

Dictionary attack

A well-known attack to such systems is a dictionary attack. When performing a dictionary attack, the intruder is iterating through a list containing a large amount of words, e.g., a dictionary. The iteration will stop (or maybe just log) when it finds a word that matches the hash value. When the last word has been tried the process will terminate or it will start over with another user account. Different kinds of cycles have further been implemented to avoid the intruder detection being activated, when the attack is made online.

H (x1 || Ru) != H (x2), x1 != x2

Because of the risks of such an attack, most UNIX systems do not make a hash value based on the password only. Normally they include a random chosen (user specific) value, which is concatenated with the entered password where the random value is stored locally on the system and is changed regularly. This prevents offline dictionary attacks.

Using the code

The Secure Storage component is made like a MFC application. The component is encapsulating the hashing functionality in the Win32 Crypto API, which is placed in separated source and header files (the crypto.cpp and crypto.h files).

Sample screenshot

The functionality can be tried using the graphical user interface where clear text can be entered and a hash value is generated on behalf of it. The hash value will afterwards be shown in the disabled textbox.

void make_hash(const char * data, BYTE ** hash, long * len)
 HCRYPTPROV hProv = 0;
 HCRYPTHASH hHash = 0;
 BYTE *pbHash = NULL;
 DWORD dwHashLen;

 BYTE * pbBuffer = NULL;
 DWORD dwCount;
 unsigned long bufLen = 0;

 if(!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0)) {
 if(!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) {

 bufLen = strlen(data);

 pbBuffer = (BYTE*)malloc(bufLen +1);
 memset(pbBuffer, 0, bufLen +1);

 for(i = 0 ; i < bufLen ; i++) {
  pbBuffer[i] = (BYTE)data[i];

 if(!CryptHashData(hHash, pbBuffer, bufLen, 0)) {

 dwCount = sizeof(DWORD);
 if(!CryptGetHashParam(hHash, HP_HASHSIZE, (BYTE *)&dwHashLen, &dwCount, 0)) {
 if((pbHash = (unsigned char*)malloc(dwHashLen)) == NULL) {

 memset(pbHash, 0, dwHashLen);

 if(!CryptGetHashParam(hHash, HP_HASHVAL, pbHash, &dwHashLen, 0)) {

 *hash = pbHash;
 *len = dwHashLen;

 if(hHash) CryptDestroyHash(hHash);
 if(hProv) CryptReleaseContext(hProv,0);

When making the hash value programmatically the method as outlined above is used. It takes three arguments; the data that has to be hashed (the clear text), a byte pointer initially set to NULL, which will be pointing at the hash value, and finally the length of the hash value.

    BYTE * hash = NULL;
    long len = 0;
    make_hash("Text to be hashed", &hash, &len);

When showing the hash value it can be cast to a char pointer (char *) but this have the disadvantages that not all characters are shown as is. A better way to show the hash value is to stream it to a file, which can be read afterwards.

void write_file(const char * filename, unsigned char * data, long length)
    FILE * fp = fopen(filename, "w+b");
    if (fp) {
        fwrite(data, 1, length, fp);

Please notice that the file is being written as binary. The reason for this is that we are writing binary data to the file.

Points of interest

Using the Win32 Crypto API, please notice that it might not necessary compile as is. This is because a pre-processor definition is missing in the project.

#define _WIN32_WINNT 0x0400

When defining the above globally to the project, it will be possible to compile code that is using the Crypto API. My suggestion is to place the definition in the StdAfx.h file.


Get a PDF Version of the article here. Please notice that the file has been renamed so it has an .xml extension instead. Right click at the link and choose "Save destination as..." to get the file. Rename the file back to .pdf and open it using Acrobat Reader.



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Technical Lead
Denmark Denmark
I'm a technical lead and software architect, who holds a master's degree from Aarhus University, Denmark. I have more than 10 yrs of commercial experience and my professionalism has been confirmed by IEEE with my elevation to Senior Member.

Support & help!forum/nielsen-tools-support

You may also be interested in...


Comments and Discussions

QuestionCryptCreateHash(hProv, CALG_SHA_256, 0, 0, &hHash) ... it is failing giving error as " NTE_BAD_ALGID" .... Pin
Member 97983789-Aug-14 3:42
memberMember 97983789-Aug-14 3:42 
GeneralYour Code is Broken Pin
deusprogrammer8-Jun-11 12:52
memberdeusprogrammer8-Jun-11 12:52 
GeneralNull terminate the hash before display Pin
OllieBrown23-Mar-07 8:01
memberOllieBrown23-Mar-07 8:01 
GeneralRe: Null terminate the hash before display Pin
Jessn25-Mar-07 23:09
memberJessn25-Mar-07 23:09 
GeneralComparing the hash Pin
vgandhi25-Jan-07 1:26
membervgandhi25-Jan-07 1:26 
AnswerRe: Comparing the hash Pin
Jessn15-May-09 5:40
memberJessn15-May-09 5:40 
GeneralRe: Comparing the hash Pin
Member 448141821-Jul-10 4:17
memberMember 448141821-Jul-10 4:17 
AnswerRe: Comparing the hash Pin
Jessn22-Jul-10 22:32
memberJessn22-Jul-10 22:32 
GeneralRe: Comparing the hash Pin
Member 448141822-Jul-10 23:10
memberMember 448141822-Jul-10 23:10 
QuestioncryptoAcquireContext() on windows 98 Pin
rajeevktripathi9-Feb-06 3:50
memberrajeevktripathi9-Feb-06 3:50 
AnswerRe: cryptoAcquireContext() on windows 98 Pin
Jessn9-Feb-06 4:12
memberJessn9-Feb-06 4:12 
Generalcant compile win32 crypto API Pin
adeelg6-Dec-05 20:12
memberadeelg6-Dec-05 20:12 
GeneralRe: cant compile win32 crypto API [modified] Pin
Jessn10-Jan-06 23:37
memberJessn10-Jan-06 23:37 
GeneralReferences Pin
Jessn13-Nov-05 5:17
memberJessn13-Nov-05 5:17 
Generalthis is crap !!! Pin
Anonymous28-Sep-05 17:31
sussAnonymous28-Sep-05 17:31 
AnswerRe: this is crap !!! Pin
T1TAN30-Sep-05 12:15
memberT1TAN30-Sep-05 12:15 
GeneralRe: this is crap !!! Pin
Anonymous2-Oct-05 15:38
sussAnonymous2-Oct-05 15:38 
GeneralRe: this is crap !!! Pin
T1TAN3-Oct-05 0:14
memberT1TAN3-Oct-05 0:14 
GeneralRe: this is crap !!! Pin
Jessn13-Nov-05 4:42
memberJessn13-Nov-05 4:42 
GeneralPerf implications of CAPI Pin
Umut Alev25-Jul-05 22:29
memberUmut Alev25-Jul-05 22:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 22 Jul 2005
Article Copyright 2005 by Jessn
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid