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:
Define a 16-bit value called Sum1 with an initial value of 1.
Define a 16-bit value called Sum2 with an initial value of 0.
Process each 8-bit message block in turn:
Add the numeric value of the message block to Sum 1, modulo 65521.
Add the value of Sum 1 to Sum 2, modulo 65521.
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