Script to write random data to a hard drive using openssl

The script I posted on how to write random data to the hard drive was kinda buggy..

Here is a better version, that actually stop when it finishes. It also shows the progress and does some safety checking (It’s weird, but I actually don’t enjoy much of this. I like the feeling of having to be a lot careful at the console. Sorry, can’t explain)

I also removed a lot of quotes I found unnecessary (a thing that surely would chock the folks at #bash @ So I bet there is some bug hidden that I couldn’t spot. Use at your own risk 🙂


# The author of this script, Elias Amaral,
# claims no copyright over it.


msg() {
  printf "\n - $1\n\n" $2

mbs=4 # 4mb
blocksize=$(($mbs * 1024 * 1024))


if [[ -z $dev ]]; then
  msg "usage: $0 <device>"; exit
elif [[ ! -b $dev ]]; then
  msg "$dev: not a block device"; exit
elif [[ ! -w $dev ]]; then
  msg "$dev: no write permission"; exit
elif grep -q $dev /etc/mtab; then
  msg "$dev: mounted filesystem on device, omgomg!"; exit

cat <<end
This program writes random data to a hard disk.
It is intended to be used before storing encrypted data.
It may contain bugs (but seems to work for me).

It seems you have chosen to wipe data from the disk $dev.
Here is the partition table of this disk:

fdisk -l $dev

echo 'Are you sure you want to proceed?'


read -p 'Type uppercase yes if you want to proceed: ' q

if [[ $q != YES  ]]; then

  echo $i >
  mv step

  msg 'Writing at offset %s' $(($mbs * $i))M

  openssl rand \
          -rand /dev/urandom \
          $blocksize | \

  dd of=$dev \
     bs=$blocksize \
  let i++

msg Finished.

About Elias

Some random geek
This entry was posted in Linux and tagged , , , , . Bookmark the permalink.

One Response to Script to write random data to a hard drive using openssl

  1. Dave Webb says:

    Nice script. I still had some problems with the quotes. And I think you forget to initialise i with 0 before the while loop begins. And it wasn’t fast enough to produce data at full hard disk speed on my machine.

    Another idea is to seed a fast symmetric stream cipher with some random data as mentioned on
    and already mentioned in your first post.

    dd if=/dev/zero bs=1M | openssl bf-cbc -pass pass:`cat /dev/urandom | tr -dc [:graph:] | head -c56` | sudo dd of=$dev bs=1M

    This writes at about 90 MiByte/s to my disc (which is the maximum speed for the drive) and stops when the disk is full.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s