5,392
社区成员
发帖
与我相关
我的任务
分享
unit ToMD5;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdHashMessageDigest, IdHash, IdGlobal, StdCtrls;
type
TMD5 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Label1: TLabel;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Label2: TLabel;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
function StrToMD5(S: String): String; overload;
function StrToMD516(S: String; L: integer): String; overload; //返加16位MD5值
function StrToMD532(S: String; L: integer): String; overload; //返回32位MD5值
{ Private declarations }
public
{ Public declarations }
end;
var
MD5: TMD5;
implementation
{$R *.dfm}
{ TMD5 }
//执行返回32位MD5值
procedure TMD5.Button1Click(Sender: TObject);
var
s, s1: string;
i: Integer;
begin
s := Edit1.Text;
i := StrToInt(Edit2.Text);
Label1.Caption := StrToMD532(s, i);
//返回MD5值的长度
s1 := Label1.Caption;
Edit4.Text := IntToStr(Length(s1));
end;
//执行返回16位MD5值
procedure TMD5.Button2Click(Sender: TObject);
var
s: string;
i: Integer;
begin
s := Edit3.Text;
i := StrToInt(Edit4.Text);
Label2.Caption := StrToMD516(s, i);
end;
function TMD5.StrToMD5(S: String): String;
var
Md5Encode: TIdHashMessageDigest5;
begin
Md5Encode:= TIdHashMessageDigest5.Create;
try
result:= Md5Encode.HashStringAsHex(s);
finally
Md5Encode.Free;
end;
end;
//返回16位MD5值函数
function TMD5.StrToMD516(S: String; L: Integer): String;
begin
result:= copy(StrToMD5(S), 9, L); //数值 9 是做的什么? L 是做什么?
end;
//返回16位MD5值函数
function TMD5.StrToMD532(S: String; L: Integer): String;
begin
result:= copy(StrToMD5(S), 1, L);
end;
end.