Skip to content

Instantly share code, notes, and snippets.

@jeffbowman
Created February 25, 2016 21:57
Show Gist options
  • Save jeffbowman/5532aedad3a0f0948f06 to your computer and use it in GitHub Desktop.
Save jeffbowman/5532aedad3a0f0948f06 to your computer and use it in GitHub Desktop.
Overwrite a file with random bits for secure file deletion
#lang racket/base
(require racket/list
racket/sequence)
(define MAX-OVERWRITE 8)
(define overwrite-range (range MAX-OVERWRITE))
(define (get-random-bytes count)
(with-input-from-file "/dev/urandom"
(λ () (read-bytes count))))
(define (wipe-file fname)
(let ([fsize (file-size fname)])
(printf "wiping ~a : ~a bytes~n" fname fsize)
(for ([i overwrite-range])
(with-output-to-file fname
(λ () (write-bytes (get-random-bytes fsize)))
#:exists 'replace))))
(define (process-files f)
(cond
[(file-exists? f) (wipe-file f)]
[(directory-exists? f)
(for ([file (in-directory f)])
(when (file-exists? file) (wipe-file file)))]
[else (raise (make-exn:fail:filesystem:exists
(format "failed to find ~a" f)
(current-continuation-marks)))]))
(define (main)
(let ([args (current-command-line-arguments)])
(with-handlers
([exn:fail:unsupported?
(λ (exn) (format "ERROR: ~a" (exn-message exn)))]
[exn:fail:filesystem:exists?
(λ (exn) (exn-message exn))])
(when (= 0 (sequence-length args))
(raise (make-exn:fail:unsupported
"a file or directory must be provided"
(current-continuation-marks))))
(for ([arg args])
(process-files arg)))))
(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment