AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

DEC 6.1 generelle Fragen

Ein Thema von DieDolly · begonnen am 15. Feb 2021 · letzter Beitrag vom 23. Feb 2021
 
DieDolly

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

AW: DEC 6.1 generelle Fragen

  Alt 15. Feb 2021, 23: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
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 13:43 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz