APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Powerful crypto from the UNIX command line

by Girish Venkatachalam

Girish Venkatachalam is a UNIX hacker with more than a decade of networking and crypto programming experience. His hobbies include yoga,cycling, cooking and he runs his own business. Details here:


I will be really surprised if a linux distro ships without OpenSSL pre installed. I know that it comes by default since it uses a very liberal license. The importance of OpenSSL toolkit for crypto cannot be overestimated.

OpenSSH project relies on OpenSSL for its backend crypto operations. Though named as an SSL library, it is a very comprehensive toolkit with all kinds of facilities related to cryptography. Even simple things like random number generation, base64 conversion, file integrity checking with SHA1 or prime number generation can be done with the OpenSSL command line tool.

OpenSSL is a crypto toolkit and it produces two libraries libcrypto.so and libssl.so. You could also link to it statically. The main purpose of the OpenSSL project is to provide library facilities for various operations related to SSL. But over time it has evolved into a fantastic command line utility that gets our job done as long as we know a thing or two about crypto.

Knowing a thing or two about crypto is not so easy. Most people have no idea what is meant by PKI. Most people do not understand why RSA keys are 1024 bits and why AES keys are 256 bits. How can the AES key be stronger? Crypto theory is very deep and mathematical in nature. And OpenSSL programming requires very advanced C skills. I remember how I struggled when I worked with the guts of OpenSSL. But for this article, we only want to look at some of the really useful features offered by such a rich toolkit. All from the command line.

OpenSSL also has a shell interface which gets invoked when you type openssl like this:

$ openssl                                                                      

But we do not have to go inside the shell at all. We can use command line switches for most of our needs. Nowadays you also have GnuPG for performing command line operations like file encryption and message signing. OpenSSL also comes in very handy when you wish to generate server certificates for your Apache HTTPS service. Or you might wish to create a public keypair for accessing your IMAP over SSL account or SMTP over SSL e-mail account.

Let us take a few simple examples.

File integrity checks help us detect file corruption and change. We want to detect a truncated upload or download. We also want to know whether a binary file has changed. With text files you can always use diff. Diff also is useful with binary data but the best approach would be to use a strong fingerprinting algorithm like SHA1 checksum.

There is a command called sha1sum on most linux boxes similar to the cksum utility of yore. OpenSSL can also do the same thing with this command.

$ openssl sha1 /etc/passwd                                                     
SHA1(/etc/passwd)= 61293afc53dd8465a28d49fc1d11676badcd0076

A SHA1 digest output has a constant length of 160 bits and it is in a binary format. This output has 40 bytes because the binary output is Hex encoded. BCD representation takes one byte of binary data and represents it as two ASCII bytes. Hence the output here is 40 characters/bytes.

Let us generate some random data just for fun.

$ openssl rand 1024

Oops. There is a problem here. Your screen gets garbled. What to do? We do not want raw binary data spat to our beautiful terminal. Instead we want to do one of 3 things. We could get a Hex encoded output like above, we could base64 encode it or we could write it to a file by redirection or ask OpenSSL itself to do it with the -out switch.

Base64 encoding is different from the BCD hex representation we saw above. OpenSSL is good with ASN1 encoding and Base64 encoding in addition to crypto stuff. Base64 uses 3 bytes of binary data and create 4 ASCII characters as output. So there is a fixed 33% increase in filesize when you base64 encode a file. Of course you have padding and other variations, but this is the simple math. 3 x 8 = 4 x 6 = 24.

Base64 output uses 2^6 or 64 ASCII characters to represent a byte. Nowadays Base64 encoding is used everywhere. A good example is for e-mail authentication protocols. SMTP authentication uses DIGEST authentication using base64 encoding of passwords.

Evidently OpenSSL is a lot more than this. I hope this gets you started.


  1. OpenSSL homepage
  2. GPG homepage

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Powerful crypto from the UNIX command line


More Articles by

Click here to add your comments
- no registration needed!

Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments

If you want a picture to show with your comment, go get a Gravatar
Kerio Connect Mailserver

Kerio Samepage

Kerio Control Firewall

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Jump to Comments

Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

I am a Kerio reseller. Articles here related to Kerio products reflect my honest opinion, but I do have an obvious interest in selling those products also.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.

This post tagged: