Einzelnen Beitrag anzeigen

a.def
(Gast)

n/a Beiträge
 
#56

AW: DEC 5.2 String hashen?

  Alt 3. Mai 2017, 18: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);