• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

【Delphi】HMAC-MD5算法(四):应用

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 以下是D7的代码,2009后支持unicode,Char和PChar,string等类型不一样,请注意自行修改

//******************************************************************************
//基于MD5算法的Hash MAC:应用

//******************************************************************************
//作者:Cai
//日期:2011-10-25
//******************************************************************************

unit HMAC_MD5Utils;


interface
uses
SysUtils, Classes,
HMAC_MD5Class, MD5Class;

type


THMAC_MD5 = class
protected
   FHMAC: THMAC_MD5Class;
public
   constructor Create(); virtual;
   destructor Destroy; override;
   //=========基于MD5的Hash MAC算法:应用==========
   function HMAC_StrToMD5_String(sStr: string; sKey: string): string;
   function HMAC_StrToMD5_Digest(sStr: string; sKey: string): TMD5Digest;
   function HMAC_FileToMD5_String(sFileName: string; sKey: string): string;
   function HMAC_FileToMD5_Digest(sFileName: string; sKey: string): TMD5Digest; 
end;

implementation



{ THMAC_MD5 }

constructor THMAC_MD5.Create;
begin
   FHMAC:=THMAC_MD5Class.Create();
end;

destructor THMAC_MD5.Destroy;
begin
   FHMAC.Destroy;
   inherited;
end;

function THMAC_MD5.HMAC_FileToMD5_Digest(sFileName,
sKey: string): TMD5Digest;
var
   FileStream: TFileStream;
   iLen: UINT4;
   Buffer: array[0..1024-1]of Byte;
   Context: THMAC_MD5Context;
begin
   FillChar(Result, SizeOf(TMD5Digest), 0);
   if not FileExists(sFileName) then Exit;
   FileStream := TFileStream.Create(sFileName, fmOpenRead or fmShareDenyNone);
   try
   FHMAC.HMAC_MD5Init(Context, sKey);
   while True do
   begin
     iLen := FileStream.Read(Buffer, 1024);
     if iLen>0 then
       FHMAC.HMAC_MD5Update(Context, @Buffer, iLen);
     if iLen<1024 then Break;
   end;
   FHMAC.HMAC_MD5Final(Result, Context);
   finally
     FileStream.Destroy;
   end;
end;

function THMAC_MD5.HMAC_FileToMD5_String(sFileName, sKey: string): string;
begin
   Result := TMD5Class.DigestToString(HMAC_FileToMD5_Digest(sFileName, sKey));
end;

function THMAC_MD5.HMAC_StrToMD5_Digest(sStr, sKey: string): TMD5Digest;
var
Context: THMAC_MD5Context;
begin
   FillChar(Result, SizeOf(TMD5Digest), 0);
   FHMAC.HMAC_MD5Init(Context, sKey);
   FHMAC.HMAC_MD5Update(Context, PByte(PChar(sStr)), Length(sStr));
   FHMAC.HMAC_MD5Final(Result, Context);
end;

function THMAC_MD5.HMAC_StrToMD5_String(sStr, sKey: string): string;
begin
   Result := TMD5Class.DigestToString(HMAC_StrToMD5_Digest(sStr, sKey));
end;

end. 

 

 

// ==========================================

//测试代码;

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
   Edit1: TEdit;
   Memo1: TMemo;
   Button1: TButton;
   Button2: TButton;
   OpenDialog1: TOpenDialog;

   Edit2: TEdit;
   procedure Button1Click(Sender: TObject);
   procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
uses
HMAC_MD5Utils;

{$R *.dfm}

var   
HMAC_MD5: THMAC_MD5;

procedure TForm1.Button1Click(Sender: TObject);
begin
  HMAC_MD5:=THMAC_MD5.Create;
  try
    Memo1.Lines.Add('Text HMAC_MD5 = '+HMAC_MD5.HMAC_StrToMD5_String(Edit1.Text, Edit2.Text));
  finally
    HMAC_MD5.Destroy;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if not OpenDialog1.Execute() then Exit;
  HMAC_MD5:=THMAC_MD5.Create;
  try
    Memo1.Lines.Add('File HMAC_MD5 = '+HMAC_MD5.HMAC_FileToMD5_String(OpenDialog1.FileName, Edit2.Text));
  finally
    HMAC_MD5.Destroy;
  end;
end;

end.

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
配置VLFeat-0.9.20+MATLAB2013a发布时间:2022-07-18
下一篇:
matlab读取nc发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap