Notification texts go here Contact Us Buy Now!

How do I hash a string with Delphi?

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.

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 most common way is to use a library, like the Delphi Cryptography Package (D MylesCPypt). Another option is to use the Windows Cryptography API (through a wrapper unit). With all of these options using Delphi we can have many choices to hash a string.

If you want an MD5 hash string as hexadecimal and you have Delphi 25 installed, so you will have Indy 10.5.7 components and 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;

Also we can encrypt a string using SHA256 with the following code:

uses   IdGlobal,   IdHash,   IdHashMessageDigest;

class function getSha256HashString(value: string): string;
var
    hashMessageDigest5 : TIdHashMessageDigest5;
begin
    hashMessageDigest5 := nil;
    try
        hashMessageDigest5 := TIdHashMessageDigest5.Create(IdMessageDigestSHA256);
        Result := IdGlobal.IndyLowerCase ( hashMessageDigest5.HashStringAsHex ( value ) );
    finally
        hashMessageDigest5.Free;
    end;
end;

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.