Search code examples
delphilockbox-3

How to change THash.Hash at runtime


How can I change the default THash.Hash algorythm from trhe default SHA-1 to MD5?

The following does not works:

var
  StringHash: THash;
begin
  StringHash.Create(nil);
  StringHash.Hash := 'MD5';
end;

Edit:

Yes you are all right: I apologize for not having mentioned the fact that THash is a class of the new TurboPower LockBox 3.

My apologies again for this omission!

Anyway Sean has already given the answer I was looking for.

Thank you all


Solution

  • Assuming that you are referring to the THash component of TurboPower Lockbox, you can select the hashing algorithm at run-time like so:

    function FindHashOfBananaBananaBanana: TBytes;
    var
      StringHash: THash;
      Lib: TCrypographicLibrary;
    begin
    StringHash := THash.Create( nil);
    Lib := TCrypographicLibrary( nil);
    try
      StringHash.CryptoLibrary := Lib;
      StringHash.HashId := SHA512_ProgId; // Find constants for other algorithms
                                          //  in unit uTPLb_Constants.
      StringHash.HashAnsiString('Banana banana banana');
      SetLength( result, StringHash.HashOutputValue.Size);
      StringHash.HashOutputValue.Read( result[0], StringHash.HashOutputValue.Size);
      StringHash.Burn
    finally
      StringHash.Free;
      Lib.Free
      end
    end;