SECURITY

Hashing Algorithms: Extending the .NET Framework (part 1)

1/8/2011 3:33:00 PM
3.3. Implementing the HashCore and HashFinal methods

The HashCore method receives message data into the algorithm class; the amount of data to process can vary from a single byte to very large arrays. Always ensure that you process only the region of the array indicated by the p_start_index and p_count parameters. This method may be invoked multiple times, and you must make sure that your algorithm can process data blocks incrementally, rather then assuming that all of the message data will arrive at once:

# C#

protected override void HashCore(byte[] p_array, int p_start_index, int p_count) {
// process each byte in the array
for (int i = p_start_index; i < p_count; i++) {
o_sum_1 = (ushort)((o_sum_1 + p_array[i]) % 65521);
o_sum_2 = (ushort)((o_sum_1 + o_sum_2) % 65521);
}
}

# Visual Basic .NET

Protected Overrides Sub HashCore(ByVal p_array( ) As Byte, _
ByVal p_start_index As Integer, ByVal p_count As Integer)

' process each byte in the array
Dim i As Integer
For i = p_start_index To p_count - 1 Step i + 1
o_sum_1 = (o_sum_1 + p_array(i)) Mod 65521
o_sum_2 = (o_sum_1 + o_sum_2) Mod 65521
Next
End Sub


The HashFinal method is called when all of the message data has been processed by the HashCore method and returns the computed hash code value as a byte array. This method allows you to complete any pending calculations before computing the final hash code. For Alder32, you simply need to concatenate the two sum values together to form the 32-bit hash code, and convert the result into a byte array:

# C#

protected override byte[] HashFinal( ) {
// concat the two 16 bit values to form
// one 32-bit value
uint x_concat_value = (uint)((o_sum_2 << 16) | o_sum_1);
// use the bitconverter class to render the
// 32-bit integer into an array of bytes
return BitConverter.GetBytes(x_concat_value);
}

# Visual Basic .NET

Protected Overrides Function HashFinal( ) As Byte( )
' concat the two 16 bit values to form
' one 32-bit value
Dim x_concat_value As Integer = (o_sum_2 * 65536) Or o_sum_1
' use the bitconverter class to render the
' 32-bit integer into an array of bytes
Return BitConverter.GetBytes(x_concat_value)
End Function

3.4. Completing the class

The final step is to implement the HashSize property to specify the size (in bits) of the hash code that you are generating. Example 1 is the complete listing for the Alder32Managed class:

Example 1. algorithm
# C#

using System;
using System.Security.Cryptography;

public class Adler32Managed : HashAlgorithm {
private ushort o_sum_1;
private ushort o_sum_2;

public Adler32Managed( ) {
Initialize( );
}

public override int HashSize {
get {
return 32;
}
}

public override void Initialize( ) {
// reset the sum values
o_sum_1 = 1;
o_sum_2 = 0;
}

protected override void HashCore(byte[] p_array, int p_start_index,
int p_count) {
// process each byte in the array
for (int i = p_start_index; i < p_count; i++) {
o_sum_1 = (ushort)((o_sum_1 + p_array[i]) % 65521);
o_sum_2 = (ushort)((o_sum_1 + o_sum_2) % 65521);
}
}

protected override byte[] HashFinal( ) {
// concat the two 16 bit values to form
// one 32-bit value
uint x_concat_value = (uint)((o_sum_2 << 16) | o_sum_1);
// use the bitconverter class to render the
// 32-bit integer into an array of bytes
return BitConverter.GetBytes(x_concat_value);

}

# Visual Basic .NET

Imports System
Imports System.Security.Cryptography

Public Class Adler32Managed
Inherits Adler32

Private o_sum_1 As Integer
Private o_sum_2 As Integer

Public Sub New( )
Initialize( )
End Sub

Public Overrides ReadOnly Property HashSize( ) As Integer
Get
Return 32
End Get
End Property

Public Overrides Sub Initialize( )
' reset the sum values
o_sum_1 = 1
o_sum_2 = 0
End Sub

Protected Overrides Sub HashCore(ByVal p_array( ) As Byte, _
ByVal p_start_index As Integer, ByVal p_count As Integer)

' process each byte in the array
Dim i As Integer
For i = p_start_index To p_count - 1 Step i + 1
o_sum_1 = (o_sum_1 + p_array(i)) Mod 65521
o_sum_2 = (o_sum_1 + o_sum_2) Mod 65521
Next
End Sub

Protected Overrides Function HashFinal( ) As Byte( )
' concat the two 16 bit values to form
' one 32-bit value
Dim x_concat_value As Integer = (o_sum_2 * 65536) Or o_sum_1
' use the bitconverter class to render the
' 32-bit integer into an array of bytes
Return BitConverter.GetBytes(x_concat_value)
End Function

End Class


3.5. Testing the implementation

Now that you have created the implementation class, you can use the Adler32 algorithm in exactly the same way as the other classes covered in this chapter. The following listing details how to create an Adler32 hash code for the message "Programming .NET Security":

# C#

// define the message string that we will create a hash code for
string x_message_string = "Programming .NET Security";
// convert the message string into bytes
byte[] x_message_bytes = Encoding.Default.GetBytes(x_message_string);

HashAlgorithm x_hash_alg = new Adler32Managed( );
byte[] x_hash_code = x_hash_alg.ComputeHash(x_message_bytes);

// print out the hash code
foreach (byte x_byte in x_hash_code) {
Console.Write("{0:X2} ", x_byte);
}

# Visual Basic .NET

' define the message string that we will create a hash code for
Dim x_message_string As String = "Programming .NET Security"
' convert the message string into bytes
Dim x_message_bytes( ) As Byte = Encoding.Default.GetBytes(x_message_string)
Other  
  •  Programming Keyed Hashing Algorithms
  •  Programming .NET Security : Keyed Hashing Algorithms Explained
  •  Programming Hashing Algorithms (part 5) - Validating Hash Codes
  •  Programming Hashing Algorithms (part 4) - Hashing Streamed Data
  •  Programming Hashing Algorithms (part 3) - Hashing Data from Memory
  •  Programming Hashing Algorithms (part 2) - Instantiating the Algorithm
  •  Programming Hashing Algorithms (part 1) - The HashAlgorithm Class
  •  Programming .NET Security : Hashing Algorithms Explained
  •  Programming .NET Security : Cryptography Explained (part 2)
  •  Programming .NET Security : Cryptography Explained (part 1) - Confidentiality
  •  .NET security : Administering Isolated Storage
  •  .NET security : Programming Isolated Storage
  •  .NET security : Isolated Storage Explained
  •  Programming Role-Based Security
  •  Role-Based Security Explained
  •  Infrastructure Security: The Application Level
  •  Infrastructure Security: The Host Level
  •  Infrastructure Security: The Network Level
  •  .NET Components : Configuring Permissions
  •  The .NET Security Architecture
  •  
    Most View
    Astrophotography Alternative : Canon EOS 60Da, Lumix GF5
    Huge Screen Supertest (Part 8) - Philips Brilliance 248C3LHSB & Samsung 5 Series T27A550
    CMS Revolution (Part 2)
    Working with Access and Connectivity Policies in Vista
    Four Of The Best Stereo Systems (Part 1)
    The 50 Best Headphones You Can Buy (Part 3)
    Netgear DGND3700 - The Ultimate Home Gateway
    The Art of SEO : How Links Influence Search Engine Rankings (part 1) - The Original PageRank Algorithm
    Restart Manager in Windows Vista
    Corel Painter X : Working with Layers - The Chair
    Top 10
    ADO.NET Programming : Microsoft SQL Server (part 4) - Working with Typed Data Sets
    ADO.NET Programming : Microsoft SQL Server (part 3) - Using Stored Procedures with DataSet Objects
    ADO.NET Programming : Microsoft SQL Server (part 2) - Using SQL Server Stored Procedures
    ADO.NET Programming : Microsoft SQL Server (part 1) - Connecting to SQL Server, Creating Command Objects
    Windows Phone 8 In-Depth Review (Part 6)
    Windows Phone 8 In-Depth Review (Part 5)
    Windows Phone 8 In-Depth Review (Part 4)
    Windows Phone 8 In-Depth Review (Part 3)
    Windows Phone 8 In-Depth Review (Part 2)
    Windows Phone 8 In-Depth Review (Part 1)