![]() |
AW: DEC 6.1 generelle Fragen
Zitat:
|
AW: DEC 6.1 generelle Fragen
Ich habe jetzt nur mal schnell in GitHub direkt in den Sourcen geschaut. Versuch mal die
Delphi-Quellcode:
Klasse. Die sieht mir eigentlich nach der passenden Basisklasse aus. Mit
THashBaseMD4
Delphi-Quellcode:
bist du ja schon fast ganz oben angekommen.
TDECHash
|
AW: DEC 6.1 generelle Fragen
Gerade auch schon versucht.
Delphi-Quellcode:
function X(Hash: THashBaseMD4): string;
begin ... Result := Hash.CalcString('123'); ... end; showmessage(X(THash_MD5)); Zitat:
Eine sehr schlechte Umsetzung wäre das aber. |
AW: DEC 6.1 generelle Fragen
Naja du musst schon eine Instanz und nicht die Klasse übergeben. Ich habe mal schnell ein Testprojekt zusammen geknüppelt. Kompiliert zumindest einwandfrei. Auch zur Laufzeit kommen bei mir keinerlei Fehler.
Delphi-Quellcode:
program Project1;
{$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, DECHash; type THashTest = class private public function HashData(HashClass: THashBaseMD4): string; end; { THashTest } function THashTest.HashData(HashClass: THashBaseMD4): string; begin Result := HashClass.CalcString(''); end; var sha256: THash_SHA256; md5: THash_MD5; hashTest: THashTest; begin try sha256 := THash_SHA256.Create; md5 := THash_MD5.Create; hashTest := THashTest.Create; Writeln(hashTest.HashData(sha256)); Writeln(hashTest.HashData(md5)); Readln; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end. |
AW: DEC 6.1 generelle Fragen
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. |
AW: DEC 6.1 generelle Fragen
Und warum? Ich habe doch geschrieben wie es funktioniert.
Naja mach wie du denkst. Du musst ja mit dem Source Code klar kommen. |
AW: DEC 6.1 generelle Fragen
Ich verstehe nicht wie ich dein Beispiel in eine Unit packen soll. Ich brauche am Ende eine Unit wo ich von Außen nur aufzurufen brauche ob ich md5 oder sha256 möchte jeweils für string, datei und stream. Ich kann deinen Code jetzt 6x kopieren aber dann habe ich wieder doppelten Code.
|
AW: DEC 6.1 generelle Fragen
Ich würde dir erstmal sowas vorschlagen:
Delphi-Quellcode:
Aber das finde ich unelegant.
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. Denn es geht noch besser.
Delphi-Quellcode:
Anzuwenden dann so:
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.
Delphi-Quellcode:
Vorteil: Alle anderen Nachfahren von TDECHashAuthentication können können jetzt ganz einfach benutzt werden (THash_RipeMD128, THash_Tiger, THash_SHA512, THash_Sapphire...usw.)
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. |
AW: DEC 6.1 generelle Fragen
Danke an alle, die hier auch zur DEC Support leisten...
Top! Gerne weiter so... |
AW: DEC 6.1 generelle Fragen
@DieDolly: Bist du hier zu einer für dich brauchbaren Lösung gekommen?
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 21:27 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