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( ' )); //2566ca7678fcdb309846eabef3911dc6e5f8814d Memo1.Lines.Add(sha2_utf8( ' )); //f3bbbfaf81071d67f02fd519553789c671c7e0514045885e5bd8faa80d66792f Memo1.Lines.Add(md5_hmac( ' , '钥匙' )); //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;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?