Archive for the ‘TWS Software’ category

Hashing Is Not Encryption

October 14th, 2009

One of my pet peeves when talking with other programmers is when they use the wrong terminology. One of the most common ones that comes up for me is the issue of Encryption, and most of the time people are not encrypting, but hashing. And yes, there is a distinction.

What Hashing Is

Hashing is a mechanism for figuring out if two things are similar and is a one-way process. You take an object (file, string of text, ISO) and convert it to a fixed length string. You can then use this key to see if something else is the same thing.

The most common example of this is with large downloads. All the major Linux distributions will give an MD5 hash for their downloads so that you can verify that the file was not corrupted during transmission. You can run the ISO file through an MD5 generator which will give you back a 32 character string. If that string matches what Canonical says was their MD5, you have a match and your ISO is good.

In programming, one of the most common methods for hashing is password. In this case it is done for two reasons:

  1. You never, ever store passwords in Plaintext in a database
  2. You never should care what the user’s password is

Since #2 means you never need to know what the hash stands for, Hashing is a light-weight alternative to encryption while still providing security.

$password = md5($_POST['password']);
$username = $_POST['username'];
$result = mysql_query("SELECT username, password FROM user_accounts WHERE password = '$password' AND username = '$username'");
if( mysql_num_rows($result) == 1 ) {
echo 'Found a proper account!';
} else {
echo 'Invalid username and Password';
}

Salts

No, not Bacon Salt. Salts are additional bits of text you add to something before hashing to prevent someone from cracking the hashes that you are using.

$salt = 'ThisIsAReallyLongAndSecureString';
$hash = md5($_POST['password'] . $salt);
echo md5($_POST['password']) . ' != ' . $hash;

Salts are only useful if you are trying to use Hashing for security reasons, like storing passwords. If you are looking for just verification then a salt is useless. If you are using hashing for security purposes, ALWAYS USE A SALT.

Proper Hashing

Prior to PHP 5.1.2, the two most common ways to hash a file was to use either of the built-in hashing functions for MD5 or SHA1. Due to the fact that MD5 and SHA1 are considered insecure I don’t recommend using them even with a salt. You can generate a hash very easily using either MD5 or SHA1 and it should work on every single platform.

$sha1 = sha1('This is a string');
$md5 = md5('This is a string');

Since 5.1.2 PHP has added a proper hashing library that allows you to take advantage of more powerful algorithms. It is turned on by default, but if PHP is compiled by hand it can be disabled.

You can find out what algorithms are installed on a system by running the hash_algos() function. This returns an array of different algorithms that are installed on the system and that are available for use. These are also the names that can be used with the companion function hash(). So, if you wanted to store a password in the database the proper way using a hash, you would do something like the following:

// Our Salt
$salt = 'SuperSecretSaltNoOneWillEverFindOutAbout';
// The user-supplied, unhashed password and username
$usPassword = $_POST['password'];
$sUsername = mysql_real_escape_string($_POST['username']);
// Generate a SHA-384 hash using our salt
$sPassword = hash('sha384', $usPassword . $salt);
// Save it to the DB
mysql_query("UPDATE user_accounts SET password = '$sPassword' WHERE username = '$sUsername'");

Fine, what is Encryption then?

If Hashing is a one-way street, Encryption is two-way. You would use encryption whenever you need to safely store information but need to retrieve it later. (Again, you almost never need to know what a user’s password is, so why use encryption?) Encryption is also a heavier action than hashing.

Say we had a table, user_accounts, which had the following fields:

  • id
  • username
  • password
  • name
  • social_security_number
  • address_street
  • address_city
  • address_state
  • address_zip
  • cc_number
  • cc_type
  • cc_cvv
  • cc_exp_date

We would want to hash the password obviously. ID, username, and name we can probably not worry about doing anything too. Everything else though we would want to encrypt as all that information can be used in a very destructive manner if the database is stolen, but we need to use that data. Encryption would allow us to store that information in the database and sleep at night, yet still pull it back up when we needed it.

Keys

Keys, unlike Salts in hashes, are required. The key is used during the encryption process as a constant. If you use the wrong key you will not get back what you stored as the key is actually used for both encryption and decryption. Like a Salt, you’ll want to store this somewhere such as a file with limited read access.

For security reasons you will also want to look at a key rotation plan. One advantage that hashing has over encryption is that it is not meant to turn the random output of the hash back into something useful, where encryption is designed for just that purpose. If someone gets a hold of the encryption key, it is trivial to decrypt the data.

You will want to consider a key rotation plan because of this. Every set number of days you will want to decrypt all the data using the old key and re-encrypt it with the new key. Yes, it is time consuming, but that is the tradeoff for using encryption and staying secure.

Encryption Made Easy

PHP can do encryption as well as hashing. I personally prefer using the Mcrypt library that is available on most Linux systems, but PHP can also use OpenSSL. Encryption is a fairly heavy weight process and requires a bit of setup to get going. I’ve created a simple class for setting up and using Mcrypt at http://code.google.com/p/tws-code/ .

$key = 'SuperSecretKey';
$twsMcrypt = new Tws_Mcrypt($key);
$encryptedString = $twsMcrypt->encrypt('This is a secret message');
$unencryptedString = $twsMcrypt->decrypt($encryptedString');

Tws_Mcrypt can also take a second constructor argument to specify the type of encryption to use, otherwise it defaults to RIJNDAEL_128. It also encodes the encrypted string in Base64 to make storage easier, so if you encrypt something using Tws_Mcrypt and try to decrypt it straight, make sure you Base64 decode it first.

“Unless you are a cryptanalyst, don’t do your own crypto”

One final comment on hashing and encryption. The algorithms that are used in either case are tried and tested algorithms that are hard to crack and, in some cases, extremely sophisticated. People much, much smarter have taken the time to develop them and you, me, nor anyone that is not a cryptanalyst will do better.

Don’t think that the nifty encryption algorithm you came up with last night while you were in the zone will be any better than the ones that ship in PHP or in libraries like Mcrypt or OpenSSL. It won’t be and will only put you at risk.

So which to use?

Use Hashing when:

  • You don’t care what the actual data is
  • You just need to do verification
  • You need something extremely light-weight

Use Encryption when:

  • Data needs to be secured, but pulled back up later

Keep all this in mind next time you are talking to someone about how your application works. Don’t tell them you are encrypting passwords in the database when what you are really doing is hashing them. Don’t think that using straight MD5 is a viable means for data security.

Hopefully this will help you choose which type of security based on what you are doing as well.

BiffCMS is now Kiroku!

June 4th, 2009

A few weeks ago BiffCMS changed names to Kiroku (which means Document in Japanese). This was mostly motivated by the fact that BiffCMS, in its current incarnation, bears little resemblence to what BiffCMS was even a year ago. A great deal of work has been done under the hood as it was converted over to Zend Framework and the UI is undergoing a complete overhaul that removes the old table-like layout of most of the pages.

jQuery is being employed in the Adminstration area to make it function much easier and cleaner than ever before:

The Plugin model has been revamped from the old BiffCMS months ago as well as the addition of Sections. A nicer WYSIWYG editor is in place over the old BiffCMS. All in all, Kiroku has grown a lot since BiffCMS was started by me years ago as a quick way to design and maintain web pages.

There are a few other projects that I’m working on and, as they grow, are moving away from the Biff name. The BiffAPI most of my code was built on in the PHP4 days is no longer worthwhile since I’ve moved on to the Zend Framework, so its appropriate that these project names change.

If you’re interested in working with Kiroku, just drop me a line through e-mail or hit me up on Twitter.

Major Upgrade to BiffCMS

February 7th, 2009

One of the nice things about switching BiffCMS to using the Zend Framework from the now extremely old and not-PHP5 BiffAPI that it used to run on is flexibility with power. Due to Zend Framework’s nature of allowing the programmer to do what they want instead of forcing them into a design paradigm, BiffCMS is now more modular.

What was overhauled?

The basic folder structure and some plumbing were overhauled in the last few days. The introduction of a ‘plugins’, a ’sections’, and a better theme system were all moved around and coded to allow for easier development. If you were using the Zend Framework version of BiffCMS all you will need to do is move some files around, but this is not a simple ‘overwrite files’ kind of upgrade.

Plugins

The heart of BiffCMS has always been Modules, which dictate what pages you can add to your site through BiffCMS. Since this has a much different term to Zend Framework developers these are now renamed Plugins and are much more easy to develop. Using the new ‘plugins’ folder, each plugin is now fully self contained inside its own folder. Need to install a new plugin? Drop it into the ‘plugins’ folder and go from there (still needs to be manually entered into the DB, but work on installation is coming along).  I’ve also added an Externallink plugin that allows pages that direct to outside URLs.

Sections

BiffCMS is built using Zend Framework’s MVC components and therefore has full support for Zend Framework modules (collections of Controllers and View Scripts). BiffCMS calls these Sections and they can be used when a simple page Plugin is not enough. Drop them into the ’sections’ folder and add them to the index.php page just like a regular Zend Framework module. Work is also progressing on allowing Sections to be enabled and disabled via the admin GUI.

Theming

The last thing a web developer wants is for his website to look like everyone elses. BiffCMS now has support for packaged themes, much like the BiffAPI version used to. Just create a folder containing at least a ‘layout.phtml’ and an ‘admin-layout.phtml’, drop it into the ‘www/themes/’ folder, and you can select it from the System Config portion of the Admin. These are just regular Zend Framework layout files so existing layouts can be easily ported.

Administration

Along with the above changes, BiffCMS now supports adding and editing users (deleting coming soon!), the beginnings of a System Config section to allow low level config changes like Themes, and a better Plugin management system.

If you want to check it out, head over to http://code.google.com/p/biffcms and check out a copy of the SVN. Within a few weeks there will be a formal release as some loose ends are tied up.

Tankersley Web Solutions running BiffCMS

January 18th, 2009

I’ve been meaning to do this for a while since BiffCMS has stablized to the point where I feel comfortable running it in production since switching it to Zend Framework from the old BiffAPI that it had been running. This is good since I just finished setting up and working on the website for Dragonlance: What If?, a new MUSH that will be going into beta this week.

Want to know how easy it is to get up and running with BiffCMS? I installed the app, ported the layout, and added a few pages in just about four hours total. Less than half of a day and I’ve got a site that I’m happy with and will have to do less to maintain than the straight Zend Framework-based site that was there before.

Want to try out BiffCMS? Head on over to the Google Code site and check out the latest working copy. I’ll be bundling a full release here within a week or so.

Biff Project Manager Launched

November 19th, 2008

Another project of mine has been launched – Biff Project Manager. Biff Project Manager is a project management system that I am building to replace my by-hand method of project management. The first SVN commit has been made and you can check out a fresh copy from Google Code.

What does it do?

Right now not a whole lot. You can add projects and tasks to those projects. Thanks to the beauty of jQuery and AJAX you can edit the tasks right there in the table without having to go to any special pages so updating your tasks is as simple as using a spreadsheet. It is great for simply noting down what you need to do but in the coming weeks it will be expanded to do much more. Time spent on each task is recorded as well as who completed the task.

Installation

Installation is fairly easy. Check out a copy from SVN and put it up on your web host. You will need to edit the ‘public/.htaccess’ file’s rewrite rule to the path that you unpacked it to. Once that is done run the SQL setup script in ‘data/db/base.sql’ to set up a basic database. That’s all there is!