Ver Mensaje Individual
  #3  
Antiguo 27-12-2018
cloayza cloayza is offline
Miembro
 
Registrado: may 2003
Ubicación: San Pedro de la Paz, Chile
Posts: 933
Reputación: 23
cloayza Tiene un aura espectacularcloayza Tiene un aura espectacular
Yo utilizo las funciones de las Indy...
Código Delphi [-]
{
Autor: Christian Loayza
Descripcion: Permite calcular los valores hash utilizando los algoritmos
             haMD5, haSHA1, haSHA256, haSHA384, haSHA512.
             Para ello hace uso de las funciones contenidas en las Indy.
}
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
Responder Con Cita