Skip to content

Instantly share code, notes, and snippets.

@amintos
Created September 6, 2012 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amintos/3655714 to your computer and use it in GitHub Desktop.
Save amintos/3655714 to your computer and use it in GitHub Desktop.
Securely erases free disk space on small disks (flash drives, etc.)
import os
import time
ITERATIONS = 3 # Number of passes
BLOCKSIZE = 1 << 20 # Bytes to be written at once
INFO_SECONDS = 3 # Interval in which output is displayed
FILE = 'crap' # File where random output is dumped
def format_bytes(n):
'''Human readable representation of bytes'''
n = float(n)
if n < 1024: return '%s Bytes' % n
elif n < 1024 * 1024: return '%s KB' % round(n / 1024, 2)
elif n < 1024 * 1024 * 1024: return '%s MB' % round(n / 1024 / 1024, 2)
else: return '%s GB' % round(n / 1024 / 1024 / 1024, 2)
def print_info(bytes_written, bytes_recently, last_info_time):
print "%s written at" % format_bytes(bytes_written),
print "%s/s" % format_bytes(round(
bytes_recently / (time.time() - last_info_time), 1))
def shred(iterations, filename, blocksize, info_seconds):
for i in xrange(iterations):
print "BEGIN: Pass %s of %s" % (i + 1, iterations)
bytes_written = 0
time_started = time.time()
last_info_time = time.time()
bytes_recently = 0
with file(filename, 'wb') as f:
while 1:
try:
f.write(os.urandom(blocksize))
except KeyboardInterrupt:
return
except:
print "DONE: Pass %s of %s" % (i + 1, iterations)
break
bytes_written += blocksize
bytes_recently += blocksize
if time.time() - last_info_time >= info_seconds:
print_info(bytes_written, bytes_recently, last_info_time)
last_info_time = time.time()
bytes_recently = 0
if __name__ == '__main__':
shred(ITERATIONS, FILE, BLOCKSIZE, INFO_SECONDS)
os.remove(FILE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment