mirror of
https://github.com/TalAloni/SMBLibrary.git
synced 2025-07-19 15:45:54 +02:00
SMBServer v1.0.5
This commit is contained in:
parent
b75820452d
commit
bd1006cb81
400 changed files with 28062 additions and 0 deletions
120
Utilities/Cryptography/CRC32.cs
Normal file
120
Utilities/Cryptography/CRC32.cs
Normal file
|
@ -0,0 +1,120 @@
|
|||
using System;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace Utilities
|
||||
{
|
||||
// Author: Damien Guard (damieng@gmail.com)
|
||||
// http://damieng.com/blog/2006/08/08/calculating_crc32_in_c_and_net
|
||||
public class CRC32 : HashAlgorithm
|
||||
{
|
||||
public const UInt32 DefaultPolynomial = 0xedb88320;
|
||||
public const UInt32 DefaultSeed = 0xffffffff;
|
||||
|
||||
private UInt32 hash;
|
||||
private UInt32 seed;
|
||||
private UInt32[] table;
|
||||
private static UInt32[] defaultTable;
|
||||
|
||||
public CRC32()
|
||||
{
|
||||
table = InitializeTable(DefaultPolynomial);
|
||||
seed = DefaultSeed;
|
||||
Initialize();
|
||||
}
|
||||
|
||||
public CRC32(UInt32 polynomial, UInt32 seed)
|
||||
{
|
||||
table = InitializeTable(polynomial);
|
||||
this.seed = seed;
|
||||
Initialize();
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
hash = seed;
|
||||
}
|
||||
|
||||
protected override void HashCore(byte[] buffer, int start, int length)
|
||||
{
|
||||
hash = CalculateHash(table, hash, buffer, start, length);
|
||||
}
|
||||
|
||||
protected override byte[] HashFinal()
|
||||
{
|
||||
byte[] hashBuffer = UInt32ToBigEndianBytes(~hash);
|
||||
this.HashValue = hashBuffer;
|
||||
return hashBuffer;
|
||||
}
|
||||
|
||||
public override int HashSize
|
||||
{
|
||||
get { return 32; }
|
||||
}
|
||||
|
||||
public static UInt32 Compute(byte[] buffer)
|
||||
{
|
||||
return ~CalculateHash(InitializeTable(DefaultPolynomial), DefaultSeed, buffer, 0, buffer.Length);
|
||||
}
|
||||
|
||||
public static UInt32 Compute(UInt32 seed, byte[] buffer)
|
||||
{
|
||||
return ~CalculateHash(InitializeTable(DefaultPolynomial), seed, buffer, 0, buffer.Length);
|
||||
}
|
||||
|
||||
public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer)
|
||||
{
|
||||
return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
|
||||
}
|
||||
|
||||
private static UInt32[] InitializeTable(UInt32 polynomial)
|
||||
{
|
||||
if (polynomial == DefaultPolynomial && defaultTable != null)
|
||||
return defaultTable;
|
||||
|
||||
UInt32[] createTable = new UInt32[256];
|
||||
for (int i = 0; i < 256; i++)
|
||||
{
|
||||
UInt32 entry = (UInt32)i;
|
||||
for (int j = 0; j < 8; j++)
|
||||
if ((entry & 1) == 1)
|
||||
entry = (entry >> 1) ^ polynomial;
|
||||
else
|
||||
entry = entry >> 1;
|
||||
createTable[i] = entry;
|
||||
}
|
||||
|
||||
if (polynomial == DefaultPolynomial)
|
||||
defaultTable = createTable;
|
||||
|
||||
return createTable;
|
||||
}
|
||||
|
||||
private static UInt32 CalculateHash(UInt32[] table, UInt32 seed, byte[] buffer, int start, int size)
|
||||
{
|
||||
UInt32 crc = seed;
|
||||
for (int i = start; i < size; i++)
|
||||
unchecked
|
||||
{
|
||||
crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff];
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
private byte[] UInt32ToBigEndianBytes(UInt32 x)
|
||||
{
|
||||
return new byte[] {
|
||||
(byte)((x >> 24) & 0xff),
|
||||
(byte)((x >> 16) & 0xff),
|
||||
(byte)((x >> 8) & 0xff),
|
||||
(byte)(x & 0xff)
|
||||
};
|
||||
}
|
||||
|
||||
// Added by Tal Aloni 2013.07.11
|
||||
public static uint UPDC32(byte octet, uint crc)
|
||||
{
|
||||
uint[] table = InitializeTable(DefaultPolynomial);
|
||||
return (table[((crc) ^ ((byte)octet)) & 0xff] ^ ((crc) >> 8));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue