Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Debates (https://www.clubdelphi.com/foros/forumdisplay.php?f=29)
-   -   Componentes SDean (https://www.clubdelphi.com/foros/showthread.php?t=93664)

mlara 23-12-2018 15:31:24

Componentes SDean
 
Hola... alguien sabe qué pasó con los SDeanComponents? Qué componentes recomendarían para obtener cadenas Hash?

Casimiro Notevi 23-12-2018 20:37:41

No los conocía, puedes descargarlo de github.
Aunque veo que son de 2014
Creo que si solamente necesitas el hash de una cadena, delphi trae ya opción para eso.

cloayza 27-12-2018 16:28:19

Yo utilizo las funciones de las Indy...:D
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

cloayza 27-12-2018 20:57:06

El compañero MAJ, habían dado una excelente solución...

System.Hash

Saludos cordiales


La franja horaria es GMT +2. Ahora son las 05:55:40.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi