AGB  ·  Datenschutz  ·  Impressum  







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

DEC 5.2 String hashen?

Ein Thema von a.def · begonnen am 2. Mai 2017 · letzter Beitrag vom 7. Mai 2017
 
a.def
(Gast)

n/a Beiträge
 
#24

AW: DEC 5.2 String hashen?

  Alt 3. Mai 2017, 17:58
Danke für die Hilfe. Ein paar Seiten vorher hatte ich meine Unit schon einmal vorgestellt die genau das auch macht Mein letztes Problem war nur das Setzen des Hashes mitten in die Datei.
Meine Primitive Lösung dafür ist

Delphi-Quellcode:
var
 aPrefixSuffixLen: Byte = 4;
 iHashLengthInBytes = 64;
 a: RawByteString = '1234';
 b: RawByteString = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';

function doHashAction(aHashAction: THashAction): string;
var
 aByteStream, aByteStream2: TBytesStream;
 iPosRawByte, iPosEx: Int64;
 sHash, sTmp, sContent: string;
 sTmpRawByte, c: RawByteString;
begin
 Result := '';

 if not FileExists(aFileName) then
  Exit;

 aByteStream := TBytesStream.Create;
 try
  aByteStream.LoadFromFile(aFileName);

  case aHashAction of
   THashAction.addHashToFile:
    begin
     c := a + b + a;
     sTmpRawByte := c;
    end
  else
   sTmpRawByte := a;
  end;

  iPosRawByte := Pos(RawByteString(sTmpRawByte), RawByteString(aByteStream.Bytes));
  if iPosRawByte > 0 then
   begin
    iPosRawByte := iPosRawByte + Length(a);

    case aHashAction of
     THashAction.addHashToFile:
      begin
       // This code adds a hash of the original file content to the destination files position of RawByteString
       // ==============================================================================================================================================
       aByteStream2 := TBytesStream.Create;
       try
        aByteStream2.Write(aByteStream.Bytes[0], iPosRawByte - 1);
        aByteStream2.Write(aByteStream.Bytes[iPosRawByte + Length(c) - (aPrefixSuffixLen * 2 + 1)], aByteStream.Size - (iPosRawByte + (Length(c) - (aPrefixSuffixLen * 2 + 1))));
        sHash := System.Hash.THashSHA2.GetHashString(Trim(TEncoding.ANSI.GetString(aByteStream2.Bytes)));

        Move(AnsiString(sHash)[1], aByteStream.Bytes[iPosRawByte - 1], Length(c) - aPrefixSuffixLen * 2);
        aByteStream.SaveToFile(aFileName);
       finally
        aByteStream2.Free;
       end;
       // ==============================================================================================================================================
      end;
     THashAction.getHashedFileContent:
      begin
       // This code reads the original file content and calculates the hash
       // ==============================================================================================================================================
       iPosEx := Pos(RawByteString(a), RawByteString(aByteStream.Bytes), iPosRawByte + 1);

       sTmp := Trim(TEncoding.ANSI.GetString(aByteStream.Bytes));
       sContent := Copy(sTmp, 0, iPosRawByte - 1);
       sContent := System.Hash.THashSHA2.GetHashString(sContent + Copy(sTmp, iPosEx, Length(sTmp)));

       Result := sContent;
       ShowMessage(Result);
       // ==============================================================================================================================================
      end;
     THashAction.getAddedHash:
      begin
       // This code reads the hash added at the position of RawByteString
       // ==============================================================================================================================================
       aByteStream.Position := iPosRawByte - 1;
       aByteStream.Read(aByteStream.Bytes[0], iHashLengthInBytes);

       Result := Trim(TEncoding.ANSI.GetString(aByteStream.Bytes, 0, iHashLengthInBytes));
       ShowMessage(Result);
       // ==============================================================================================================================================
      end;
     THashAction.doCompareHashes:
      begin
       // ==============================================================================================================================================
       ShowMessage(BoolToStr(doHashAction(THashAction.getHashedFileContent) = doHashAction(THashAction.getAddedHash), True));
       // ==============================================================================================================================================
      end;
    end;
   end;
 finally
  aByteStream.Free;
 end;
end;
Delphi-Quellcode:
 // Hash hinzufügen
 doHashAction(THashAction.addHashToFile);

 // Inhalt gehasht auslesen
 doHashAction(THashAction.getHashedFileContent);

 // Hinzugefügten Hash auslesen
 doHashAction(THashAction.getAddedHash);

 // Selftest ausführen
 doHashAction(THashAction.doCompareHashes);
 
 


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 02:03 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