Created
February 25, 2016 21:57
-
-
Save jeffbowman/5532aedad3a0f0948f06 to your computer and use it in GitHub Desktop.
Overwrite a file with random bits for secure file deletion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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