Newsgroups : Borland : borland.public.delphi.rtl.win32 : 2005 Apr : Wiping files securely

www.cryer.info
Managed Newsgroup Archive

Wiping files securely

Subject:Wiping files securely
Posted by:"Glenn Alcott" (galco..@compuserve.com)
Date:Sun, 17 Apr 2005 11:26:20

I need a routine to securely wipe out a file and have found a couple that
are supposed to do this. But I'm a little concerned about how secure they
are and whether they will really wipe out all traces of the file. The two
routines are listed here and I would appreciate any opinions on which is
better, what possible weaknesses they have, or possible alternatives.

Glenn

-------------

procedure FileWipe(FileName: string, Times: integer);
// Adapted from GX Explorer
var
  StringStreamBuffer: TStringStream;
  n, intFileSize: integer;
  OverwriteChar: char;
begin
for n:=1 to Times do begin
   OverwriteChar:=chr(n);
    with TFileStream.Create(FileName, fmOpenReadWrite or fmShareExclusive)
do begin
      try
        intFileSize := Size;
        StringStreamBuffer :=
TStringStream.Create(StringOfChar(OverwriteChar, intFileSize));
        try
          CopyFrom(StringStreamBuffer, intFileSize);
        finally
          StringStreamBuffer.Free;
        end;
      finally
        Free;
      end;
    end;
  end;
DeleteFile(PChar(FileName));
end;

------------------

procedure ShredFile(const FileName: string; Times: Integer);
// From Jedi JCL code library
const
  BUFSIZE   = 4096;
  ODD_FILL  = $C1;
  EVEN_FILL = $3E;
var
  Fs: TFileStream;
  Size: Integer;
  N: Integer;
  ContentPtr: Pointer;
begin
  Size := FileGetSize(FileName);
  if Size > 0 then
  begin
    if Times < 0 then
      Times := 2
    else
      Times := Times * 2;
    ContentPtr := nil;
    Fs := TFileStream.Create(FileName, fmOpenReadWrite);
    try
      GetMem(ContentPtr, BUFSIZE);
      while Times > 0 do
      begin
        if Times mod 2 = 0 then
          FillMemory(ContentPtr, BUFSIZE, EVEN_FILL)
        else
          FillMemory(ContentPtr, BUFSIZE, ODD_FILL);
        Fs.Seek(0, soFromBeginning);
        N := Size div BUFSIZE;
        while N > 0 do
        begin
          Fs.Write(ContentPtr^, BUFSIZE);
          Dec(N);
        end;
        N := Size mod BUFSIZE;
        if N > 0 then
          Fs.Write(ContentPtr^, N);
        FlushFileBuffers(Fs.Handle);
        Dec(Times);
      end;
    finally
      if ContentPtr <> nil then
        FreeMem(ContentPtr, Size);
      Fs.Free;
      DeleteFile(FileName);
    end;
  end
  else
    DeleteFile(FileName);
end;

Replies:

www.cryer.info
Managed Newsgroup Archive