How to create SHA-512 Cryptographic Hash Algorithm using Delphi 7?

264 views Asked by At

I use this code to try it and I need to get result like this site https://sha512.online/

use IdHash,IdHashSHA;

function TForm1.SHA512(const AString: string): string;
var LSHA512: TIdHashSHA512;
begin
 LSHA512 := TIdHashSHA512.Create;
  try
    Result := LSHA512.HashStringAsHex(AString);
  finally
    LSHA512.Free;
  end;
end;

But it raise an Access Violation error at line LSHA512.HashStringAsHex. Thank you.

2

There are 2 answers

1
IVO GELOV On
0
Remy Lebeau On

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;