programming4us
programming4us
SECURITY

Programming Keyed Hashing Algorithms

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
The .NET Framework has a much more direct model for representing keyed hashing algorithms. There are no abstract classes for different algorithms, meaning that there can be only one implementation of each keyed algorithm. Figure 1 shows the class hierarchy for keyed hashing algorithms.
Figure 1. The .NET Framework class hierarchy for keyed hashing algorithms.

1. The KeyedHashAlgorithm Class

The KeyedHashAlgorithm class extends HashAlgorithm, adding a new property called Key, which is used to get and set the secret key as a byte array. Aside from this addition, the KeyedHashAlgorithm class provides the same abstraction benefits as using HashAlgorithm for creating normal hash codes.

2. Instantiating the Algorithm

The process of creating instances of keyed algorithms is much the same as for normal hashing algorithms. You can create algorithms directly using the implementation class name, or indirectly using the Create method of the KeyedHashingAlgorithm class.

When using the direct approach, you can supply the secret key as an argument to the class constructor. In the following fragment, use the System.Text.Encoding class to convert a string into a byte array and to initialize the keyed hashing algorithm:

# C#

// define the key as a string
string x_key_string = "This is my secret key";
// convert the string to a byte array
byte[] x_key_bytes
= System.Text.Encoding.Default.GetBytes(x_key_string);

// create the keyed hashing algorithm, using the
// byte array as the constructor argument
KeyedHashAlgorithm x_hash_alg = new HMACSHA1(x_key_bytes);

# Visual Basic .NET

' define the key as a string
Dim x_key_string As String = " This is my secret key "
' convert the string to a byte array
Dim x_key_bytes As Byte( ) _
= System.Text.Encoding.Default.GetBytes(x_key_string)

' create the keyed hashing algorithm, using the
' byte array as the constructor argument
Dim x_hash_alg As KeyedHashAlgorithm = New HMACSHA1(x_key_bytes)

These statements create an instance of the HMAC-SHA-1 implementation class, using the key "This is my secret key." When using the Create method, the key value is set separately, using the Key property. The following fragment demonstrates how to do this:

# C#

// create the keyed hashing algorithm, using the
// byte array as the constructor argument
KeyedHashAlgorithm x_hash_alg = KeyedHashAlgorithm.Create("HMACSHA1");

// define the key as a string
string x_key_string = " This is my secret key ";
// convert the string to a byte array
byte[] x_key_bytes
= System.Text.Encoding.Default.GetBytes(x_key_string);

// set the keyed hash algorithm key
x_hash_alg.Key = x_key_bytes;

# Visual Basic .NET

' create the keyed hashing algorithm, using the
' byte array as the constructor argument
Dim x_hash_alg As KeyedHashAlgorithm = KeyedHashAlgorithm.Create("HMACSHA1")

' define the key as a string
Dim x_key_string As String = " This is my secret key "
' convert the string to a byte array
Dim x_key_bytes As Byte( ) _
= System.Text.Encoding.Default.GetBytes(x_key_string)

' set the keyed hash algorithm key
x_hash_alg.Key = x_key_bytes


The Create method creates an algorithm class based on the value of the string argument. Table 1 lists the mapping between string values and implementation classes for keyed hashing algorithms.

If you do not supply a string as an argument to the Create method, the System.Security.Cryptography.HMACSHA1 algorithm will be used as a default. If you use a string that is not one of the values listed in Table 1, then a System.NullReferenceException will be thrown.


Table 1. Mapping string values to keyed algorithm classes
String value Algorithm implementation class
HMACSHA1 HMACSHA1
System.Security.Cryptography.HMACSHA1 HMACSHA1
MACTripleDES MACTripleDES
System.Security.Cryptography.MACTripleDES MACTripleDES

3. Hashing Data and Validating Hash Codes

Because the KeyedHashAlgorithm class derives from HashAlgorithm, the processes for creating and validating keyed hash codes is the same as for normal hash codes, with the exception that the value of the key should be set either as a constructor argument or as using the Key property. For reference, Example 1 demonstrates how to create a keyed hash code from a byte array of data:

You must set the secret key value before creating the hash code; otherwise, the KeyedHashAlgorithm will create a random key.


Example 1. Creating a keyed hash code from a byte array of data
# C#

using System;
using System.Text;
using System.Security.Cryptography;

class ByteArrayHash {

static void Main(string[] args) {

// define the string that we will
// create a hash code for
String x_str = "Programming .NET Security";
// create a byte array from the string
byte[] x_message_data = Encoding.Default.GetBytes(x_str);

// define the secret key as a string
string x_key_string = "This is my secret key";
// create a byte array containing the key
byte[] x_key_bytes = Encoding.Default.GetBytes(x_key_string);

// create an instance of the HMAC-SHA-1 keyed algorithm
KeyedHashAlgorithm x_hash_alg = KeyedHashAlgorithm.Create("HMACSHA1");
Other  
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
REVIEW
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us
Celebrity Style, Fashion Trends, Beauty and Makeup Tips.
programming4us
 
 
programming4us