21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

if (pattern_pass(fd, buf, sizeof(buf), st.st_size) = = -1) return -1;<br />

}<br />

for (pass = 0; pass < sizeof(triple_pats) / patternsz; pass++) {<br />

pattern = triple_pats[pass];<br />

count = sizeof(buf) / patternsz;<br />

for (i = 0; i < count; i++)<br />

memcpy(buf + (i * patternsz), pattern, patternsz);<br />

if (pattern_pass(fd, buf, patternsz * count, st.st_size) = = -1) return -1;<br />

}<br />

for (pass = 0; pass < 4; pass++)<br />

if (random_pass(fd, st.st_size) = = -1) return -1;<br />

return 0;<br />

}<br />

int spc_file_wipe(FILE *f) {<br />

return spc_fd_wipe(fileno(f));<br />

}<br />

The Unix implementations should work on Windows systems using the standard C<br />

runtime API; however, it is rare that the standard C runtime API is used on Windows.<br />

The following code implements SpcWipeFile( ), which is virtually identical to<br />

the standard C version except that it uses only Win32 APIs for file access.<br />

#include <br />

#include <br />

#define SPC_WIPE_BUFSIZE 4096<br />

static BOOL RandomPass(HANDLE hFile, HCRYPTPROV hProvider, DWORD dwFileSize)<br />

{<br />

BYTE pbBuffer[SPC_WIPE_BUFSIZE];<br />

DWORD cbBuffer, cbTotalWritten, cbWritten;<br />

if (SetFilePointer(hFile, 0, 0, FILE_BEGIN) = = 0xFFFFFFFF) return FALSE;<br />

while (dwFileSize > 0) {<br />

cbBuffer = (dwFileSize > sizeof(pbBuffer) ? sizeof(pbBuffer) : dwFileSize);<br />

if (!CryptGenRandom(hProvider, cbBuffer, pbBuffer)) return FALSE;<br />

for (cbTotalWritten = 0; cbBuffer > 0; cbTotalWritten += cbWritten)<br />

if (!WriteFile(hFile, pbBuffer + cbTotalWritten, cbBuffer - cbTotalWritten,<br />

&cbWritten, 0)) return FALSE;<br />

dwFileSize -= cbTotalWritten;<br />

}<br />

return TRUE;<br />

}<br />

static BOOL PatternPass(HANDLE hFile, BYTE *pbBuffer, DWORD cbBuffer, DWORD<br />

dwFileSize) {<br />

DWORD cbTotalWritten, cbWrite, cbWritten;<br />

if (!cbBuffer || SetFilePointer(hFile, 0, 0, FILE_BEGIN) = = 0xFFFFFFFF) return<br />

FALSE;<br />

while (dwFileSize > 0) {<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.<br />

Erasing Files Securely | 51

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!