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  
 
Top 10
Review : Sigma 24mm f/1.4 DG HSM Art
Review : Canon EF11-24mm f/4L USM
Review : Creative Sound Blaster Roar 2
Review : Philips Fidelio M2L
Review : Alienware 17 - Dell's Alienware laptops
Review Smartwatch : Wellograph
Review : Xiaomi Redmi 2
Extending LINQ to Objects : Writing a Single Element Operator (part 2) - Building the RandomElement Operator
Extending LINQ to Objects : Writing a Single Element Operator (part 1) - Building Our Own Last Operator
3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
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)
VIDEO TUTORIAL
- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
Popular Tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8