1

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

Fabio Vitale
  • 2,257
  • 5
  • 28
  • 38
  • What is THash ? I can't find any reference. – TLama Dec 13 '11 at 16:29
  • T looks like it's just the Class Type which is really Hash since Delphi uses T for it's naming convention. can you navigate to the "THash" and see what it's true class inherits from does Indy have a class for this..? here is a good link to utilize http://stackoverflow.com/questions/58621/how-do-i-hash-a-string-with-delphi – MethodMan Dec 13 '11 at 17:02
  • 1
    Of course that doesn't work. That will crash your program because you're accessing methods and properties of a non-existent object. But that has nothing to do with hashing. That's true of *all* Delphi objects. Is *that* what you're actually asking about? If not, then please post *relevant* code showing how you *successfully* use a hash, and then we can show you what to change to use a different one. – Rob Kennedy Dec 13 '11 at 18:01

2 Answers2

3

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;
Sean B. Durkin
  • 12,659
  • 1
  • 36
  • 65
2

Your example code is invalid. The variable type is THASH, the variable name is STRINGHASH. When you construct an instance of a class the format is typically:

var
  StringHash:THash;
begin
  StringHash := THash.Create();
  try
    DoSomethingWithStringHash;
  finally
    StringHash.Free()
  end
end;

Fix your example and come back with more details.

Darian Miller
  • 7,808
  • 3
  • 43
  • 62