SECURITY

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

1/8/2011 3:31:34 PM
1. The Alder32 Algorithm Explained

The Adler32 algorithm generates a 32-bit hash code and is included as part of GZIP-compressed files to guard against inadvertent modification (for example, errors introduced in copying a compressed file over a network connection).

Adler32 is a simple algorithm, which operates on 8-bit blocks of message data to create hash codes, as follows:

  1. Define a 16-bit value called Sum1 with an initial value of 1.

  2. Define a 16-bit value called Sum2 with an initial value of 0.

  3. Process each 8-bit message block in turn:

  4. Add the numeric value of the message block to Sum 1, modulo 65521.

  5. Add the value of Sum 1 to Sum 2, modulo 65521.

  6. Concatenate Sum 2 and Sum 1 together to create a 32-bit hash code.

The Adler32 algorithm is not suitable for cryptographic uses because it produces hash codes that are susceptible to attack. Adler32 produces short hash codes that easily succumb to birthday attacks and, more importantly, it is easy to generate a message that results in a specific Adler32 hash code.


2. Defining the Abstract Class

The first step is to create an abstract class representing the Adler32 algorithm. The abstract class extends the System.Security.Cryptography.HashAlgorithm class but does not need to implement any members. Here is the definition of the abstract Adler32 algorithm class:

# C#
using System.Security.Cryptography;

public abstract class Adler32 : HashAlgorithm {

}

# Visual Basic .NET

Imports System.Security.Cryptography

Public MustInherit Class Adler32
Inherits HashAlgorithm

End Class

We have created the abstract class to conform to the .NET class model, allowing you to add other Adler32 implementations in the future. You can omit the abstract class, but we feel that the extra flexibility of this model is well worth the effort.

3. Defining the Implementation Class

The abstract HashAlgorithm class contains a lot of functionality that makes writing a new algorithm a simple process. For example, the details of the ComputeHash method are hidden from the implementer, and only a small number of methods need to be written in order to create a fully functional implementation.

3.1. Creating the implementation class

We have named our implementation class Adler32Managed, because it is written using a managed .NET language. The following listing shows the initial version of the implementation class extending the abstract Adler32 class. We have defined the two 16-bit values as o_sum_1 and o_sum_2. For Visual Basic .NET, we have used 32-bit integers to represent the 16-bit sums, because Visual Basic .NET does not support unsigned data types:

# C#

using System;
using System.Security.Cryptography;

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

}

# 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

End Class

3.2. Implementing the Initialize method

You should use the Initialize method to set the initial state of any algorithm; an algorithm will produce unexpected hash code values unless correctly reset between invocations. For Adler32, we use this method to set the initial sum values. We have also added a class constructor that calls the Initialize method to ensure that the values are correctly set before the class is first used:

# 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 void Initialize( ) {
// reset the sum values
o_sum_1 = 1;
o_sum_2 = 0;

}

# 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 Sub Initialize( )
' reset the sum values
o_sum_1 = 1
o_sum_2 = 0
End Sub

End Class

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