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  
  •  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
    Bits Of Bytes
    Microsoft Surface With Windows RT
    Showdown: lOS vs Android vs WP7 (Part 1)
    Apple iPhone 5 vs. iPhone 4S
    Top Tablet Apps – November 2012 (Part 1)
    BlackBerry Development : The Connected BlackBerry - Service Books
    Samsung Galaxy Ace 2 Reviews (Part 1)
    Programming with DirectX : Sound in DirectX - XACT3 (part 1) - XACT3 Tools
    Google vs Apple vs Microsoft (Part 2)
    Buying Guide: Memory Kit (Part 1) - Centon Advanced DDR3-2133 16GB (CMP2133PC4096.01K4)
    Top 10
    G-360 And G-550 Power Supply Devices Review (Part 4)
    G-360 And G-550 Power Supply Devices Review (Part 2)
    Canon IXUS 140 Camera - Great Color Reproduction
    Nikon Coolpix S5200 Camera - 10fps Continuous Shooting Mode
    Corsair Neutron GTX 240GB - A Fast Performing SSD
    G-360 And G-550 Power Supply Devices Review (Part 3)
    G-360 And G-550 Power Supply Devices Review (Part 1)
    OCZ Vector 256GB - One Of The Dominant Names In SSD
    Don’t Pay For Office 2013 (Part 2)
    Don’t Pay For Office 2013 (Part 1)