// Yuri Zarechny for News2News (c) 2007
#include "stdafx.h"
#include "windows.h"
#include "conio.h"
#include "wincrypt.h"
char Buf [256];
DWORD BufSize;
LPSTR CreateHash(LPSTR tohash, ALG_ID alg)
{
HCRYPTPROV hProv;
HCRYPTHASH hash;
if (CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT))
{
if (CryptCreateHash(hProv, alg, 0, 0, &hash))
{
if (CryptHashData(hash, (BYTE *)&tohash, sizeof(tohash), 0))
{
ZeroMemory(&Buf, sizeof(Buf));
BufSize = sizeof(Buf);
if (!CryptGetHashParam(hash, HP_HASHVAL,
(BYTE *) &Buf, &BufSize, 0))
printf("Call to CryptGetHashParam failed. Error: %d\n",
GetLastError());
} else printf("Call to CryptHashData failed. Error: %d\n",
GetLastError());
if (!CryptDestroyHash(hash))
printf("Call to CryptDestroyHash failed. Error: %d\n",
GetLastError());
} else printf("Call to CryptCreateHash failed. Error: %d\n",
GetLastError());
if (!CryptReleaseContext(hProv, 0))
printf("Call to CryptReleaseContext failed. Error: %d\n",
GetLastError());
} else printf("Call to CryptAcquireContext failed. Error: %d\n",
GetLastError());
return Buf;
}
int _tmain()
{
printf("SHA hash: %s\n", CreateHash("Visual C++", CALG_SHA));
printf("MD5 hash: %s\n", CreateHash("Visual C++", CALG_MD5));
printf("\nPress any key to exit.");
getch();
return 0;
}
|
Transact-SQL HASHBYTES() function computes MD5 and SHA1 hashes identical to those produced by VFP and C# code samples above.

* * *
One-way hash functions can be used for creating a fingerprint for a block of data.
Also hash functions can be used to evaluate passwords. In Users table of your FoxPro application you store not passwords but their hash values.
It is extremely difficult, if ever possible, to find the original string for a given hash value -- depends on the hash algorithm as well as computer power and methodology you have.
* * *
Message Digest: The representation of text in the form of a single string of digits, created using a formula called a one-way hash function.
One-Way Hash Function: An algorithm that turns messages or text into a fixed string of digits, usually for security or data management purposes. The "one way" means that it is nearly impossible to derive the original text from the string.
Hash Collision: More than one input message producing identical hash output.
* * *
Secure Hash Algorithm, a hash function developed by the NSA for use with NIST Digital Signature Standard (DSS). NSA almost immediately developed a minor change known as SHA-1. Both SHA and SHA-1 produce a 160-bit digest. SHA-1 is used in SSL.
* * *
MD-5 is a one-way message-digest hash function. The algorithm processes input text and creates a 128-bit message digest which is unique to the message and can be used to verify data integrity.
MD-5 was developed by Ron Rivest and is intended to be used in digital signatures applications. Earlier message digest algorithms include obsolete MD-2 and MD-4.
See also:
RFC 1321 - The MD5 Message-Digest Algorithm
What are MD2, MD4, and MD5?
Example C Program: Creating an MD-5 Hash From File Content.
MD5 Online Cracking using Rainbow Tables
RFC 3174 - US Secure Hash Algorithm 1 (SHA1)
More on Newly Broken SHA1 on Alex Feldstein"s blog
Hash Collisions Q&A |