0

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.

3
  • 3
    Please edit your question with an actual example. Don't make us go to another website to find out what you want. Also, have you searched this site for TIdHashSHA512? It could well be a duplicate of stackoverflow.com/questions/47379509/… or stackoverflow.com/questions/53292571/…
    – Jan Doggen
    Sep 25 at 7:01
  • As you can see from answers that Jan Doggen pointed you to Indy Hashing algorithms heavily depend on OS API capabilities. In order to avoid OS dependency you might want to take a look at DEC - Delphi Encryption Compendium which is open-sourced library that supports many cryptographic and hashing algorithms. Sep 25 at 8:59
  • @SilverWarior i'll check this out
    – Lionmaru
    Sep 26 at 1:17

2 Answers 2

3

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;
0

You can use one of the following libraries:

1
  • 2
    While useful info, this does nothing to explain why the original code fails, or how to fix it. Sep 25 at 21:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.