Using Hash Functions


Hash functions are an essential part of any security setup. However, hash functions are also used for nonsecurity needs too. A hash is simply a mathematical result of some type of data comparison. For example, you could use the Unicode number assigned to each character in a data file as the basis for a numeric comparison. Add all of the character number representations up and you have a unique number that identifies that file. Other files could have the same number, but that chance is infinitely small compared to the number of text files as a whole. In short, hash functions provide means of identifying the content of a file. By using the same algorithm to compare the file each time, you know whether something inside the file has changed (because the hash will change) without performing a character-by-character comparison. The output of a hash is often called a message digest because it’s a representation of the data.

Most hash functions are far more complex than the one I’ve just described, but the idea is always the same. A hash is a numeric representation of the content of the file derived by some mathematical means. The .NET Framework lumps all hashes under the HashAlgorithm class. Like the SymmetricAlgorithm and AsymmetricAlgorithm classes, you can’t use the HashAlgorithm class directly, but every class you can use derives from this one. The .NET Framework supports the following hash algorithms:

  • MD5

  • SHA-1

  • SHA-256

  • SHA-384

  • SHA-512

All of these options are called cryptographic hashes because they are commonly used for cryptographic purposes. Hashes don’t impart any significant security feature by themselves—they’re always used in combination with something else. For example, the Digital Signature Standard (DSS) relies on the160-bit number produced by the SHA-1 hash. The SHA-256 hash is similar to the SHA-1 hash but outputs a 256-bit number instead of a 160-bit number. The larger the number, the less likely that the message digest for two documents will be the same. The SHA-384 and SHA-512 hashes require 64-bit processing and can become quite slow on a 32-bit processor. However, if you’re looking for maximum cryptographic potential, the SHA-512 hash is the best to use because it outputs a 512-bit number. You can read about the merits of these various standards at http://islab.oregonstate.edu/koc/ece575/03Project/Watson/watson-project.doc.

The MD5 hash is a little less capable than any of the SHA variants, but it’s very fast. Many people use the MD5 because it’s fast and more than adequate for many needs. This hash outputs a 128-bit number, which means the message digest is also more efficient than the others discussed in this section. You can read about the merits of the MD5 hash and its predecessors at http://www.rsasecurity.com/rsalabs/faq/3-6-6.html.

The .NET Framework also supports keyed hashes that derive from the KeyedHashAlgorithm class. When using a standard cryptographic hash, anyone can verify the file because the hash function always works. A keyed hash requires a key in addition to the file. Only someone who knows the key can verify the authenticity of the file because both inputs are required. This makes keyed hashes both more secure and slower than standard cryptographic hashes. The .NET Framework supports the following keyed hashes.

  • Hash-based Message Authentication Code Secure Hashing Algorithm 1 (HMACSHA1)

  • Message Authentication Code Triple Data Encryption Standard (MACTripleDES)

Hashes have been used in a number of examples in the book. For example, the password example in Listing 7.6 requires use of a hash. Hashes figure so prominently in security that you’ll see other examples as the book progresses.




.Net Development Security Solutions
.NET Development Security Solutions
ISBN: 0782142664
EAN: 2147483647
Year: 2003
Pages: 168

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net