Yo utilizo las funciones de las Indy...
Código Delphi
[-]
unit CLCalcHash;
interface
Uses System.Classes, System.SysUtils, IdGlobal, IdHash, IdHashMessageDigest,
IdHashSHA, IdSSLOpenSSL;
type
THashAlgorithm = ( haMD5, haSHA1, haSHA256, haSHA384, haSHA512);
function CalcHashText(Algorithm:THashAlgorithm; Text:String; Key:String=''):string;
function CalcMd5(Text:String; Key:String=''):String;
function CalcSha1(Text:String; Key:String=''):String;
function CalcSha256(Text:String; Key:String=''):String;
function CalcSha384(Text:String; Key:String=''):String;
function CalcSha512(Text:String; Key:String=''):String;
function CalcHashFile(Algorithm:THashAlgorithm; Filename:String; Key:String=''):string;
implementation
function CalcHashFile(Algorithm:THashAlgorithm; Filename:String; Key:String=''):string;
var
Stream: TFileStream;
hash:TIdHash;
begin
Result:= '';
if not FileExists(Filename) then
Exit;
Stream := TFileStream.Create(Filename, fmOpenRead);
case Algorithm of
haMD5 :hash:=TIdHashMessageDigest5.Create;
haSHA1 :hash:=TIdHashSHA1.Create;
haSHA256:hash:=TIdHashSHA256.Create;
haSHA384:hash:=TIdHashSHA384.Create;
haSHA512:hash:=TIdHashSHA512.Create;
end;
try
with hash do
try
Result := HashStreamAsHex(Stream);
finally
Free;
end;
finally
Stream.Free;
end;
end;
function CalcMd5(Text:String; Key:String=''):String;
begin
result:=CalcHashText(haMD5, Text, Key);
end;
function CalcSha1(Text:String; Key:String=''):String;
begin
result:=CalcHashText(haSha1, Text, Key);
end;
function CalcSha256(Text:String; Key:String=''):String;
begin
result:=CalcHashText(haSha256, Text, Key);
end;
function CalcSha384(Text:String; Key:String=''):String;
begin
result:=CalcHashText(haSha384, Text, Key);
end;
function CalcSha512(Text, Key:String):String;
begin
result:=CalcHashText(haSha512, Text, Key);
end;
function CalcHashText(Algorithm: THashAlgorithm; Text:String; Key:String=''):string;
var
hash:TIdHash;
begin
case Algorithm of
haMD5 :hash:=TIdHashMessageDigest5.Create;
haSHA1 :hash:=TIdHashSHA1.Create;
haSHA256:hash:=TIdHashSHA256.Create;
haSHA384:hash:=TIdHashSHA384.Create;
haSHA512:hash:=TIdHashSHA512.Create;
end;
with hash do
try
Result := HashStringAsHex(iif(Key='',Text, Key+Text));
finally
Free;
end;
end;
initialization
LoadOpenSSLLibrary();
end.
Saludos cordiales