type
ULONG = Cardinal;
ULONG_PTR = ^ULONG;
THCRYPTPROV = ULONG;
PTHCRYPTPROV = ULONG_PTR;
LPCTSTR = PAnsiChar;
PBYTE = ^Byte;
ALG_ID = Cardinal;
HCRYPTKEY = THandle;
PHCRYPTKEY = HCRYPTKEY;
PHCRYPTHASH = ULONG_PTR;
const
ALG_CLASS_HASH = 4
shl 13;
ALG_TYPE_ANY = 0;
ALG_SID_MD5 = 3;
ALG_SID_SHA = 4;
ALG_SID_SHA1 = ALG_SID_SHA;
CALG_MD5 = ALG_CLASS_HASH
or ALG_TYPE_ANY
or ALG_SID_MD5;
CALG_SHA1 = ALG_CLASS_HASH
or ALG_TYPE_ANY
or ALG_SID_SHA1;
PROV_RSA_FULL = 1;
CRYPT_NEWKEYSET = $08;
HP_HASHVAL = $0002;
NTE_BAD_KEYSET = HRESULT($80090016);
function CryptAcquireContext(phProv: PTHCRYPTPROV; pszContainer, pszProvider: LPCTSTR; dwProvType, dwFlags: Cardinal): LongBool;
stdcall;
external '
Advapi32.dll'
name '
CryptAcquireContextA';
function CryptCreateHash(hProv: THCRYPTPROV; Algid: ALG_ID; hKey: HCRYPTKEY; dwFlags: Cardinal; phHash: PHCRYPTHASH): LongBool;
stdcall;
external '
Advapi32.dll'
name '
CryptCreateHash';
function CryptHashData(hHash: PHCRYPTHASH; pbData: PBYTE; dwDataLen, dwFlags: Cardinal): LongBool;
stdcall;
external '
Advapi32.dll'
name '
CryptHashData';
function CryptGetHashParam(hHash: PHCRYPTHASH; dwParam: Cardinal; pbData: PBYTE; pdwDataLen: PCardinal; dwFlags: Cardinal): LongBool;
stdcall;
external '
Advapi32.dll'
name '
CryptGetHashParam';
function CryptDestroyHash(hHash: PHCRYPTHASH): LongBool;
stdcall;
external '
Advapi32.dll'
name '
CryptDestroyHash';
function CryptReleaseContext(hProv: THCRYPTPROV; dwFlags: Cardinal): LongBool;
stdcall;
external '
Advapi32.dll'
name '
CryptReleaseContext';
function CreateHash(aString: AnsiString; aAlgoID: Cardinal): AnsiString;
var
hCryptProv : THCRYPTPROV;
hHash : PHCRYPTHASH;
dwDataLen : Cardinal;
Data :
Array[1..20]
of Byte;
i : Integer;
begin
{
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa379886.aspx
* Handle zum Provider Content.
}
if CryptAcquireContext(@hCryptProv,
nil,
nil, PROV_RSA_FULL, 0)
or ((GetLastError = NTE_BAD_KEYSET)
and CryptAcquireContext(@hCryptProv,
nil,
nil, PROV_RSA_FULL, CRYPT_NEWKEYSET))
then begin
{
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa379908.aspx
* Handle zum Hashobject.
}
if CryptCreateHash(hCryptProv, aAlgoID, 0, 0, @hHash)
then begin
{
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa380202.aspx
* Daten übergeben.
}
CryptHashData(hHash, Pointer(PByte(aString)), Length(aString), 0);
{
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa379947.aspx
* Hash abholen.
}
FillChar(Data, SizeOf(Data), 0);
// if aAlgoID = CALG_SHA1 then dwDataLen := 20;
// if aAlgoID = CALG_MD5 then dwDataLen := 16;
dwDataLen := 20;
if CryptGetHashParam(hHash, HP_HASHVAL, @Data, @dwDataLen, 0)
then begin
{
* Ergebnis übergeben
}
i := 1;
Result := '
';
while (Data[i] <> 0)
and (i < dwDataLen + 1)
do begin
Result := Result + Chr(Integer(Data[i]));
inc(i);
end;
end;
{
* [url]http://msdn.microsoft.com/en-us/library/windows/desktop/aa379917.aspx[/url]
* Handle zum Hashobject freigeben.
}
if hHash <>
nil
then CryptDestroyHash(hHash);
{
* [url]http://msdn.microsoft.com/en-us/library/windows/desktop/aa380268.aspx[/url]
* Den Content releasen.
}
if hCryptProv <> 0
then CryptReleaseContext(hCryptProv, 0);
end;
end;
end;