Hash codes generation by Anton


What do we know about Hash codes? They based on hash functions which well-defined procedure or mathematical function that converts a large, possibly variable-sized amount of data into a small datum, usually a single integer that may serve as an index to an array (cf. associative array). The values returned by a hash function are called hash values, hash codes, hash sums, checksums or simply hashes.

Hash functions are mostly used to speed up table lookup or data comparison tasks—such as finding items in a database, detecting duplicated or similar records in a large file, finding similar stretches in DNA sequences, and so on.

In Silverlight-based applications, you can use hash codes to help ensure data integrity.

A hash value is a numeric value of a fixed length that uniquely identifies data. Hash values are created with a hashing algorithm derived from the System.Security.Cryptography.HashAlgorithm class. A hashing algorithm is a well-defined mathematical procedure that represents a (potentially) large amount of data as a much smaller integer.

So we must include namespace System.Security.Cryptography to our project and use HashAlgorithm class. You can get full information about this class on MSDN.

First, we need to generate hash code:

// Initialize the keyed hash object.
HMACSHA256 myhmacsha256 = new HMACSHA256(key);
IsolatedStorageFileStream inStream = isoStore.OpenFile(sourceFilePath, FileMode.Open);
inStream.Position = 0;
// Compute the hash of the input file.
byte[] hashValue = myhmacsha256.ComputeHash(inStream);
// Reset inStream to the beginning of the file.
inStream.Position = 0;
// Write the computed hash value to the output file.
outStream.Write(hashValue, 0, hashValue.Length);
// Copy the contents of the sourceFile to the destFile.
int bytesRead;
// read 1K at a time
byte[] buffer = new byte[1024];
do
{
// Read from the wrapping CryptoStream.
bytesRead = inStream.Read(buffer, 0, 1024);
outStream.Write(buffer, 0, bytesRead);
} while (bytesRead > 0);
myhmacsha256.Clear();

We’ve used SHA256 hash-function. Full list of functions available here. OutStream is using defined like-this: IsolatedStorageFileStream outStream = isoStore.OpenFile(signedFilePath, FileMode.Open);

To use this code we need to get a key:

// Create a random key using a random number generator. This would be the
//  secret key shared by sender and receiver.
byte[] key = new Byte[64];
Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes("Current pass", Encoding.UTF8.GetBytes("Some text"));
key = deriveBytes.GetBytes(64);

We are using Rfc2898DeriveBytes class, which implements password-based key derivation functionality, PBKDF2, by using a pseudo-random number generator based on HMACSHA1.

More info available here.

Do decrypt, the same procedures used: 1) Generate the key (the same code)
2) Decode (something new)

// Initialize the keyed hash object. 
HMACSHA256 hmacsha256 = new HMACSHA256(key);
// Create an array to hold the keyed hash value read from the file.
byte[] storedHash = new byte[hmacsha256.HashSize / 8];
// Create a FileStream for the source file
// Read in the storedHash.
inStream.Read(storedHash, 0, storedHash.Length);
// Compute the hash of the remaining contents of the file.
// The stream is properly positioned at the beginning of the content, 
// immediately after the stored hash value.
byte[] computedHash = hmacsha256.ComputeHash(inStream);
// compare the computed hash with the stored value
for (int i = 0; i < storedHash.Length; i++)
{
if (computedHash[i] != storedHash[i])
{
MessageBox.Show("Hash values differ! Either wrong password or file has changed.");
return false;
}
}

To get closely, you can look at this sample. It’ll sure help you a lot.

It full .NET library (not for Silverlight) there are SHA512 and TripleDES algorithms for encryption which may be useful for non-browser using.