My favorites
|
Sign in
coderjournal
Nick Berardi's Coder Journal
Project Home
Source
Checkout
|
Browse
|
Changes
|
r36
Source path:
svn
/
trunk
/
ManagedFusion
/
Source
/
Crc32.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Security.Cryptography;
namespace ManagedFusion
{
/// <summary>
///
/// </summary>
public class Crc32 : HashAlgorithm
{
public const uint DefaultPolynomial = 0xedb88320;
public const uint DefaultSeed = 0xffffffff;
private uint hash;
private uint seed;
private uint[] table;
/// <summary>
/// Initializes a new instance of the <see cref="Crc32"/> class.
/// </summary>
public Crc32()
{
table = InitializeTable(DefaultPolynomial);
seed = DefaultSeed;
Initialize();
}
/// <summary>
/// Initializes a new instance of the <see cref="Crc32"/> class.
/// </summary>
/// <param name="polynomial">The polynomial.</param>
/// <param name="seed">The seed.</param>
public Crc32(uint polynomial, uint seed)
{
table = InitializeTable(polynomial);
this.seed = seed;
Initialize();
}
/// <summary>
/// Initializes an implementation of the <see cref="T:System.Security.Cryptography.HashAlgorithm"/> class.
/// </summary>
public override void Initialize()
{
hash = seed;
}
/// <summary>
/// Hashes the core.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <param name="start">The start.</param>
/// <param name="length">The length.</param>
protected override void HashCore(byte[] buffer, int start, int length)
{
hash = CalculateHash(table, hash, buffer, start, length);
}
/// <summary>
/// When overridden in a derived class, finalizes the hash computation after the last data is processed by the cryptographic stream object.
/// </summary>
/// <returns>The computed hash code.</returns>
protected override byte[] HashFinal()
{
byte[] hashBuffer = UInt32ToBigEndianBytes(hash);
this.HashValue = hashBuffer;
return hashBuffer;
}
/// <summary>
/// Gets the size, in bits, of the computed hash code.
/// </summary>
/// <value></value>
/// <returns>The size, in bits, of the computed hash code.</returns>
public override int HashSize
{
get { return 32; }
}
/// <summary>
/// Computes the specified polynomial.
/// </summary>
/// <param name="polynomial">The polynomial.</param>
/// <param name="seed">The seed.</param>
/// <param name="buffer">The buffer.</param>
/// <returns></returns>
public static uint Compute(uint polynomial, uint seed, byte[] buffer)
{
return CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
}
/// <summary>
/// Initializes the table.
/// </summary>
/// <param name="polynomial">The polynomial.</param>
/// <returns></returns>
private static UInt32[] InitializeTable(uint polynomial)
{
uint[] createTable = new uint[256];
for (int i = 0; i < 256; i++)
{
uint entry = (uint)i;
for (int j = 0; j < 8; j++)
if ((entry & 1) == 1)
entry = (entry >> 1) ^ polynomial;
else
entry = entry >> 1;
createTable[i] = entry;
}
return createTable;
}
/// <summary>
/// Calculates the hash.
/// </summary>
/// <param name="table">The table.</param>
/// <param name="seed">The seed.</param>
/// <param name="buffer">The buffer.</param>
/// <param name="start">The start.</param>
/// <param name="size">The size.</param>
/// <returns></returns>
private static uint CalculateHash(uint[] table, uint seed, byte[] buffer, int start, int size)
{
uint crc = seed;
for (int i = start; i < size; i++)
unchecked
{
crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff];
}
return ~crc;
}
/// <summary>
/// Us the int32 to big endian bytes.
/// </summary>
/// <param name="x">The x.</param>
/// <returns></returns>
private byte[] UInt32ToBigEndianBytes(uint x)
{
return new byte[] {
(byte)((x >> 24) & 0xff),
(byte)((x >> 16) & 0xff),
(byte)((x >> 8) & 0xff),
(byte)(x & 0xff)
};
}
}
}
Show details
Hide details
Change log
r30
by nberardi on Aug 28, 2009
Diff
refresh of ManagedFusion
Go to:
/trunk/ManagedFusion
/trunk/ManagedFusion.Build
/trunk/ManagedFusion.Build/Source
...d/Source/BuildNumberGenerator.cs
...ource/ManagedFusion.Build.csproj
.../ManagedFusion.Build.csproj.user
...pareForContentDeliveryNetwork.cs
...edFusion.Build/Source/Properties
...ource/Properties/AssemblyInfo.cs
...uild/Source/SourceAnalysis.Cache
...sion.Build/Source/YuiCompress.cs
...d/Source/yuicompressor-2.3.5.jar
.../ManagedFusion/ManagedFusion.suo
/trunk/ManagedFusion/Source
...ManagedFusion/Source/Collections
...Source/Collections/IPagedList.cs
...n/Source/Collections/LazyList.cs
.../Source/Collections/PagedList.cs
...Source/Collections/Pagination.cs
...nk/ManagedFusion/Source/Crc32.cs
.../ManagedFusion/Source/Extensions
.../Source/Extensions/Enumerator.cs
...sion/Source/Extensions/String.cs
...on/Source/Extensions/TimeSpan.cs
...sion/Source/ManagedFusion.csproj
...Source/ManagedFusion.csproj.user
...dFusion/Source/ManagedFusion.sln
.../ManagedFusion/Source/Properties
...ource/Properties/AssemblyInfo.cs
...nagedFusion/Source/Serialization
...rce/Serialization/ISerializer.cs
.../Serialization/JsonSerializer.cs
...ableCollectionObjectAttribute.cs
...n/SerializableObjectAttribute.cs
...SerializablePropertyAttribute.cs
...ation/SerializationExtensions.cs
...urce/Serialization/Serializer.cs
...e/Serialization/XmlSerializer.cs
...sion/Source/SourceAnalysis.Cache
.../ManagedFusion/Source/Utility.cs
Older revisions
All revisions of this file
File info
Size: 3999 bytes, 148 lines
View raw file
Hosted by