system.hash.pas md5

 

转载

https://www.cnblogs.com/hnxxcxg/p/14276734.html

 

system.hash.pas

delphi xe8开始提供system.hash.pas。

xe10.4.1版本,提供有几个记录:

THash = record

THashBobJenkins = record

THashMD5 = record

THashSHA1 = record

THashSHA2 = record

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
uses  System.Hash;
 
function md5_hmac(const val, key: string): string;
begin
  var md5: THashMD5 := THashMD5.Create;
  Result := md5.GetHMAC(val, key);
end;
 
function md5_utf8(const val: string): string;
begin
  var md5: THashMD5 := THashMD5.Create;
  md5.Update(TEncoding.UTF8.GetBytes(val));
  Result := md5.HashAsString;
end;
 
function sha1_utf8(const val: string): string;
begin
  var sha1: THashSHA1 := THashSHA1.Create;
  sha1.Update(TEncoding.UTF8.GetBytes(val));
  Result := sha1.HashAsString;
end;
 
function sha2_utf8(const val: string): string;
begin
  var sha2: THashSHA2 := THashSHA2.Create;
  sha2.Update(TEncoding.UTF8.GetBytes(val));
  Result := sha2.HashAsString;
end;
 
procedure TForm2.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Add(md5_utf8('abc'));         //ce33960c8f97c85161a8b28b7000b3c6
  Memo1.Lines.Add(sha1_utf8('abc'));        //2566ca7678fcdb309846eabef3911dc6e5f8814d
  Memo1.Lines.Add(sha2_utf8('abc'));        //f3bbbfaf81071d67f02fd519553789c671c7e0514045885e5bd8faa80d66792f
  Memo1.Lines.Add(md5_hmac('abc', '钥匙')); //0796b66fa3180024cb5e49cba66dbf58
end;

--------------------------------------------------------========================

uses System.Hash;

function GetStrHashMD5(Str: String): String;

var

  HashMD5: THashMD5;

begin

    HashMD5 := THashMD5.Create;

    HashMD5.GetHashString(Str);

    result := HashMD5.GetHashString(Str);

end;

 

function GetStrHashSHA1(Str: String): String;

var

  HashSHA: THashSHA1;

begin

    HashSHA := THashSHA1.Create;

    HashSHA.GetHashString(Str);

    result := HashSHA.GetHashString(Str);

end;

 

function GetStrHashSHA224(Str: String): String;

var

  HashSHA: THashSHA2;

begin

    HashSHA := THashSHA2.Create;

    HashSHA.GetHashString(Str);

    result := HashSHA.GetHashString(Str,SHA224);

end;

 

function GetStrHashSHA256(Str: String): String;

var

  HashSHA: THashSHA2;

begin

    HashSHA := THashSHA2.Create;

    HashSHA.GetHashString(Str);

    result := HashSHA.GetHashString(Str,SHA256);

end;

 

function GetStrHashSHA384(Str: String): String;

var

  HashSHA: THashSHA2;

begin

    HashSHA := THashSHA2.Create;

    HashSHA.GetHashString(Str);

    result := HashSHA.GetHashString(Str,SHA384);

end;

 

function GetStrHashSHA512(Str: String): String;

var

  HashSHA: THashSHA2;

begin

    HashSHA := THashSHA2.Create;

    HashSHA.GetHashString(Str);

    Result := HashSHA.GetHashString(Str,SHA512);

end;

 

function GetStrHashSHA512_224(Str: String): String;

var

  HashSHA: THashSHA2;

begin

    HashSHA := THashSHA2.Create;

    HashSHA.GetHashString(Str);

    Result := HashSHA.GetHashString(Str,SHA512_224);

end;

 

function GetStrHashSHA512_256(Str: String): String;

var

  HashSHA: THashSHA2;

begin

    HashSHA := THashSHA2.Create;

    HashSHA.GetHashString(Str);

    Result := HashSHA.GetHashString(Str,SHA512_256);

end;

 

function GetStrHashBobJenkins(Str: String): String;

var

  Hash: THashBobJenkins;

begin

    Hash := THashBobJenkins.Create;

    Hash.GetHashString(Str);

    Result := Hash.GetHashString(Str);

end;

Examples calculating file hashes:

 

function GetFileHashMD5(FileName: WideString): String;

var

  HashMD5: THashMD5;

  Stream: TStream;

  Readed: Integer;

  Buffer: PByte;

  BufLen: Integer;

begin

  HashMD5 := THashMD5.Create;

  BufLen := 16 * 1024;

  Buffer := AllocMem(BufLen);

  try

    Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);

    try

      while Stream.Position < Stream.Size do

      begin

        Readed := Stream.Read(Buffer^, BufLen);

        if Readed > 0 then

        begin

          HashMD5.update(Buffer^, Readed);

        end;

      end;

    finally

      Stream.Free;

    end;

  finally

    FreeMem(Buffer)

  end;

  result := HashMD5.HashAsString;

end;

 

function GetFileHashSHA1(FileName: WideString): String;

var

  HashSHA: THashSHA1;

  Stream: TStream;

  Readed: Integer;

  Buffer: PByte;

  BufLen: Integer;

begin

  HashSHA := THashSHA1.Create;

  BufLen := 16 * 1024;

  Buffer := AllocMem(BufLen);

  try

    Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);

    try

      while Stream.Position < Stream.Size do

      begin

        Readed := Stream.Read(Buffer^, BufLen);

        if Readed > 0 then

        begin

          HashSHA.update(Buffer^, Readed);

        end;

      end;

    finally

      Stream.Free;

    end;

  finally

    FreeMem(Buffer)

  end;

  result := HashSHA.HashAsString;

end;

 

 

function GetFileHashSHA224(FileName: WideString): String;

var

  HashSHA: THashSHA2;

  Stream: TStream;

  Readed: Integer;

  Buffer: PByte;

  BufLen: Integer;

begin

  HashSHA := THashSHA2.Create(SHA224);

  BufLen := 16 * 1024;

  Buffer := AllocMem(BufLen);

  try

    Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);

    try

      while Stream.Position < Stream.Size do

      begin

        Readed := Stream.Read(Buffer^, BufLen);

        if Readed > 0 then

        begin

          HashSHA.update(Buffer^, Readed);

        end;

      end;

    finally

      Stream.Free;

    end;

  finally

    FreeMem(Buffer)

  end;

  result := HashSHA.HashAsString;

end;

 

function GetFileHashSHA256(FileName: WideString): String;

var

  HashSHA: THashSHA2;

  Stream: TStream;

  Readed: Integer;

  Buffer: PByte;

  BufLen: Integer;

begin

  HashSHA := THashSHA2.Create(SHA256);

  BufLen := 16 * 1024;

  Buffer := AllocMem(BufLen);

  try

    Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);

    try

      while Stream.Position < Stream.Size do

      begin

        Readed := Stream.Read(Buffer^, BufLen);

        if Readed > 0 then

        begin

          HashSHA.update(Buffer^, Readed);

        end;

      end;

    finally

      Stream.Free;

    end;

  finally

    FreeMem(Buffer)

  end;

  result := HashSHA.HashAsString;

end;

 

function GetFileHashSHA384(FileName: WideString): String;

var

  HashSHA: THashSHA2;

  Stream: TStream;

  Readed: Integer;

  Buffer: PByte;

  BufLen: Integer;

begin

  HashSHA := THashSHA2.Create(SHA384);

  BufLen := 16 * 1024;

  Buffer := AllocMem(BufLen);

  try

    Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);

    try

      while Stream.Position < Stream.Size do

      begin

        Readed := Stream.Read(Buffer^, BufLen);

        if Readed > 0 then

        begin

          HashSHA.update(Buffer^, Readed);

        end;

      end;

    finally

      Stream.Free;

    end;

  finally

    FreeMem(Buffer)

  end;

  result := HashSHA.HashAsString;

end;

 

function GetFileHashSHA512(FileName: WideString): String;

var

  HashSHA: THashSHA2;

  Stream: TStream;

  Readed: Integer;

  Buffer: PByte;

  BufLen: Integer;

begin

  HashSHA := THashSHA2.Create(SHA512);

  BufLen := 16 * 1024;

  Buffer := AllocMem(BufLen);

  try

    Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);

    try

      while Stream.Position < Stream.Size do

      begin

        Readed := Stream.Read(Buffer^, BufLen);

        if Readed > 0 then

        begin

          HashSHA.update(Buffer^, Readed);

        end;

      end;

    finally

      Stream.Free;

    end;

  finally

    FreeMem(Buffer)

  end;

  result := HashSHA.HashAsString;

end;

 

function GetFileHashSHA512_224(FileName: WideString): String;

var

  HashSHA: THashSHA2;

  Stream: TStream;

  Readed: Integer;

  Buffer: PByte;

  BufLen: Integer;

begin

  HashSHA := THashSHA2.Create(SHA512_224);

  BufLen := 16 * 1024;

  Buffer := AllocMem(BufLen);

  try

    Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);

    try

      while Stream.Position < Stream.Size do

      begin

        Readed := Stream.Read(Buffer^, BufLen);

        if Readed > 0 then

        begin

          HashSHA.update(Buffer^, Readed);

        end;

      end;

    finally

      Stream.Free;

    end;

  finally

    FreeMem(Buffer)

  end;

  result := HashSHA.HashAsString;

end;

 

function GetFileHashSHA512_256(FileName: WideString): String;

var

  HashSHA: THashSHA2;

  Stream: TStream;

  Readed: Integer;

  Buffer: PByte;

  BufLen: Integer;

begin

  HashSHA := THashSHA2.Create(SHA512_256);

  BufLen := 16 * 1024;

  Buffer := AllocMem(BufLen);

  try

    Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);

    try

      while Stream.Position < Stream.Size do

      begin

        Readed := Stream.Read(Buffer^, BufLen);

        if Readed > 0 then

        begin

          HashSHA.update(Buffer^, Readed);

        end;

      end;

    finally

      Stream.Free;

    end;

  finally

    FreeMem(Buffer)

  end;

  result := HashSHA.HashAsString;

end;

 

function GetFileHashBobJenkins(FileName: WideString): String;

var

  Hash: THashBobJenkins;

  Stream: TStream;

  Readed: Integer;

  Buffer: PByte;

  BufLen: Integer;

begin

  Hash := THashBobJenkins.Create;

  BufLen := 16 * 1024;

  Buffer := AllocMem(BufLen);

  try

    Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);

    try

      while Stream.Position < Stream.Size do

      begin

        Readed := Stream.Read(Buffer^, BufLen);

        if Readed > 0 then

        begin

          Hash.update(Buffer^, Readed);

        end;

      end;

    finally

      Stream.Free;

    end;

  finally

    FreeMem(Buffer)

  end;

  result := Hash.HashAsString;

end;

posted @ 2021-09-23 14:41  熊大熊二  阅读(201)  评论(0编辑  收藏  举报