Einzelnen Beitrag anzeigen

DieDolly

Registriert seit: 22. Jun 2018
2.175 Beiträge
 
#15

AW: DEC 6.1 generelle Fragen

  Alt 16. Feb 2021, 00:31
Habe das jetzt etwas umständlich gemacht aber es funktioniert. Mein Aufruf bleibt der gleiche.

Delphi-Quellcode:
unit DEC.Hashing;

interface

uses
 System.Classes, System.SysUtils, DECHash, DECFormat;

type
 THashMethod = (hmMD5, hmSHA256);

type
 THashFunctions = record
 private
  class function GetHash(const HashMethod: THashMethod): THashBaseMD4; static;
  class function HashStringBase(const Text: string; const HashMethod: THashMethod): string; static;
  class function HashFileBase(const FileName: string; const HashMethod: THashMethod): string; static;
  class function HashStreamBase(const Stream: TStream; const HashMethod: THashMethod): 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.GetHash(const HashMethod: THashMethod): THashBaseMD4;
begin
 Result := nil;
 case HashMethod of
  hmMD5:
   Result := THash_MD5.Create;
  hmSHA256:
   Result := THash_SHA256.Create;
 end;
end;

class function THashFunctions.HashStringBase(const Text: string; const HashMethod: THashMethod): string;
var
 Hash: THashBaseMD4;
begin
 Hash := THashFunctions.GetHash(HashMethod);
 try
  Result := string(Hash.CalcString(RawByteString(Text), TFormat_HEX)).ToLower;
 finally
  Hash.Free;
 end;
end;

class function THashFunctions.HashFileBase(const FileName: string; const HashMethod: THashMethod): string;
var
 Hash: THashBaseMD4;
begin
 Hash := THashFunctions.GetHash(HashMethod);
 try
  Result := string(Hash.CalcFile(FileName, TFormat_HEX)).ToLower;
 finally
  Hash.Free;
 end;
end;

class function THashFunctions.HashStreamBase(const Stream: TStream; const HashMethod: THashMethod): string;
var
 Hash: THashBaseMD4;
begin
 Hash := THashFunctions.GetHash(HashMethod);
 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, THashMethod.hmMD5);
end;

class function THashFunctions.MD5File(const FileName: string): string;
begin
 Result := HashFileBase(FileName, THashMethod.hmMD5);
end;

class function THashFunctions.MD5Stream(const Stream: TStream): string;
begin
 Result := HashStreamBase(Stream, THashMethod.hmMD5);
end;
// ==============================================================================================================================================

// SHA256
// ==============================================================================================================================================
class function THashFunctions.SHA256String(const Text: string): string;
begin
 Result := HashStringBase(Text, THashMethod.hmSHA256);
end;

class function THashFunctions.SHA256File(const FileName: string): string;
begin
 Result := HashFileBase(FileName, THashMethod.hmMD5);
end;

class function THashFunctions.SHA256Stream(const Stream: TStream): string;
begin
 Result := HashStreamBase(Stream, THashMethod.hmMD5);
end;
// ==============================================================================================================================================

end.
  Mit Zitat antworten Zitat