How do I make an MD5 hash of a string with Delphi?
问题:
回答1:
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.
回答2:
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;
回答3:
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.
回答4:
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
回答5:
TurboPower Lockbox supports:
- MD-5,
- SHA-1 and
- the entire SHA-2 family including the recently published SHA-512/224 & SHA-512/256 hashes.
回答6:
Spring For Delphi project - http://www.spring4d.org - has implementation for a number of SHAxxx hashes, MD5 hash, and also number of CRC functions
回答7:
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.
http://docwiki.embarcadero.com/VCL/en/Generics.Defaults.BobJenkinsHash
回答8:
You can also use the WindowsCrypto API with Delphi:
- General Crypto & Hash demo and resources
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.