利用Indy的TIdHashMessageDigest5类计算MD5(Indy10)

it2022-05-05  143

Indy自带TIdHashMessageDigest*类,可以方便的计算MD2、MD4和MD5。

代码很简单,其中需要注意的是,Indy9中 Md5Encode.AsHex(Md5Encode.HashValue(S)) 这种写法在Indy10里已经不能用了,之前就是因为这个卡了好久。

unit  uMD5; interface uses   IdHashMessageDigest, IdHash, IdGlobal; type   TMD5  =   class (TIdHashMessageDigest5); function  StrToMD5(S: String): String;  overload ; function  StrToMD5(S: String; L: integer): String;  overload ; implementation function  StrToMD5(S: String): String; var   Md5Encode: TMD5; begin   Md5Encode: =  TMD5.Create;    try      //Result := Md5Encode.AsHex(Md5Encode.HashValue(S));  // Indy9的写法     Result : =  Md5Encode.HashStringAsHex(S);     // Indy10中可以直接HashStringAsHex    finally     Md5Encode.Free;    end ; end ; function  StrToMD5(S: String; L: integer): String; begin   Result : =  Copy(StrToMD5(S),  5 , L); end ; end .

 

调用方法很简单,Edit2.Text := StrToMD5(Edit1.Text); 就可以搞定了,见上图。

完整工程源码(Source):CalcMD5.rar 附带一个C++ Builder版的工程文件,之前学习CB和Delphi混合编译的产物:CalcMD5_CBuilder.rar

转载于:https://www.cnblogs.com/bits/archive/2009/03/05/Delphi-CalcMD5.html

相关资源:DirectX修复工具V4.0增强版

最新回复(0)