zoukankan      html  css  js  c++  java
  • 【Delphi】HMACMD5算法(四):应用

     以下是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.
  • 相关阅读:
    python学习日记(OOP访问限制)
    python学习日记(OOP——@property)
    python学习日记(OOP——静态方法和类方法)
    python学习日记(isinstance和issubclass)
    python学习日记(OOP——反射)
    python学习日记(初识面向对象)
    python学习日记(内置、匿名函数练习题)
    python学习日记(内置函数)
    python学习日记(内置函数补充)
    Raft一致性协议
  • 原文地址:https://www.cnblogs.com/caibirdy1985/p/4232960.html
Copyright © 2011-2022 走看看