Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I hash a string with Delphi?

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

like image 285
devstopfix Avatar asked Sep 12 '08 10:09

devstopfix


People also ask

How is string hashed?

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 ) ).

How does hash () work?

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.

Are hashes used for reversing a string?

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.


2 Answers

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.

like image 73
devstopfix Avatar answered Oct 12 '22 16:10

devstopfix


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; 
like image 27
Stéphane B. Avatar answered Oct 12 '22 16:10

Stéphane B.