0

I'm trying to use the SHA3 cryptography library that is in the repository https://github.com/TheLazyTomcat/lib.SHA3 in Delphi 7, however, I wasn't successful. The project compiles using the library but I don't know how to implement it. I would like to generate a SHA3-512 hash from a string using this library but I don't know how to do it. Can someone help me?

I started the code below but I couldn't evolve to the solution:

procedure TForm1.Button1Click(Sender: TObject);
var
  Hasher: TSHA3_512Hash;
  Hash: TSHA3_512;
  Input: string;
  I: Integer;
begin
  Input := 'Hello, world!';

  Hasher := TSHA3_512Hash.Create;
  try
    Hasher.Init;
    Hasher.Update(Input[1], Length(Input) * SizeOf(Char));
    Hasher.HashString(Input);
    //Hash := Hasher.Final;
  finally
    Hasher.Free;
  end;

  {ShowMessage('Hash SHA-3 de ' + Input + ':'#13#10
              + '0x' + IntToHex(Hash[0], 2));
  for I := 1 to 63 do
    ShowMessage('0x' + IntToHex(Hash[I], 2));}
end;
  • Asking for tutorials is [off-topic](https://stackoveflow.com/help/on-topic) for StackOverflow. However, why are you hashing the `Input` twice? – Remy Lebeau Apr 25 '23 at 22:03

1 Answers1

1
program Demo;
{$APPTYPE CONSOLE}

uses
  SHA3;  // ...which needs AuxTypes, HashBase, AuxClasses, BitOps, StaticMemoryStream, StrRect, SimpleCPUID

var
  Hasher: TSHA3_512Hash;
begin
  Hasher:= TSHA3_512Hash.Create;
  try
    // Calling Hasher.Init() is not needed, as it is already done in THashBase.HashBuffer().
    Hasher.HashString( 'ABCD1234' );  // As per Delphi 7 these 8 characters equal 8 bytes.
    Writeln( Hasher.AsString() );  // That's it. Calling Hasher.Final() would raise an exception.
  finally
    Hasher.Free;
  end;
end.

Output is (spaces inserted by me):

D713A871 E75F8223 6D0396C6 7474F1F6 96181797 24584D55 4792F059 5E35F414 892C9B72 21058B0F FB3B1236 6901F25C 6A5BB5F7 19EB5A65 94D9C63F FDE1CDDB

Compared with the result of https://www.browserling.com/tools/sha3-hash which also is:

d713a871 e75f8223 6d0396c6 7474f1f6 96181797 24584d55 4792f059 5e35f414 892c9b72 21058b0f fb3b1236 6901f25c 6a5bb5f7 19eb5a65 94d9c63f fde1cddb

As you see: it's quite simple:

  1. Calling .HashString() to process the bytes you want.
  2. Getting the hexadecimal representation of the binary hash sum via .AsString().
AmigoJack
  • 5,234
  • 1
  • 15
  • 31