Funciones para calcular el hash de strings y archivos

A continuación tienen una serie de funciones para calcular el hash (MD5, SHA1, SHA224, SHA256, SHA 384, SHA 512, BobJenkins) de strings y de archivos.

Probado en RAD Studio 10 - Tokyo.

Recordar que hay que añadir en el bloque "USES" la unit "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;




Ejemplos del cálculo de hash en archivos:




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;









No hay comentarios:

Publicar un comentario

Simulación del movimiento de los electrones en un campo electrico

Espectacular simulación realizada con OpenGL del movimiento de los electrones cuando atraviesan un campo eléctrico. Como muestra la image...