Einzelnen Beitrag anzeigen

TiGü

Registriert seit: 6. Apr 2011
Ort: Berlin
3.070 Beiträge
 
Delphi 10.4 Sydney
 
#18

AW: DEC 6.1 generelle Fragen

  Alt 16. Feb 2021, 12:49
Ich würde dir erstmal sowas vorschlagen:

Delphi-Quellcode:
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.
Aber das finde ich unelegant.
Denn es geht noch besser.

Delphi-Quellcode:
unit DEC.Hashing;

interface

uses
 System.Classes, System.SysUtils, DECHash, DECFormat,
 DECHashAuthentication; // <----- neue Unit, nicht vergessen, da kommt die Definition von TDECHashAuthentication her

type
 THashFunctions = record
 public
  class function HashString<HashClass: TDECHashAuthentication, constructor>(const Text: string): string; static;
  class function HashFile<HashClass: TDECHashAuthentication, constructor>(const FileName: string): string; static;
  class function HashStream<HashClass: TDECHashAuthentication, constructor>(const Stream: TStream): string; static;
 end;

implementation

// Base functions
// ==============================================================================================================================================
class function THashFunctions.HashString<HashClass>(const Text: string): string;
var
 Hash: TDECHashAuthentication;
begin
 Hash := HashClass.Create;
 try
  Result := string(Hash.CalcString(RawByteString(Text), TFormat_HEX)).ToLower;
 finally
  Hash.Free;
 end;
end;

class function THashFunctions.HashFile<HashClass>(const FileName: string): string;
var
 Hash: TDECHashAuthentication;
begin
 Hash := HashClass.Create;
 try
  Result := string(Hash.CalcFile(FileName, TFormat_HEX)).ToLower;
 finally
  Hash.Free;
 end;
end;

class function THashFunctions.HashStream<HashClass>(const Stream: TStream): string;
var
 Hash: TDECHashAuthentication;
begin
 Hash := HashClass.Create;
 try
  Result := string(Hash.CalcStream(Stream, Stream.Size, TFormat_HEX)).ToLower;
 finally
  Hash.Free;
 end;
end;

end.
Anzuwenden dann so:

Delphi-Quellcode:
var
  LStream: TStream;
  MyHash: string;
begin
  MyHash := THashFunctions.HashString<THash_MD5>('Hello');
  MyHash := THashFunctions.HashFile<THash_MD5>('World');
  LStream := TFileStream.Create('C:\temp\Project2.dpr', fmCreate);
  try
    MyHash := THashFunctions.HashStream<THash_MD5>(LStream);
  finally
    LStream.Free;
  end;

  MyHash := THashFunctions.HashString<THash_SHA256>('Hello');
  MyHash := THashFunctions.HashFile<THash_SHA256>('World');
  LStream := TFileStream.Create('C:\temp\Project2.dpr', fmCreate);
  try
    MyHash := THashFunctions.HashStream<THash_SHA256>(LStream);
  finally
    LStream.Free;
  end;
end.
Vorteil: Alle anderen Nachfahren von TDECHashAuthentication können können jetzt ganz einfach benutzt werden (THash_RipeMD128, THash_Tiger, THash_SHA512, THash_Sapphire...usw.)

Geändert von TiGü (16. Feb 2021 um 12:57 Uhr)
  Mit Zitat antworten Zitat