unit DEC.Hashing;
interface
uses
System.Classes, System.SysUtils, DECHash, DECFormat;
type
THashBaseMD4Class =
class of THashBaseMD4;
type
THashFunctions =
record
private
class function HashStringBase(
const Text:
string;
const HashClass: THashBaseMD4Class):
string;
static;
class function HashFileBase(
const FileName:
string;
const HashClass: THashBaseMD4Class):
string;
static;
class function HashStreamBase(
const Stream: TStream;
const HashClass: THashBaseMD4Class):
string;
static;
public
class function MD5String(
const Text:
string):
string;
static;
class function MD5File(
const FileName:
string):
string;
static;
class function MD5Stream(
const Stream: TStream):
string;
static;
class function SHA256String(
const Text:
string):
string;
static;
class function SHA256File(
const FileName:
string):
string;
static;
class function SHA256Stream(
const Stream: TStream):
string;
static;
end;
implementation
// Base functions
// ==============================================================================================================================================
class function THashFunctions.HashStringBase(
const Text:
string;
const
HashClass: THashBaseMD4Class):
string;
var
Hash: THashBaseMD4;
begin
Hash := HashClass.Create;
try
Result :=
string(Hash.CalcString(RawByteString(Text), TFormat_HEX)).ToLower;
finally
Hash.Free;
end;
end;
class function THashFunctions.HashFileBase(
const FileName:
string;
const
HashClass: THashBaseMD4Class):
string;
var
Hash: THashBaseMD4;
begin
Hash := HashClass.Create;
try
Result :=
string(Hash.CalcFile(FileName, TFormat_HEX)).ToLower;
finally
Hash.Free;
end;
end;
class function THashFunctions.HashStreamBase(
const Stream: TStream;
const
HashClass: THashBaseMD4Class):
string;
var
Hash: THashBaseMD4;
begin
Hash := HashClass.Create;
try
Result :=
string(Hash.CalcStream(Stream, Stream.Size, TFormat_HEX)).ToLower;
finally
Hash.Free;
end;
end;
// ==============================================================================================================================================
// MD5
// ==============================================================================================================================================
class function THashFunctions.MD5String(
const Text:
string):
string;
begin
Result := HashStringBase(Text, THash_MD5);
end;
class function THashFunctions.MD5File(
const FileName:
string):
string;
begin
Result := HashFileBase(FileName, THash_MD5);
end;
class function THashFunctions.MD5Stream(
const Stream: TStream):
string;
begin
Result := HashStreamBase(Stream, THash_MD5);
end;
// ==============================================================================================================================================
// SHA256
// ==============================================================================================================================================
class function THashFunctions.SHA256String(
const Text:
string):
string;
begin
Result := HashStringBase(Text, THash_SHA256);
end;
class function THashFunctions.SHA256File(
const FileName:
string):
string;
begin
Result := HashFileBase(FileName, THash_SHA256);
end;
class function THashFunctions.SHA256Stream(
const Stream: TStream):
string;
begin
Result := HashStreamBase(Stream, THash_SHA256);
end;
// ==============================================================================================================================================
end.