Corrected CRC algorithms

This commit is contained in:
2015-02-02 18:49:29 +00:00
parent 47eb235e1a
commit a1520d6b41
3 changed files with 20 additions and 11 deletions

View File

@@ -30,8 +30,8 @@ namespace SharpHash.Checksums
/// </summary> /// </summary>
public class CRC16Context public class CRC16Context
{ {
const UInt16 crc16Poly = 0x8408; const UInt16 crc16Poly = 0xA001;
const UInt16 crc16Seed = 0xFFFF; const UInt16 crc16Seed = 0x0000;
UInt16[] table; UInt16[] table;
UInt16 hashInt; UInt16 hashInt;
@@ -64,7 +64,7 @@ namespace SharpHash.Checksums
public void Update(byte[] data, uint len) public void Update(byte[] data, uint len)
{ {
for (int i = 0; i < len; i++) for (int i = 0; i < len; i++)
hashInt = (ushort)((hashInt >> 8) ^ table[data[i] ^ hashInt & 0xff]); hashInt = (ushort)((hashInt >> 8) ^ table[data[i] ^ (hashInt & 0xFF)]);
} }
/// <summary> /// <summary>
@@ -82,6 +82,7 @@ namespace SharpHash.Checksums
public byte[] Final() public byte[] Final()
{ {
hashInt ^= crc16Seed; hashInt ^= crc16Seed;
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
return BigEndianBitConverter.GetBytes(hashInt); return BigEndianBitConverter.GetBytes(hashInt);
} }
@@ -93,6 +94,7 @@ namespace SharpHash.Checksums
hashInt ^= crc16Seed; hashInt ^= crc16Seed;
StringBuilder crc16Output = new StringBuilder(); StringBuilder crc16Output = new StringBuilder();
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
for (int i = 0; i < BigEndianBitConverter.GetBytes(hashInt).Length; i++) for (int i = 0; i < BigEndianBitConverter.GetBytes(hashInt).Length; i++)
{ {
crc16Output.Append(BigEndianBitConverter.GetBytes(hashInt)[i].ToString("x2")); crc16Output.Append(BigEndianBitConverter.GetBytes(hashInt)[i].ToString("x2"));
@@ -140,7 +142,8 @@ namespace SharpHash.Checksums
for (int i = 0; i < fileStream.Length; i++) for (int i = 0; i < fileStream.Length; i++)
localhashInt = (ushort)((localhashInt >> 8) ^ localTable[fileStream.ReadByte() ^ localhashInt & 0xff]); localhashInt = (ushort)((localhashInt >> 8) ^ localTable[fileStream.ReadByte() ^ localhashInt & 0xff]);
hash = BitConverter.GetBytes(localhashInt); BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
hash = BigEndianBitConverter.GetBytes(localhashInt);
StringBuilder crc16Output = new StringBuilder(); StringBuilder crc16Output = new StringBuilder();
@@ -193,7 +196,8 @@ namespace SharpHash.Checksums
for (int i = 0; i < len; i++) for (int i = 0; i < len; i++)
localhashInt = (ushort)((localhashInt >> 8) ^ localTable[data[i] ^ localhashInt & 0xff]); localhashInt = (ushort)((localhashInt >> 8) ^ localTable[data[i] ^ localhashInt & 0xff]);
hash = BitConverter.GetBytes(localhashInt); BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
hash = BigEndianBitConverter.GetBytes(localhashInt);
StringBuilder crc16Output = new StringBuilder(); StringBuilder crc16Output = new StringBuilder();

View File

@@ -30,12 +30,8 @@ namespace SharpHash.Checksums
/// </summary> /// </summary>
public class CRC32Context public class CRC32Context
{ {
//const UInt32 crc32Poly = 0xEDB88320; const UInt32 crc32Poly = 0xEDB88320;
//const UInt32 crc32Seed = 0xFFFFFFFF; const UInt32 crc32Seed = 0xFFFFFFFF;
const UInt32 crc32Poly = 0xD8018001;
const UInt32 crc32Seed = 0x00000000;
UInt32[] table; UInt32[] table;
UInt32 hashInt; UInt32 hashInt;
@@ -86,6 +82,7 @@ namespace SharpHash.Checksums
public byte[] Final() public byte[] Final()
{ {
hashInt ^= crc32Seed; hashInt ^= crc32Seed;
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
return BigEndianBitConverter.GetBytes(hashInt); return BigEndianBitConverter.GetBytes(hashInt);
} }
@@ -97,6 +94,7 @@ namespace SharpHash.Checksums
hashInt ^= crc32Seed; hashInt ^= crc32Seed;
StringBuilder crc32Output = new StringBuilder(); StringBuilder crc32Output = new StringBuilder();
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
for (int i = 0; i < BigEndianBitConverter.GetBytes(hashInt).Length; i++) for (int i = 0; i < BigEndianBitConverter.GetBytes(hashInt).Length; i++)
{ {
crc32Output.Append(BigEndianBitConverter.GetBytes(hashInt)[i].ToString("x2")); crc32Output.Append(BigEndianBitConverter.GetBytes(hashInt)[i].ToString("x2"));
@@ -144,6 +142,7 @@ namespace SharpHash.Checksums
for (int i = 0; i < fileStream.Length; i++) for (int i = 0; i < fileStream.Length; i++)
localhashInt = (localhashInt >> 8) ^ localTable[fileStream.ReadByte() ^ localhashInt & 0xff]; localhashInt = (localhashInt >> 8) ^ localTable[fileStream.ReadByte() ^ localhashInt & 0xff];
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
hash = BitConverter.GetBytes(localhashInt); hash = BitConverter.GetBytes(localhashInt);
StringBuilder crc32Output = new StringBuilder(); StringBuilder crc32Output = new StringBuilder();
@@ -197,6 +196,7 @@ namespace SharpHash.Checksums
for (int i = 0; i < len; i++) for (int i = 0; i < len; i++)
localhashInt = (localhashInt >> 8) ^ localTable[data[i] ^ localhashInt & 0xff]; localhashInt = (localhashInt >> 8) ^ localTable[data[i] ^ localhashInt & 0xff];
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
hash = BitConverter.GetBytes(localhashInt); hash = BitConverter.GetBytes(localhashInt);
StringBuilder crc32Output = new StringBuilder(); StringBuilder crc32Output = new StringBuilder();

View File

@@ -65,6 +65,7 @@ namespace SharpHash.Checksums
{ {
for (int i = 0; i < len; i++) for (int i = 0; i < len; i++)
hashInt = (hashInt >> 8) ^ table[data[i] ^ hashInt & 0xff]; hashInt = (hashInt >> 8) ^ table[data[i] ^ hashInt & 0xff];
} }
/// <summary> /// <summary>
@@ -82,6 +83,7 @@ namespace SharpHash.Checksums
public byte[] Final() public byte[] Final()
{ {
hashInt ^= crc64Seed; hashInt ^= crc64Seed;
BigEndianBitConverter.IsLittleEndian = BigEndianBitConverter.IsLittleEndian;
return BigEndianBitConverter.GetBytes(hashInt); return BigEndianBitConverter.GetBytes(hashInt);
} }
@@ -93,6 +95,7 @@ namespace SharpHash.Checksums
hashInt ^= crc64Seed; hashInt ^= crc64Seed;
StringBuilder crc64Output = new StringBuilder(); StringBuilder crc64Output = new StringBuilder();
BigEndianBitConverter.IsLittleEndian = BigEndianBitConverter.IsLittleEndian;
for (int i = 0; i < BigEndianBitConverter.GetBytes(hashInt).Length; i++) for (int i = 0; i < BigEndianBitConverter.GetBytes(hashInt).Length; i++)
{ {
crc64Output.Append(BigEndianBitConverter.GetBytes(hashInt)[i].ToString("x2")); crc64Output.Append(BigEndianBitConverter.GetBytes(hashInt)[i].ToString("x2"));
@@ -140,6 +143,7 @@ namespace SharpHash.Checksums
for (int i = 0; i < fileStream.Length; i++) for (int i = 0; i < fileStream.Length; i++)
localhashInt = (localhashInt >> 8) ^ localTable[(ulong)fileStream.ReadByte() ^ localhashInt & (ulong)0xff]; localhashInt = (localhashInt >> 8) ^ localTable[(ulong)fileStream.ReadByte() ^ localhashInt & (ulong)0xff];
BigEndianBitConverter.IsLittleEndian = BigEndianBitConverter.IsLittleEndian;
hash = BitConverter.GetBytes(localhashInt); hash = BitConverter.GetBytes(localhashInt);
StringBuilder crc64Output = new StringBuilder(); StringBuilder crc64Output = new StringBuilder();
@@ -193,6 +197,7 @@ namespace SharpHash.Checksums
for (int i = 0; i < len; i++) for (int i = 0; i < len; i++)
localhashInt = (localhashInt >> 8) ^ localTable[data[i] ^ localhashInt & 0xff]; localhashInt = (localhashInt >> 8) ^ localTable[data[i] ^ localhashInt & 0xff];
BigEndianBitConverter.IsLittleEndian = BigEndianBitConverter.IsLittleEndian;
hash = BitConverter.GetBytes(localhashInt); hash = BitConverter.GetBytes(localhashInt);
StringBuilder crc64Output = new StringBuilder(); StringBuilder crc64Output = new StringBuilder();