How do I make an MD5 hash of a string with Delphi?
如何用Delphi做一个MD5散列?
8 个解决方案
#1
27
If you want an MD5 digest and have the Indy components installed, you can do this:
如果您想要一个MD5摘要并安装了Indy组件,您可以这样做:
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:
在Delphi加密包中支持最流行的算法:
- Haval
- 哈弗
- MD4, MD5
- MD4 MD5
- RipeMD-128, RipeMD-160
- ripemd ripemd - 128 - 160
- SHA-1, SHA-256, SHA-384, SHA-512,
- sha - 1,sha - 256、sha - 384、sha - 512,
- Tiger
- 老虎
Update DCPCrypt
is now maintained by Warren Postma and source can be found here.
更新DCPCrypt现在由Warren Postma维护,源代码可以在这里找到。
#2
16
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:
如果你想要一个MD5哈希字符串作为hexadeciamal并且你已经安装了Delphi XE 1,那么你有Indy 10.5.7组件,你可以这样做:
uses IdGlobal, IdHash, IdHashMessageDigest;
使用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
10
I usually use DCPCrypt2 (Delphi Cryptography Package) from David Barton (City in the Sky).
我通常使用DCPCrypt2 (Delphi加密包)来自David Barton(天空之城)。
It is also contains the following Encryption Algorithms:
它还包含以下加密算法:
- Blowfish
- 河豚
- Cast 128
- 投128
- Cast 256
- 投256
- DES, 3DES
- DES、3 DES
- Ice, Thin Ice, Ice2
- 冰,薄冰,Ice2
- IDEA
- 的想法
- Mars
- 火星
- Misty1
- Misty1
- RC2, RC4, RC5, RC6
- RC2,RC4 RC5,将是
- Rijndael (the new AES)
- Rijndael(AES)
- Serpent
- 蛇
- Tea
- 茶
- Twofish
- Twofish
Update DCPCrypt
is now maintained by Warren Postma and source can be found here.
更新DCPCrypt现在由Warren Postma维护,源代码可以在这里找到。
#4
4
TurboPower Lockbox supports:
TurboPower密码箱支持:
- MD-5,
- MD-5,
- SHA-1 and
- sha - 1和
- the entire SHA-2 family including the recently published SHA-512/224 & SHA-512/256 hashes.
- 整个SHA-2家族包括最近出版的SHA-512/224 & SHA-512/256哈希。
#5
4
Spring For Delphi project - http://www.spring4d.org - has implementation for a number of SHAxxx hashes, MD5 hash, and also number of CRC functions
Delphi项目的Spring——http://www.spring4d.org——已经实现了一些SHAxxx散列、MD5哈希和CRC函数的数量。
#6
4
This is a modification of devstopfix's answer which was accepted.
这是对devstopfix的回答的修改。
In current Indy version you can hash strings and streams more easily. Example:
在当前的Indy版本中,您可以更轻松地处理字符串和流。例子:
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
使用HashString、HashStringAsHex、HashBytes、HashBytesAsHex、HashStream、HashStreamAsHex。优点是您还可以指定文本编码。
#7
4
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
如果您想要做的只是使用字典,那么您就不会寻找安全性:在Delphi 2009和更高版本中,可以创建字符串的散列值。
BobJenkinsHash
(Value, Length(Value) * SizeOf(Value), 0)
BobJenkinsHash(值,长度(值)* SizeOf(值),0)
where Value is a string.
其中值为字符串。
http://docwiki.embarcadero.com/VCL/en/Generics.Defaults.BobJenkinsHash
http://docwiki.embarcadero.com/VCL/en/Generics.Defaults.BobJenkinsHash
#8
3
You can also use the WindowsCrypto API with Delphi:
您还可以使用Delphi来使用WindowsCrypto API:
- 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.
这里有一个封装所有密码的单元。您也可以使用Lockbox,它现在是开源的。
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.
最后,你可以用Delphi来支持任何哈希算法。Indy示例可能是在Delphi中最接近本地的,因为Indy包含了大多数Delphi版本。对于rest,您需要使用一个库或者编写更多的代码来访问CryptoAPI或自己实现它。
#1
27
If you want an MD5 digest and have the Indy components installed, you can do this:
如果您想要一个MD5摘要并安装了Indy组件,您可以这样做:
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:
在Delphi加密包中支持最流行的算法:
- Haval
- 哈弗
- MD4, MD5
- MD4 MD5
- RipeMD-128, RipeMD-160
- ripemd ripemd - 128 - 160
- SHA-1, SHA-256, SHA-384, SHA-512,
- sha - 1,sha - 256、sha - 384、sha - 512,
- Tiger
- 老虎
Update DCPCrypt
is now maintained by Warren Postma and source can be found here.
更新DCPCrypt现在由Warren Postma维护,源代码可以在这里找到。
#2
16
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:
如果你想要一个MD5哈希字符串作为hexadeciamal并且你已经安装了Delphi XE 1,那么你有Indy 10.5.7组件,你可以这样做:
uses IdGlobal, IdHash, IdHashMessageDigest;
使用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
10
I usually use DCPCrypt2 (Delphi Cryptography Package) from David Barton (City in the Sky).
我通常使用DCPCrypt2 (Delphi加密包)来自David Barton(天空之城)。
It is also contains the following Encryption Algorithms:
它还包含以下加密算法:
- Blowfish
- 河豚
- Cast 128
- 投128
- Cast 256
- 投256
- DES, 3DES
- DES、3 DES
- Ice, Thin Ice, Ice2
- 冰,薄冰,Ice2
- IDEA
- 的想法
- Mars
- 火星
- Misty1
- Misty1
- RC2, RC4, RC5, RC6
- RC2,RC4 RC5,将是
- Rijndael (the new AES)
- Rijndael(AES)
- Serpent
- 蛇
- Tea
- 茶
- Twofish
- Twofish
Update DCPCrypt
is now maintained by Warren Postma and source can be found here.
更新DCPCrypt现在由Warren Postma维护,源代码可以在这里找到。
#4
4
TurboPower Lockbox supports:
TurboPower密码箱支持:
- MD-5,
- MD-5,
- SHA-1 and
- sha - 1和
- the entire SHA-2 family including the recently published SHA-512/224 & SHA-512/256 hashes.
- 整个SHA-2家族包括最近出版的SHA-512/224 & SHA-512/256哈希。
#5
4
Spring For Delphi project - http://www.spring4d.org - has implementation for a number of SHAxxx hashes, MD5 hash, and also number of CRC functions
Delphi项目的Spring——http://www.spring4d.org——已经实现了一些SHAxxx散列、MD5哈希和CRC函数的数量。
#6
4
This is a modification of devstopfix's answer which was accepted.
这是对devstopfix的回答的修改。
In current Indy version you can hash strings and streams more easily. Example:
在当前的Indy版本中,您可以更轻松地处理字符串和流。例子:
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
使用HashString、HashStringAsHex、HashBytes、HashBytesAsHex、HashStream、HashStreamAsHex。优点是您还可以指定文本编码。
#7
4
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
如果您想要做的只是使用字典,那么您就不会寻找安全性:在Delphi 2009和更高版本中,可以创建字符串的散列值。
BobJenkinsHash
(Value, Length(Value) * SizeOf(Value), 0)
BobJenkinsHash(值,长度(值)* SizeOf(值),0)
where Value is a string.
其中值为字符串。
http://docwiki.embarcadero.com/VCL/en/Generics.Defaults.BobJenkinsHash
http://docwiki.embarcadero.com/VCL/en/Generics.Defaults.BobJenkinsHash
#8
3
You can also use the WindowsCrypto API with Delphi:
您还可以使用Delphi来使用WindowsCrypto API:
- 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.
这里有一个封装所有密码的单元。您也可以使用Lockbox,它现在是开源的。
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.
最后,你可以用Delphi来支持任何哈希算法。Indy示例可能是在Delphi中最接近本地的,因为Indy包含了大多数Delphi版本。对于rest,您需要使用一个库或者编写更多的代码来访问CryptoAPI或自己实现它。