You are not checking to make sure that TIdHashSHA512.IsAvailable
is True
before using TIdHashSHA512.HashStringAsHex()
, eg:
use
IdHash, IdHashSHA;
function TForm1.SHA512(const AString: string): string;
var
LSHA512: TIdHashSHA512;
begin
if TIdHashSHA512.IsAvailable then // <-- ADD THIS!
begin
LSHA512 := TIdHashSHA512.Create;
try
Result := LSHA512.HashStringAsHex(AString);
finally
LSHA512.Free;
end;
end
else begin
// SHA-512 is not hooked up for use!!
end;
end;
SHA-512 (and most other cryptographic algorithms) is NOT built-in to Indy, so TIdHashSHA512.IsAvailable
returns False
by default. To make it return True
, the IsHashingIntfAvail
and IsSHA512HashIntfAvail
function pointers in the IdFIPS
unit need to be hooked up to a library/API that actually implements SHA-512, such as OpenSSL (via the IdSSLOpenSSLHeaders
unit). HashStringAsHex()
itself requires corresponding functions to be assigned to the GetSHA512HashInst
, UpdateHashInst
, and FinalHashInst
function pointers.
For example:
use
IdHash, IdHashSHA,
IdSSLOpenSSLHeaders; // <-- ADD THIS!
// And then deploy OpenSSL DLLs with your app...
function TForm1.SHA512(const AString: string): string;
var
LSHA512: TIdHashSHA512;
begin
IdSSLOpenSSLHeaders.Load;
if TIdHashSHA512.IsAvailable then
begin
LSHA512 := TIdHashSHA512.Create;
try
Result := LSHA512.HashStringAsHex(AString);
finally
LSHA512.Free;
end;
end
else begin
// SHA-512 is not hooked up for use!!
end;
end;
As Delphi 7 only supports Windows development, if you don't want to rely on 3rd party libraries, Microsoft's Crypto API does support SHA-512 on modern Windows versions, so you could opt to write a few functions in your code to invoke the Crypto API, and then assign those functions to the IdFIPS
function pointers, eg:
uses
IdFIPS;
function MyHashingIntfAvail: Boolean;
begin
Result := True;
end;
function MySHA512HashIntfAvail: Boolean;
begin
Result := True;
end;
function MyGetSHA512HashInst: TIdHashIntCtx;
begin
// initialize SHA-512 as needed
// return a context that refers to its hash...
// see CryptCreateHash()
Result := ...;
end;
procedure MyUpdateHashInst(ACtx: TIdHashIntCtx; const AIn: TIdBytes);
begin
// update the context's hash as needed...
// see CryptHashData()
end;
function MyFinalHashInst(ACtx: TIdHashIntCtx): TIdBytes;
begin
// finalize the context's hash, free the context,
// and return the final hash...
// see CryptDestroyHash()
end;
initialization
IdFIPS.IsHashingIntfAvail := @MyHashingIntfAvail;
IdFIPS.IsSHA512HashIntfAvail := @MySHA512HashIntfAvail;
IdFIPS.GetSHA512HashInst := @MyGetSHA512HashInst;
IdFIPS.UpdateHashInst := @MyUpdateHashInst;
IdFIPS.FinalHashInst := @MyFinalHashInst;