Murphy Mac » Page 'Create an openssl Function'

Create an openssl Function

Remote Login With SSHThis is a quick way to make encrypting files using the terminal fast and convenient. Murphy posted instructions on making an interactive shell script to do essentially the same thing. This is a slightly different spin. Skip ahead to the screencast to see how easy file encryption can be. The openssl command we’re using is included with OS X.

Like other things we’ve covered - the specific example might not apply to you - but for people who’ve never used a function it might be helpful. Functions can make complicated Terminal commands more convenient to use.

To create the function just add this line to the .bash_profile file in your home directory:

des3() { openssl des3 -salt -in "$1" -out "$2"; }

Note that there’s a space after the opening curly bracket and a space before the closing curly bracket. All we need to remember is the function name, and to provide two file names: one to encrypt and one to be the output file.

The breakdown on the command: the first des3 is what we named the function. We can name it anything but des3 is what Murphy chose. The name of the function is what you’ll type whenever you use it.

The stuff in the curly brackets is what happens when we invoke the function. See this post for more on the openssl command.

The des3 following the openssl command is the type of encryption we’re using. It’s part of the openssl command syntax. We added $1 and $2 after the in and out respectively because they’re the two pieces of information we need when we invoke our function.

The $1 and $2 will be replaced with the paths we type into Terminal. In the screencast Murphy shows how to invoke the function. Instead of typing the paths he drags the file to be encrypted into the window - which saves us the typing. He also names the output file with a des3 extension to remind himself how he encrypted the input file.

As always, be careful with the Terminal if you’re not familiar with it. See Murphy’s warning about the dangerous possibilities.

Share/Save/Bookmark

Watch Screencast | Permalink

One comment to “Create an openssl Function”

  1. Thanks. I used your example to write AES encrypt and decrypt functions. Note that only one file needs to be supplied and .aes is automatically appended in encrypt and stripped in decrypt:

    function encrypt {
    if [ "$1" = "" ]; then
    echo “Usage: encrypt filename”
    else
    /usr/bin/openssl aes-256-cbc -salt -in “$1″ -out “$1″.aes
    if [ $? -eq 0 ] ; then
    echo “encryted file: “$1″.aes”
    fi
    fi
    }

    function decrypt {
    if [ "$1" = "" ]; then
    echo “Usage: decrypt filename”
    else
    /usr/bin/openssl aes-256-cbc -d -salt -in “$1″ -out “$(dirname “$1″)/$(basename “$1″ .aes)”
    if [ $? -eq 0 ] ; then
    echo “decryted file: $(dirname “$1″)/$(basename “$1″ .aes)”
    else
    /bin/rm “$(dirname “$1″)/$(basename “$1″ .aes)”
    fi
    fi
    }

Leave a comment