programming4us
programming4us
SECURITY

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

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
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
Free Mobile And Desktop Apps For Accessing Restricted Websites
MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
TOYOTA CAMRY 2; 2.5 : Camry now more comely
KIA SORENTO 2.2CRDi : Fuel-sipping slugger
How To Setup, Password Protect & Encrypt Wireless Internet Connection
Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
Backup & Restore Game Progress From Any Game With SaveGameProgress
Generate A Facebook Timeline Cover Using A Free App
New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
- Messages forwarded by Outlook rule go nowhere
- Create and Deploy Windows 7 Image
- How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
- Creating and using an unencrypted cookie in ASP.NET
- Directories
- Poor Performance on Sharepoint 2010 Server
- SBS 2008 ~ The e-mail alias already exists...
- Public to Private IP - DNS Changes
- Send Email from Winform application
- How to create a .mdb file from ms sql server database.......
programming4us programming4us
programming4us
 
 
programming4us