How do I make an MD5 hash of a string with Delphi?
For the conversion, we need a so-called hash function. The goal of it is to convert a string into an integer, the so-called hash of the string. The following condition has to hold: if two strings and are equal ( ), then also their hashes have to be equal ( hash ( s ) = hash ( t ) ).
A hash function is a mathematical function that converts an input value into a compressed numerical value – a hash or hash value. Basically, it's a processing unit that takes in data of arbitrary length and gives you the output of a fixed length – the hash value.
The most you can do with a hash is to get some input that when hashed will produce the same hash as what you originally had. There is no guarantee that you will get back the original string.
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:
Update DCPCrypt
is now maintained by Warren Postma and source can be found here.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With