How do I hash a string with Delphi?
Asked Answered
F

12

27

How do I make an MD5 hash of a string with Delphi?

Figurehead answered 12/9, 2008 at 10:26 Comment(3)
As a side note, as of Delphi 2009, all objects in Delphi have a GetHashCode method.Huonghupeh
You are asking two different questions. One in your subject and a different one in your description.Blavatsky
Additionally, for older Delphi versions, if MD5 was actually not a requirement and any non-cryptographical hash would do (dictionary, etc), then there is stock "IniFiles" unit having TStringHash and THashedStringList classes.Jailer
F
31

If you want an MD5 digest and have the Indy components installed, you can do this:

uses SysUtils, IdGlobal, IdHash, IdHashMessageDigest;

with TIdHashMessageDigest5.Create do
try
    Result := TIdHash128.AsHex(HashValue('Hello, world'));
finally
    Free;
end;

Most popular algorithms are supported in the Delphi Cryptography Package:

  • Haval
  • MD4, MD5
  • RipeMD-128, RipeMD-160
  • SHA-1, SHA-256, SHA-384, SHA-512,
  • Tiger

Update DCPCrypt is now maintained by Warren Postma and source can be found here.

Figurehead answered 12/9, 2008 at 10:28 Comment(5)
Keep in mind the MD5 and 128 bit hash are only in Indy 9. They removed those from Indy 10.Blavatsky
You are talking about IdHashMessageDigest.pas? It's still there, at least in D2006 it's on Indy10 directory.Haematoblast
Be very careful using Indy for hashing files. Unfortunately Indy loads the ENTIRE file into memory as it does the hash. For files of several GB this leads to out-of-memory crashes. To get around they you need to use a non-Indy hash that can read the file in small chunks.Given
If you have Delphi XE 1 installed, so you have Indy 10.5.7 components, see my answer below.Nancienancy
docwiki.embarcadero.com/Libraries/Sydney/en/System.HashWhiten
D
20

If you want an MD5 hash string as hexadeciamal and you have Delphi XE 1 installed, so you have Indy 10.5.7 components you can do this:

uses IdGlobal, IdHash, IdHashMessageDigest;

class function getMd5HashString(value: string): string;
var
    hashMessageDigest5 : TIdHashMessageDigest5;
begin
    hashMessageDigest5 := nil;
    try
        hashMessageDigest5 := TIdHashMessageDigest5.Create;
        Result := IdGlobal.IndyLowerCase ( hashMessageDigest5.HashStringAsHex ( value ) );
    finally
        hashMessageDigest5.Free;
    end;
end;
Domingadomingo answered 14/8, 2013 at 13:44 Comment(0)
A
15

Why not use the system.Hash unit from RTL, that contains also a hash algorithm for MD5 since Delphi Seattle?

MD5HashCode := THashMD5.GetHashString(ClearTextString);
Angadresma answered 9/3, 2020 at 15:40 Comment(1)
Because 11 years ago, this unit didn't exist. ;-) Good hint though for people using more modern Delphi versions.Godber
N
11

I usually use DCPCrypt2 (Delphi Cryptography Package) from David Barton (City in the Sky).

It is also contains the following Encryption Algorithms:

  • Blowfish
  • Cast 128
  • Cast 256
  • DES, 3DES
  • Ice, Thin Ice, Ice2
  • IDEA
  • Mars
  • Misty1
  • RC2, RC4, RC5, RC6
  • Rijndael (the new AES)
  • Serpent
  • Tea
  • Twofish

Update DCPCrypt is now maintained by Warren Postma and source can be found here.

Nothingness answered 17/9, 2008 at 13:30 Comment(0)
P
9

This is a modification of devstopfix's answer which was accepted.

In current Indy version you can hash strings and streams more easily. Example:

function MD5String(str: String): String;
begin
  with TIdHashMessageDigest5.Create do
    try
      Result := HashStringAsHex(str);
    finally
      Free;
    end;
end;

Use HashString, HashStringAsHex, HashBytes, HashBytesAsHex, HashStream, HashStreamAsHex. The advantage is that you can also specify a text encoding

Pustule answered 25/1, 2015 at 3:20 Comment(0)
D
5

If all you want to do is use a dictionary, and you're not looking for security then:
In Delphi 2009 and higher, hash values for strings can be created with

BobJenkinsHash(Value, Length(Value) * SizeOf(Value), 0)

where Value is a string.

https://docwiki.embarcadero.com/VCL/en/Generics.Defaults.BobJenkinsHash

Deafening answered 14/9, 2011 at 6:15 Comment(1)
Please note that this is not a cryptographic hash function (which means, it is fine for hash tables and such, but don't use it for data signing and such).Gunnel
M
5

Spring For Delphi project - http://www.spring4d.org - has implementation for a number of SHAxxx hashes, MD5 hash, and also number of CRC functions

Mev answered 17/1, 2014 at 13:59 Comment(0)
B
4

You can also use the WindowsCrypto API with Delphi:

There is a unit in there that wraps all the CryptoAPI. You can also use Lockbox, which is now open source.

In the end you can support pretty much any Hash algorithms with Delphi. The Indy example is probably the closest you will get to natively in Delphi since Indy is included with most versions of Delphi. For the rest you will need to either use a library or write some more code to access the CryptoAPI or implement it yourself.

Blavatsky answered 12/9, 2008 at 16:41 Comment(0)
S
4

TurboPower Lockbox supports:

  • MD-5,
  • SHA-1 and
  • the entire SHA-2 family including the recently published SHA-512/224 & SHA-512/256 hashes.
Seeger answered 14/12, 2011 at 13:21 Comment(0)
G
2

Recent Delphi version allow for something much simpler than all the previous solutions:

uses System.Hash;

function CalculateMD5Hash(const InString: String): String;
begin
  Result := UpperCase(THashMD5.GetHashString(InString));
end;
Gonick answered 12/10, 2023 at 15:50 Comment(0)
T
1

Using ICS, you simply call StrMD5 function which is located in OverbytecsMD5 unit.

Beside that specific function, there are a lot more MD5 function for other datatypes and scenarios. There are also other hash methods such as SHA.

Transit answered 9/3, 2020 at 14:26 Comment(1)
This one seems to be almost 30-40% faster than Indy and System.Hash, while adding the least code to the project.Sanguinaria
I
0

Using System.Hash

In the newer versions of Delphi, you can use the THashMD5 record from the System.Hash unit.

Here's a function that uses it. The first parameter is the string that you want to hash and the second parameter is a boolean where you can specify whether you want the Hash String to be uppercase or lowercase.

uses System.Hash

...

function CalculateMD5Hash(MyString: String; bUpperCase: Boolean = True): String;
begin
  Result := THashMD5.GetHashString(MyString);
  if bUpperCase then
    Result := Result.ToUpper
  else
    Result := Result.ToLower;
end;

Using Indy

If you're using an older version of Delphi that doesn't have System.Hash or you just prefer Indy, then you can use the Indy Library for it with the IdHashMessageDigest unit.

uses IdHashMessageDigest;

...

function CalculateMD5Hash(MyString: String; bUpperCase: Boolean = True): string;
begin
  var MD5 := TIdHashMessageDigest5.Create;
  try
    Result := MD5.HashStringAsHex(MyString);
    if bUpperCase then
      Result := Result.ToUpper
    else
      Result := Result.ToLower;
  finally
    MD5.Free;
  end;
end;
Ingaborg answered 10/2 at 12:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.