mirror of
https://github.com/aaru-dps/Aaru.Checksums.git
synced 2025-12-16 19:24:29 +00:00
🐛Move checksum initializers to instance constructors.
This commit is contained in:
@@ -42,12 +42,12 @@ namespace DiscImageChef.Checksums
|
|||||||
public class Adler32Context : IChecksum
|
public class Adler32Context : IChecksum
|
||||||
{
|
{
|
||||||
const ushort ADLER_MODULE = 65521;
|
const ushort ADLER_MODULE = 65521;
|
||||||
ushort sum1, sum2;
|
ushort sum1, sum2;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes the Adler-32 sums
|
/// Initializes the Adler-32 sums
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Init()
|
public Adler32Context()
|
||||||
{
|
{
|
||||||
sum1 = 1;
|
sum1 = 1;
|
||||||
sum2 = 0;
|
sum2 = 0;
|
||||||
@@ -63,7 +63,7 @@ namespace DiscImageChef.Checksums
|
|||||||
for(int i = 0; i < len; i++)
|
for(int i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
sum1 = (ushort)((sum1 + data[i]) % ADLER_MODULE);
|
sum1 = (ushort)((sum1 + data[i]) % ADLER_MODULE);
|
||||||
sum2 = (ushort)((sum2 + sum1) % ADLER_MODULE);
|
sum2 = (ushort)((sum2 + sum1) % ADLER_MODULE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,7 +81,7 @@ namespace DiscImageChef.Checksums
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public byte[] Final()
|
public byte[] Final()
|
||||||
{
|
{
|
||||||
uint finalSum = (uint)((sum2 << 16) | sum1);
|
uint finalSum = (uint)((sum2 << 16) | sum1);
|
||||||
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
||||||
return BigEndianBitConverter.GetBytes(finalSum);
|
return BigEndianBitConverter.GetBytes(finalSum);
|
||||||
}
|
}
|
||||||
@@ -91,7 +91,7 @@ namespace DiscImageChef.Checksums
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string End()
|
public string End()
|
||||||
{
|
{
|
||||||
uint finalSum = (uint)((sum2 << 16) | sum1);
|
uint finalSum = (uint)((sum2 << 16) | sum1);
|
||||||
StringBuilder adlerOutput = new StringBuilder();
|
StringBuilder adlerOutput = new StringBuilder();
|
||||||
|
|
||||||
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
||||||
@@ -119,8 +119,8 @@ namespace DiscImageChef.Checksums
|
|||||||
public static string File(string filename, out byte[] hash)
|
public static string File(string filename, out byte[] hash)
|
||||||
{
|
{
|
||||||
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
||||||
ushort localSum1, localSum2;
|
ushort localSum1, localSum2;
|
||||||
uint finalSum;
|
uint finalSum;
|
||||||
|
|
||||||
localSum1 = 1;
|
localSum1 = 1;
|
||||||
localSum2 = 0;
|
localSum2 = 0;
|
||||||
@@ -128,13 +128,13 @@ namespace DiscImageChef.Checksums
|
|||||||
for(int i = 0; i < fileStream.Length; i++)
|
for(int i = 0; i < fileStream.Length; i++)
|
||||||
{
|
{
|
||||||
localSum1 = (ushort)((localSum1 + fileStream.ReadByte()) % ADLER_MODULE);
|
localSum1 = (ushort)((localSum1 + fileStream.ReadByte()) % ADLER_MODULE);
|
||||||
localSum2 = (ushort)((localSum2 + localSum1) % ADLER_MODULE);
|
localSum2 = (ushort)((localSum2 + localSum1) % ADLER_MODULE);
|
||||||
}
|
}
|
||||||
|
|
||||||
finalSum = (uint)((localSum2 << 16) | localSum1);
|
finalSum = (uint)((localSum2 << 16) | localSum1);
|
||||||
|
|
||||||
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
||||||
hash = BigEndianBitConverter.GetBytes(finalSum);
|
hash = BigEndianBitConverter.GetBytes(finalSum);
|
||||||
|
|
||||||
StringBuilder adlerOutput = new StringBuilder();
|
StringBuilder adlerOutput = new StringBuilder();
|
||||||
|
|
||||||
@@ -154,21 +154,21 @@ namespace DiscImageChef.Checksums
|
|||||||
public static string Data(byte[] data, uint len, out byte[] hash)
|
public static string Data(byte[] data, uint len, out byte[] hash)
|
||||||
{
|
{
|
||||||
ushort localSum1, localSum2;
|
ushort localSum1, localSum2;
|
||||||
uint finalSum;
|
uint finalSum;
|
||||||
|
|
||||||
localSum1 = 1;
|
localSum1 = 1;
|
||||||
localSum2 = 0;
|
localSum2 = 0;
|
||||||
|
|
||||||
for(int i = 0; i < len; i++)
|
for(int i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
localSum1 = (ushort)((localSum1 + data[i]) % ADLER_MODULE);
|
localSum1 = (ushort)((localSum1 + data[i]) % ADLER_MODULE);
|
||||||
localSum2 = (ushort)((localSum2 + localSum1) % ADLER_MODULE);
|
localSum2 = (ushort)((localSum2 + localSum1) % ADLER_MODULE);
|
||||||
}
|
}
|
||||||
|
|
||||||
finalSum = (uint)((localSum2 << 16) | localSum1);
|
finalSum = (uint)((localSum2 << 16) | localSum1);
|
||||||
|
|
||||||
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
||||||
hash = BigEndianBitConverter.GetBytes(finalSum);
|
hash = BigEndianBitConverter.GetBytes(finalSum);
|
||||||
|
|
||||||
StringBuilder adlerOutput = new StringBuilder();
|
StringBuilder adlerOutput = new StringBuilder();
|
||||||
|
|
||||||
|
|||||||
@@ -43,14 +43,14 @@ namespace DiscImageChef.Checksums
|
|||||||
{
|
{
|
||||||
const ushort CRC16_POLY = 0xA001;
|
const ushort CRC16_POLY = 0xA001;
|
||||||
const ushort CRC16_SEED = 0x0000;
|
const ushort CRC16_SEED = 0x0000;
|
||||||
ushort hashInt;
|
|
||||||
|
|
||||||
ushort[] table;
|
readonly ushort[] table;
|
||||||
|
ushort hashInt;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes the CRC16 table and seed
|
/// Initializes the CRC16 table and seed
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Init()
|
public Crc16Context()
|
||||||
{
|
{
|
||||||
hashInt = CRC16_SEED;
|
hashInt = CRC16_SEED;
|
||||||
|
|
||||||
@@ -58,9 +58,11 @@ namespace DiscImageChef.Checksums
|
|||||||
for(int i = 0; i < 256; i++)
|
for(int i = 0; i < 256; i++)
|
||||||
{
|
{
|
||||||
ushort entry = (ushort)i;
|
ushort entry = (ushort)i;
|
||||||
for(int j = 0; j < 8; j++)
|
for(int j = 0; j < 8; j++)
|
||||||
if((entry & 1) == 1) entry = (ushort)((entry >> 1) ^ CRC16_POLY);
|
if((entry & 1) == 1)
|
||||||
else entry = (ushort)(entry >> 1);
|
entry = (ushort)((entry >> 1) ^ CRC16_POLY);
|
||||||
|
else
|
||||||
|
entry = (ushort)(entry >> 1);
|
||||||
|
|
||||||
table[i] = entry;
|
table[i] = entry;
|
||||||
}
|
}
|
||||||
@@ -102,7 +104,7 @@ namespace DiscImageChef.Checksums
|
|||||||
StringBuilder crc16Output = new StringBuilder();
|
StringBuilder crc16Output = new StringBuilder();
|
||||||
|
|
||||||
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
||||||
for(int i = 0; i < BigEndianBitConverter.GetBytes(hashInt ^ CRC16_SEED).Length; i++)
|
for(int i = 0; i < BigEndianBitConverter.GetBytes(hashInt ^ CRC16_SEED).Length; i++)
|
||||||
crc16Output.Append(BigEndianBitConverter.GetBytes(hashInt ^ CRC16_SEED)[i].ToString("x2"));
|
crc16Output.Append(BigEndianBitConverter.GetBytes(hashInt ^ CRC16_SEED)[i].ToString("x2"));
|
||||||
|
|
||||||
return crc16Output.ToString();
|
return crc16Output.ToString();
|
||||||
@@ -126,7 +128,7 @@ namespace DiscImageChef.Checksums
|
|||||||
public static string File(string filename, out byte[] hash)
|
public static string File(string filename, out byte[] hash)
|
||||||
{
|
{
|
||||||
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
||||||
ushort localhashInt;
|
ushort localhashInt;
|
||||||
|
|
||||||
localhashInt = CRC16_SEED;
|
localhashInt = CRC16_SEED;
|
||||||
|
|
||||||
@@ -134,9 +136,11 @@ namespace DiscImageChef.Checksums
|
|||||||
for(int i = 0; i < 256; i++)
|
for(int i = 0; i < 256; i++)
|
||||||
{
|
{
|
||||||
ushort entry = (ushort)i;
|
ushort entry = (ushort)i;
|
||||||
for(int j = 0; j < 8; j++)
|
for(int j = 0; j < 8; j++)
|
||||||
if((entry & 1) == 1) entry = (ushort)((entry >> 1) ^ CRC16_POLY);
|
if((entry & 1) == 1)
|
||||||
else entry = (ushort)(entry >> 1);
|
entry = (ushort)((entry >> 1) ^ CRC16_POLY);
|
||||||
|
else
|
||||||
|
entry = (ushort)(entry >> 1);
|
||||||
|
|
||||||
localTable[i] = entry;
|
localTable[i] = entry;
|
||||||
}
|
}
|
||||||
@@ -146,7 +150,7 @@ namespace DiscImageChef.Checksums
|
|||||||
(ushort)((localhashInt >> 8) ^ localTable[fileStream.ReadByte() ^ (localhashInt & 0xff)]);
|
(ushort)((localhashInt >> 8) ^ localTable[fileStream.ReadByte() ^ (localhashInt & 0xff)]);
|
||||||
|
|
||||||
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
||||||
hash = BigEndianBitConverter.GetBytes(localhashInt);
|
hash = BigEndianBitConverter.GetBytes(localhashInt);
|
||||||
|
|
||||||
StringBuilder crc16Output = new StringBuilder();
|
StringBuilder crc16Output = new StringBuilder();
|
||||||
|
|
||||||
@@ -186,9 +190,11 @@ namespace DiscImageChef.Checksums
|
|||||||
for(int i = 0; i < 256; i++)
|
for(int i = 0; i < 256; i++)
|
||||||
{
|
{
|
||||||
ushort entry = (ushort)i;
|
ushort entry = (ushort)i;
|
||||||
for(int j = 0; j < 8; j++)
|
for(int j = 0; j < 8; j++)
|
||||||
if((entry & 1) == 1) entry = (ushort)((entry >> 1) ^ polynomial);
|
if((entry & 1) == 1)
|
||||||
else entry = (ushort)(entry >> 1);
|
entry = (ushort)((entry >> 1) ^ polynomial);
|
||||||
|
else
|
||||||
|
entry = (ushort)(entry >> 1);
|
||||||
|
|
||||||
localTable[i] = entry;
|
localTable[i] = entry;
|
||||||
}
|
}
|
||||||
@@ -197,7 +203,7 @@ namespace DiscImageChef.Checksums
|
|||||||
localhashInt = (ushort)((localhashInt >> 8) ^ localTable[data[i] ^ (localhashInt & 0xff)]);
|
localhashInt = (ushort)((localhashInt >> 8) ^ localTable[data[i] ^ (localhashInt & 0xff)]);
|
||||||
|
|
||||||
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
||||||
hash = BigEndianBitConverter.GetBytes(localhashInt);
|
hash = BigEndianBitConverter.GetBytes(localhashInt);
|
||||||
|
|
||||||
StringBuilder crc16Output = new StringBuilder();
|
StringBuilder crc16Output = new StringBuilder();
|
||||||
|
|
||||||
|
|||||||
@@ -43,14 +43,14 @@ namespace DiscImageChef.Checksums
|
|||||||
{
|
{
|
||||||
const uint CRC32_POLY = 0xEDB88320;
|
const uint CRC32_POLY = 0xEDB88320;
|
||||||
const uint CRC32_SEED = 0xFFFFFFFF;
|
const uint CRC32_SEED = 0xFFFFFFFF;
|
||||||
uint hashInt;
|
|
||||||
|
|
||||||
uint[] table;
|
readonly uint[] table;
|
||||||
|
uint hashInt;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes the CRC32 table and seed
|
/// Initializes the CRC32 table and seed
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Init()
|
public Crc32Context()
|
||||||
{
|
{
|
||||||
hashInt = CRC32_SEED;
|
hashInt = CRC32_SEED;
|
||||||
|
|
||||||
@@ -58,9 +58,11 @@ namespace DiscImageChef.Checksums
|
|||||||
for(int i = 0; i < 256; i++)
|
for(int i = 0; i < 256; i++)
|
||||||
{
|
{
|
||||||
uint entry = (uint)i;
|
uint entry = (uint)i;
|
||||||
for(int j = 0; j < 8; j++)
|
for(int j = 0; j < 8; j++)
|
||||||
if((entry & 1) == 1) entry = (entry >> 1) ^ CRC32_POLY;
|
if((entry & 1) == 1)
|
||||||
else entry = entry >> 1;
|
entry = (entry >> 1) ^ CRC32_POLY;
|
||||||
|
else
|
||||||
|
entry = entry >> 1;
|
||||||
|
|
||||||
table[i] = entry;
|
table[i] = entry;
|
||||||
}
|
}
|
||||||
@@ -102,7 +104,7 @@ namespace DiscImageChef.Checksums
|
|||||||
StringBuilder crc32Output = new StringBuilder();
|
StringBuilder crc32Output = new StringBuilder();
|
||||||
|
|
||||||
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
||||||
for(int i = 0; i < BigEndianBitConverter.GetBytes(hashInt ^ CRC32_SEED).Length; i++)
|
for(int i = 0; i < BigEndianBitConverter.GetBytes(hashInt ^ CRC32_SEED).Length; i++)
|
||||||
crc32Output.Append(BigEndianBitConverter.GetBytes(hashInt ^ CRC32_SEED)[i].ToString("x2"));
|
crc32Output.Append(BigEndianBitConverter.GetBytes(hashInt ^ CRC32_SEED)[i].ToString("x2"));
|
||||||
|
|
||||||
return crc32Output.ToString();
|
return crc32Output.ToString();
|
||||||
@@ -126,7 +128,7 @@ namespace DiscImageChef.Checksums
|
|||||||
public static string File(string filename, out byte[] hash)
|
public static string File(string filename, out byte[] hash)
|
||||||
{
|
{
|
||||||
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
||||||
uint localhashInt;
|
uint localhashInt;
|
||||||
|
|
||||||
localhashInt = CRC32_SEED;
|
localhashInt = CRC32_SEED;
|
||||||
|
|
||||||
@@ -134,25 +136,27 @@ namespace DiscImageChef.Checksums
|
|||||||
for(int i = 0; i < 256; i++)
|
for(int i = 0; i < 256; i++)
|
||||||
{
|
{
|
||||||
uint entry = (uint)i;
|
uint entry = (uint)i;
|
||||||
for(int j = 0; j < 8; j++)
|
for(int j = 0; j < 8; j++)
|
||||||
if((entry & 1) == 1) entry = (entry >> 1) ^ CRC32_POLY;
|
if((entry & 1) == 1)
|
||||||
else entry = entry >> 1;
|
entry = (entry >> 1) ^ CRC32_POLY;
|
||||||
|
else
|
||||||
|
entry = entry >> 1;
|
||||||
|
|
||||||
localTable[i] = entry;
|
localTable[i] = entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
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) ^
|
||||||
if((localhashInt ^ CRC32_SEED) == 0xB883C628 || (localhashInt ^CRC32_SEED) == 0x28C683B8)
|
localTable[fileStream.ReadByte() ^ (localhashInt & 0xff)];
|
||||||
{
|
if((localhashInt ^ CRC32_SEED) == 0xB883C628 ||
|
||||||
|
(localhashInt ^ CRC32_SEED) == 0x28C683B8)
|
||||||
System.Console.WriteLine("CRC found at position {0}", fileStream.Position);
|
System.Console.WriteLine("CRC found at position {0}", fileStream.Position);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
localhashInt ^= CRC32_SEED;
|
localhashInt ^= CRC32_SEED;
|
||||||
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
||||||
hash = BigEndianBitConverter.GetBytes(localhashInt);
|
hash = BigEndianBitConverter.GetBytes(localhashInt);
|
||||||
|
|
||||||
StringBuilder crc32Output = new StringBuilder();
|
StringBuilder crc32Output = new StringBuilder();
|
||||||
|
|
||||||
@@ -192,9 +196,11 @@ namespace DiscImageChef.Checksums
|
|||||||
for(int i = 0; i < 256; i++)
|
for(int i = 0; i < 256; i++)
|
||||||
{
|
{
|
||||||
uint entry = (uint)i;
|
uint entry = (uint)i;
|
||||||
for(int j = 0; j < 8; j++)
|
for(int j = 0; j < 8; j++)
|
||||||
if((entry & 1) == 1) entry = (entry >> 1) ^ polynomial;
|
if((entry & 1) == 1)
|
||||||
else entry = entry >> 1;
|
entry = (entry >> 1) ^ polynomial;
|
||||||
|
else
|
||||||
|
entry = entry >> 1;
|
||||||
|
|
||||||
localTable[i] = entry;
|
localTable[i] = entry;
|
||||||
}
|
}
|
||||||
@@ -202,9 +208,9 @@ namespace DiscImageChef.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)];
|
||||||
|
|
||||||
localhashInt ^= CRC32_SEED;
|
localhashInt ^= CRC32_SEED;
|
||||||
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
||||||
hash = BigEndianBitConverter.GetBytes(localhashInt);
|
hash = BigEndianBitConverter.GetBytes(localhashInt);
|
||||||
|
|
||||||
StringBuilder crc32Output = new StringBuilder();
|
StringBuilder crc32Output = new StringBuilder();
|
||||||
|
|
||||||
|
|||||||
@@ -43,14 +43,14 @@ namespace DiscImageChef.Checksums
|
|||||||
{
|
{
|
||||||
const ulong CRC64_POLY = 0xC96C5795D7870F42;
|
const ulong CRC64_POLY = 0xC96C5795D7870F42;
|
||||||
const ulong CRC64_SEED = 0xFFFFFFFFFFFFFFFF;
|
const ulong CRC64_SEED = 0xFFFFFFFFFFFFFFFF;
|
||||||
ulong hashInt;
|
|
||||||
|
|
||||||
ulong[] table;
|
readonly ulong[] table;
|
||||||
|
ulong hashInt;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes the CRC64 table and seed
|
/// Initializes the CRC64 table and seed
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Init()
|
public Crc64Context()
|
||||||
{
|
{
|
||||||
hashInt = CRC64_SEED;
|
hashInt = CRC64_SEED;
|
||||||
|
|
||||||
@@ -58,9 +58,11 @@ namespace DiscImageChef.Checksums
|
|||||||
for(int i = 0; i < 256; i++)
|
for(int i = 0; i < 256; i++)
|
||||||
{
|
{
|
||||||
ulong entry = (ulong)i;
|
ulong entry = (ulong)i;
|
||||||
for(int j = 0; j < 8; j++)
|
for(int j = 0; j < 8; j++)
|
||||||
if((entry & 1) == 1) entry = (entry >> 1) ^ CRC64_POLY;
|
if((entry & 1) == 1)
|
||||||
else entry = entry >> 1;
|
entry = (entry >> 1) ^ CRC64_POLY;
|
||||||
|
else
|
||||||
|
entry = entry >> 1;
|
||||||
|
|
||||||
table[i] = entry;
|
table[i] = entry;
|
||||||
}
|
}
|
||||||
@@ -126,7 +128,7 @@ namespace DiscImageChef.Checksums
|
|||||||
public static string File(string filename, out byte[] hash)
|
public static string File(string filename, out byte[] hash)
|
||||||
{
|
{
|
||||||
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
||||||
ulong localhashInt;
|
ulong localhashInt;
|
||||||
|
|
||||||
localhashInt = CRC64_SEED;
|
localhashInt = CRC64_SEED;
|
||||||
|
|
||||||
@@ -134,9 +136,11 @@ namespace DiscImageChef.Checksums
|
|||||||
for(int i = 0; i < 256; i++)
|
for(int i = 0; i < 256; i++)
|
||||||
{
|
{
|
||||||
ulong entry = (ulong)i;
|
ulong entry = (ulong)i;
|
||||||
for(int j = 0; j < 8; j++)
|
for(int j = 0; j < 8; j++)
|
||||||
if((entry & 1) == 1) entry = (entry >> 1) ^ CRC64_POLY;
|
if((entry & 1) == 1)
|
||||||
else entry = entry >> 1;
|
entry = (entry >> 1) ^ CRC64_POLY;
|
||||||
|
else
|
||||||
|
entry = entry >> 1;
|
||||||
|
|
||||||
localTable[i] = entry;
|
localTable[i] = entry;
|
||||||
}
|
}
|
||||||
@@ -144,9 +148,9 @@ namespace DiscImageChef.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 & 0xffL)];
|
localhashInt = (localhashInt >> 8) ^ localTable[(ulong)fileStream.ReadByte() ^ (localhashInt & 0xffL)];
|
||||||
|
|
||||||
localhashInt ^= CRC64_SEED;
|
localhashInt ^= CRC64_SEED;
|
||||||
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
||||||
hash = BigEndianBitConverter.GetBytes(localhashInt);
|
hash = BigEndianBitConverter.GetBytes(localhashInt);
|
||||||
|
|
||||||
StringBuilder crc64Output = new StringBuilder();
|
StringBuilder crc64Output = new StringBuilder();
|
||||||
|
|
||||||
@@ -186,9 +190,11 @@ namespace DiscImageChef.Checksums
|
|||||||
for(int i = 0; i < 256; i++)
|
for(int i = 0; i < 256; i++)
|
||||||
{
|
{
|
||||||
ulong entry = (ulong)i;
|
ulong entry = (ulong)i;
|
||||||
for(int j = 0; j < 8; j++)
|
for(int j = 0; j < 8; j++)
|
||||||
if((entry & 1) == 1) entry = (entry >> 1) ^ polynomial;
|
if((entry & 1) == 1)
|
||||||
else entry = entry >> 1;
|
entry = (entry >> 1) ^ polynomial;
|
||||||
|
else
|
||||||
|
entry = entry >> 1;
|
||||||
|
|
||||||
localTable[i] = entry;
|
localTable[i] = entry;
|
||||||
}
|
}
|
||||||
@@ -196,9 +202,9 @@ namespace DiscImageChef.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)];
|
||||||
|
|
||||||
localhashInt ^= CRC64_SEED;
|
localhashInt ^= CRC64_SEED;
|
||||||
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
|
||||||
hash = BigEndianBitConverter.GetBytes(localhashInt);
|
hash = BigEndianBitConverter.GetBytes(localhashInt);
|
||||||
|
|
||||||
StringBuilder crc64Output = new StringBuilder();
|
StringBuilder crc64Output = new StringBuilder();
|
||||||
|
|
||||||
|
|||||||
@@ -40,19 +40,19 @@ namespace DiscImageChef.Checksums
|
|||||||
{
|
{
|
||||||
public class Fletcher32Context : IChecksum
|
public class Fletcher32Context : IChecksum
|
||||||
{
|
{
|
||||||
bool inodd;
|
bool inodd;
|
||||||
byte oddValue;
|
byte oddValue;
|
||||||
ushort sum1, sum2;
|
ushort sum1, sum2;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes the Fletcher32 sums
|
/// Initializes the Fletcher32 sums
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Init()
|
public Fletcher32Context()
|
||||||
{
|
{
|
||||||
sum1 = 0xFFFF;
|
sum1 = 0xFFFF;
|
||||||
sum2 = 0xFFFF;
|
sum2 = 0xFFFF;
|
||||||
oddValue = 0;
|
oddValue = 0;
|
||||||
inodd = false;
|
inodd = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -67,13 +67,13 @@ namespace DiscImageChef.Checksums
|
|||||||
if(len % 2 != 0)
|
if(len % 2 != 0)
|
||||||
{
|
{
|
||||||
oddValue = data[len - 1];
|
oddValue = data[len - 1];
|
||||||
inodd = true;
|
inodd = true;
|
||||||
|
|
||||||
for(int i = 0; i < len - 1; i += 2)
|
for(int i = 0; i < len - 1; i += 2)
|
||||||
{
|
{
|
||||||
block = BigEndianBitConverter.ToUInt16(data, i);
|
block = BigEndianBitConverter.ToUInt16(data, i);
|
||||||
sum1 = (ushort)((sum1 + block) % 0xFFFF);
|
sum1 = (ushort)((sum1 + block) % 0xFFFF);
|
||||||
sum2 = (ushort)((sum2 + sum1) % 0xFFFF);
|
sum2 = (ushort)((sum2 + sum1) % 0xFFFF);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -82,32 +82,32 @@ namespace DiscImageChef.Checksums
|
|||||||
for(int i = 0; i < len; i += 2)
|
for(int i = 0; i < len; i += 2)
|
||||||
{
|
{
|
||||||
block = BigEndianBitConverter.ToUInt16(data, i);
|
block = BigEndianBitConverter.ToUInt16(data, i);
|
||||||
sum1 = (ushort)((sum1 + block) % 0xFFFF);
|
sum1 = (ushort)((sum1 + block) % 0xFFFF);
|
||||||
sum2 = (ushort)((sum2 + sum1) % 0xFFFF);
|
sum2 = (ushort)((sum2 + sum1) % 0xFFFF);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Carrying odd
|
// Carrying odd
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
byte[] oddData = new byte[2];
|
byte[] oddData = new byte[2];
|
||||||
oddData[0] = oddValue;
|
oddData[0] = oddValue;
|
||||||
oddData[1] = data[0];
|
oddData[1] = data[0];
|
||||||
|
|
||||||
block = BigEndianBitConverter.ToUInt16(oddData, 0);
|
block = BigEndianBitConverter.ToUInt16(oddData, 0);
|
||||||
sum1 = (ushort)((sum1 + block) % 0xFFFF);
|
sum1 = (ushort)((sum1 + block) % 0xFFFF);
|
||||||
sum2 = (ushort)((sum2 + sum1) % 0xFFFF);
|
sum2 = (ushort)((sum2 + sum1) % 0xFFFF);
|
||||||
|
|
||||||
// Even size, carrying odd
|
// Even size, carrying odd
|
||||||
if(len % 2 == 0)
|
if(len % 2 == 0)
|
||||||
{
|
{
|
||||||
oddValue = data[len - 1];
|
oddValue = data[len - 1];
|
||||||
inodd = true;
|
inodd = true;
|
||||||
|
|
||||||
for(int i = 1; i < len - 1; i += 2)
|
for(int i = 1; i < len - 1; i += 2)
|
||||||
{
|
{
|
||||||
block = BigEndianBitConverter.ToUInt16(data, i);
|
block = BigEndianBitConverter.ToUInt16(data, i);
|
||||||
sum1 = (ushort)((sum1 + block) % 0xFFFF);
|
sum1 = (ushort)((sum1 + block) % 0xFFFF);
|
||||||
sum2 = (ushort)((sum2 + sum1) % 0xFFFF);
|
sum2 = (ushort)((sum2 + sum1) % 0xFFFF);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -116,8 +116,8 @@ namespace DiscImageChef.Checksums
|
|||||||
for(int i = 1; i < len; i += 2)
|
for(int i = 1; i < len; i += 2)
|
||||||
{
|
{
|
||||||
block = BigEndianBitConverter.ToUInt16(data, i);
|
block = BigEndianBitConverter.ToUInt16(data, i);
|
||||||
sum1 = (ushort)((sum1 + block) % 0xFFFF);
|
sum1 = (ushort)((sum1 + block) % 0xFFFF);
|
||||||
sum2 = (ushort)((sum2 + sum1) % 0xFFFF);
|
sum2 = (ushort)((sum2 + sum1) % 0xFFFF);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -146,7 +146,7 @@ namespace DiscImageChef.Checksums
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string End()
|
public string End()
|
||||||
{
|
{
|
||||||
uint finalSum = (uint)(sum1 + (sum2 << 16));
|
uint finalSum = (uint)(sum1 + (sum2 << 16));
|
||||||
StringBuilder fletcherOutput = new StringBuilder();
|
StringBuilder fletcherOutput = new StringBuilder();
|
||||||
|
|
||||||
for(int i = 0; i < BigEndianBitConverter.GetBytes(finalSum).Length; i++)
|
for(int i = 0; i < BigEndianBitConverter.GetBytes(finalSum).Length; i++)
|
||||||
@@ -173,20 +173,20 @@ namespace DiscImageChef.Checksums
|
|||||||
public static string File(string filename, out byte[] hash)
|
public static string File(string filename, out byte[] hash)
|
||||||
{
|
{
|
||||||
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
||||||
ushort localSum1, localSum2, block;
|
ushort localSum1, localSum2, block;
|
||||||
uint finalSum;
|
uint finalSum;
|
||||||
byte[] blockBytes;
|
byte[] blockBytes;
|
||||||
|
|
||||||
localSum1 = 0xFFFF;
|
localSum1 = 0xFFFF;
|
||||||
localSum2 = 0xFFFF;
|
localSum2 = 0xFFFF;
|
||||||
|
|
||||||
if(fileStream.Length % 2 == 0)
|
if(fileStream.Length % 2 == 0)
|
||||||
for(int i = 0; i < fileStream.Length; i += 2)
|
for(int i = 0; i < fileStream.Length; i += 2)
|
||||||
{
|
{
|
||||||
blockBytes = new byte[2];
|
blockBytes = new byte[2];
|
||||||
fileStream.Read(blockBytes, 0, 2);
|
fileStream.Read(blockBytes, 0, 2);
|
||||||
block = BigEndianBitConverter.ToUInt16(blockBytes, 0);
|
block = BigEndianBitConverter.ToUInt16(blockBytes, 0);
|
||||||
localSum1 = (ushort)((localSum1 + block) % 0xFFFF);
|
localSum1 = (ushort)((localSum1 + block) % 0xFFFF);
|
||||||
localSum2 = (ushort)((localSum2 + localSum1) % 0xFFFF);
|
localSum2 = (ushort)((localSum2 + localSum1) % 0xFFFF);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -195,17 +195,17 @@ namespace DiscImageChef.Checksums
|
|||||||
{
|
{
|
||||||
blockBytes = new byte[2];
|
blockBytes = new byte[2];
|
||||||
fileStream.Read(blockBytes, 0, 2);
|
fileStream.Read(blockBytes, 0, 2);
|
||||||
block = BigEndianBitConverter.ToUInt16(blockBytes, 0);
|
block = BigEndianBitConverter.ToUInt16(blockBytes, 0);
|
||||||
localSum1 = (ushort)((localSum1 + block) % 0xFFFF);
|
localSum1 = (ushort)((localSum1 + block) % 0xFFFF);
|
||||||
localSum2 = (ushort)((localSum2 + localSum1) % 0xFFFF);
|
localSum2 = (ushort)((localSum2 + localSum1) % 0xFFFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] oddData = new byte[2];
|
byte[] oddData = new byte[2];
|
||||||
oddData[0] = (byte)fileStream.ReadByte();
|
oddData[0] = (byte)fileStream.ReadByte();
|
||||||
oddData[1] = 0;
|
oddData[1] = 0;
|
||||||
|
|
||||||
block = BigEndianBitConverter.ToUInt16(oddData, 0);
|
block = BigEndianBitConverter.ToUInt16(oddData, 0);
|
||||||
localSum1 = (ushort)((localSum1 + block) % 0xFFFF);
|
localSum1 = (ushort)((localSum1 + block) % 0xFFFF);
|
||||||
localSum2 = (ushort)((localSum2 + localSum1) % 0xFFFF);
|
localSum2 = (ushort)((localSum2 + localSum1) % 0xFFFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,33 +229,33 @@ namespace DiscImageChef.Checksums
|
|||||||
public static string Data(byte[] data, uint len, out byte[] hash)
|
public static string Data(byte[] data, uint len, out byte[] hash)
|
||||||
{
|
{
|
||||||
ushort localSum1, localSum2, block;
|
ushort localSum1, localSum2, block;
|
||||||
uint finalSum;
|
uint finalSum;
|
||||||
|
|
||||||
localSum1 = 0xFFFF;
|
localSum1 = 0xFFFF;
|
||||||
localSum2 = 0xFFFF;
|
localSum2 = 0xFFFF;
|
||||||
|
|
||||||
if(len % 2 == 0)
|
if(len % 2 == 0)
|
||||||
for(int i = 0; i < len; i += 2)
|
for(int i = 0; i < len; i += 2)
|
||||||
{
|
{
|
||||||
block = BigEndianBitConverter.ToUInt16(data, i);
|
block = BigEndianBitConverter.ToUInt16(data, i);
|
||||||
localSum1 = (ushort)((localSum1 + block) % 0xFFFF);
|
localSum1 = (ushort)((localSum1 + block) % 0xFFFF);
|
||||||
localSum2 = (ushort)((localSum2 + localSum1) % 0xFFFF);
|
localSum2 = (ushort)((localSum2 + localSum1) % 0xFFFF);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
for(int i = 0; i < len - 1; i += 2)
|
for(int i = 0; i < len - 1; i += 2)
|
||||||
{
|
{
|
||||||
block = BigEndianBitConverter.ToUInt16(data, i);
|
block = BigEndianBitConverter.ToUInt16(data, i);
|
||||||
localSum1 = (ushort)((localSum1 + block) % 0xFFFF);
|
localSum1 = (ushort)((localSum1 + block) % 0xFFFF);
|
||||||
localSum2 = (ushort)((localSum2 + localSum1) % 0xFFFF);
|
localSum2 = (ushort)((localSum2 + localSum1) % 0xFFFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] oddData = new byte[2];
|
byte[] oddData = new byte[2];
|
||||||
oddData[0] = data[len - 1];
|
oddData[0] = data[len - 1];
|
||||||
oddData[1] = 0;
|
oddData[1] = 0;
|
||||||
|
|
||||||
block = BigEndianBitConverter.ToUInt16(oddData, 0);
|
block = BigEndianBitConverter.ToUInt16(oddData, 0);
|
||||||
localSum1 = (ushort)((localSum1 + block) % 0xFFFF);
|
localSum1 = (ushort)((localSum1 + block) % 0xFFFF);
|
||||||
localSum2 = (ushort)((localSum2 + localSum1) % 0xFFFF);
|
localSum2 = (ushort)((localSum2 + localSum1) % 0xFFFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -304,7 +304,7 @@ namespace DiscImageChef.Checksums
|
|||||||
for(int i = 0; i < len; i++)
|
for(int i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
sum1 = (byte)((sum1 + data[i]) % 0xFF);
|
sum1 = (byte)((sum1 + data[i]) % 0xFF);
|
||||||
sum2 = (byte)((sum2 + sum1) % 0xFF);
|
sum2 = (byte)((sum2 + sum1) % 0xFF);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -331,7 +331,7 @@ namespace DiscImageChef.Checksums
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string End()
|
public string End()
|
||||||
{
|
{
|
||||||
ushort finalSum = (ushort)(sum1 + (sum2 << 8));
|
ushort finalSum = (ushort)(sum1 + (sum2 << 8));
|
||||||
StringBuilder fletcherOutput = new StringBuilder();
|
StringBuilder fletcherOutput = new StringBuilder();
|
||||||
|
|
||||||
for(int i = 0; i < BigEndianBitConverter.GetBytes(finalSum).Length; i++)
|
for(int i = 0; i < BigEndianBitConverter.GetBytes(finalSum).Length; i++)
|
||||||
@@ -358,16 +358,16 @@ namespace DiscImageChef.Checksums
|
|||||||
public static string File(string filename, out byte[] hash)
|
public static string File(string filename, out byte[] hash)
|
||||||
{
|
{
|
||||||
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
||||||
byte localSum1, localSum2, block;
|
byte localSum1, localSum2, block;
|
||||||
ushort finalSum;
|
ushort finalSum;
|
||||||
|
|
||||||
localSum1 = 0xFF;
|
localSum1 = 0xFF;
|
||||||
localSum2 = 0xFF;
|
localSum2 = 0xFF;
|
||||||
|
|
||||||
for(int i = 0; i < fileStream.Length; i += 2)
|
for(int i = 0; i < fileStream.Length; i += 2)
|
||||||
{
|
{
|
||||||
block = (byte)fileStream.ReadByte();
|
block = (byte)fileStream.ReadByte();
|
||||||
localSum1 = (byte)((localSum1 + block) % 0xFF);
|
localSum1 = (byte)((localSum1 + block) % 0xFF);
|
||||||
localSum2 = (byte)((localSum2 + localSum1) % 0xFF);
|
localSum2 = (byte)((localSum2 + localSum1) % 0xFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -390,7 +390,7 @@ namespace DiscImageChef.Checksums
|
|||||||
/// <param name="hash">Byte array of the hash value.</param>
|
/// <param name="hash">Byte array of the hash value.</param>
|
||||||
public static string Data(byte[] data, uint len, out byte[] hash)
|
public static string Data(byte[] data, uint len, out byte[] hash)
|
||||||
{
|
{
|
||||||
byte localSum1, localSum2;
|
byte localSum1, localSum2;
|
||||||
ushort finalSum;
|
ushort finalSum;
|
||||||
|
|
||||||
localSum1 = 0xFF;
|
localSum1 = 0xFF;
|
||||||
@@ -398,7 +398,7 @@ namespace DiscImageChef.Checksums
|
|||||||
|
|
||||||
for(int i = 0; i < len; i++)
|
for(int i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
localSum1 = (byte)((localSum1 + data[i]) % 0xFF);
|
localSum1 = (byte)((localSum1 + data[i]) % 0xFF);
|
||||||
localSum2 = (byte)((localSum2 + localSum1) % 0xFF);
|
localSum2 = (byte)((localSum2 + localSum1) % 0xFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,11 +34,6 @@ namespace DiscImageChef.Checksums
|
|||||||
{
|
{
|
||||||
public interface IChecksum
|
public interface IChecksum
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Initializes the algorithm
|
|
||||||
/// </summary>
|
|
||||||
void Init();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Updates the hash with data.
|
/// Updates the hash with data.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ namespace DiscImageChef.Checksums
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes the MD5 hash provider
|
/// Initializes the MD5 hash provider
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Init()
|
public Md5Context()
|
||||||
{
|
{
|
||||||
md5Provider = MD5.Create();
|
md5Provider = MD5.Create();
|
||||||
}
|
}
|
||||||
@@ -99,7 +99,7 @@ namespace DiscImageChef.Checksums
|
|||||||
public byte[] File(string filename)
|
public byte[] File(string filename)
|
||||||
{
|
{
|
||||||
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
||||||
byte[] result = md5Provider.ComputeHash(fileStream);
|
byte[] result = md5Provider.ComputeHash(fileStream);
|
||||||
fileStream.Close();
|
fileStream.Close();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -111,8 +111,8 @@ namespace DiscImageChef.Checksums
|
|||||||
/// <param name="hash">Byte array of the hash value.</param>
|
/// <param name="hash">Byte array of the hash value.</param>
|
||||||
public string File(string filename, out byte[] hash)
|
public string File(string filename, out byte[] hash)
|
||||||
{
|
{
|
||||||
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
||||||
hash = md5Provider.ComputeHash(fileStream);
|
hash = md5Provider.ComputeHash(fileStream);
|
||||||
StringBuilder md5Output = new StringBuilder();
|
StringBuilder md5Output = new StringBuilder();
|
||||||
|
|
||||||
foreach(byte h in hash) md5Output.Append(h.ToString("x2"));
|
foreach(byte h in hash) md5Output.Append(h.ToString("x2"));
|
||||||
@@ -130,7 +130,7 @@ namespace DiscImageChef.Checksums
|
|||||||
/// <param name="hash">Byte array of the hash value.</param>
|
/// <param name="hash">Byte array of the hash value.</param>
|
||||||
public string Data(byte[] data, uint len, out byte[] hash)
|
public string Data(byte[] data, uint len, out byte[] hash)
|
||||||
{
|
{
|
||||||
hash = md5Provider.ComputeHash(data, 0, (int)len);
|
hash = md5Provider.ComputeHash(data, 0, (int)len);
|
||||||
StringBuilder md5Output = new StringBuilder();
|
StringBuilder md5Output = new StringBuilder();
|
||||||
|
|
||||||
foreach(byte h in hash) md5Output.Append(h.ToString("x2"));
|
foreach(byte h in hash) md5Output.Append(h.ToString("x2"));
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ namespace DiscImageChef.Checksums
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes the RIPEMD160 hash provider
|
/// Initializes the RIPEMD160 hash provider
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Init()
|
public Ripemd160Context()
|
||||||
{
|
{
|
||||||
ripemd160Provider = RIPEMD160.Create();
|
ripemd160Provider = RIPEMD160.Create();
|
||||||
}
|
}
|
||||||
@@ -99,7 +99,7 @@ namespace DiscImageChef.Checksums
|
|||||||
public byte[] File(string filename)
|
public byte[] File(string filename)
|
||||||
{
|
{
|
||||||
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
||||||
byte[] result = ripemd160Provider.ComputeHash(fileStream);
|
byte[] result = ripemd160Provider.ComputeHash(fileStream);
|
||||||
fileStream.Close();
|
fileStream.Close();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -111,8 +111,8 @@ namespace DiscImageChef.Checksums
|
|||||||
/// <param name="hash">Byte array of the hash value.</param>
|
/// <param name="hash">Byte array of the hash value.</param>
|
||||||
public string File(string filename, out byte[] hash)
|
public string File(string filename, out byte[] hash)
|
||||||
{
|
{
|
||||||
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
||||||
hash = ripemd160Provider.ComputeHash(fileStream);
|
hash = ripemd160Provider.ComputeHash(fileStream);
|
||||||
StringBuilder ripemd160Output = new StringBuilder();
|
StringBuilder ripemd160Output = new StringBuilder();
|
||||||
|
|
||||||
foreach(byte h in hash) ripemd160Output.Append(h.ToString("x2"));
|
foreach(byte h in hash) ripemd160Output.Append(h.ToString("x2"));
|
||||||
@@ -130,7 +130,7 @@ namespace DiscImageChef.Checksums
|
|||||||
/// <param name="hash">Byte array of the hash value.</param>
|
/// <param name="hash">Byte array of the hash value.</param>
|
||||||
public string Data(byte[] data, uint len, out byte[] hash)
|
public string Data(byte[] data, uint len, out byte[] hash)
|
||||||
{
|
{
|
||||||
hash = ripemd160Provider.ComputeHash(data, 0, (int)len);
|
hash = ripemd160Provider.ComputeHash(data, 0, (int)len);
|
||||||
StringBuilder ripemd160Output = new StringBuilder();
|
StringBuilder ripemd160Output = new StringBuilder();
|
||||||
|
|
||||||
foreach(byte h in hash) ripemd160Output.Append(h.ToString("x2"));
|
foreach(byte h in hash) ripemd160Output.Append(h.ToString("x2"));
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ namespace DiscImageChef.Checksums
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes the SHA1 hash provider
|
/// Initializes the SHA1 hash provider
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Init()
|
public Sha1Context()
|
||||||
{
|
{
|
||||||
sha1Provider = SHA1.Create();
|
sha1Provider = SHA1.Create();
|
||||||
}
|
}
|
||||||
@@ -99,7 +99,7 @@ namespace DiscImageChef.Checksums
|
|||||||
public byte[] File(string filename)
|
public byte[] File(string filename)
|
||||||
{
|
{
|
||||||
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
||||||
byte[] result = sha1Provider.ComputeHash(fileStream);
|
byte[] result = sha1Provider.ComputeHash(fileStream);
|
||||||
fileStream.Close();
|
fileStream.Close();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -111,8 +111,8 @@ namespace DiscImageChef.Checksums
|
|||||||
/// <param name="hash">Byte array of the hash value.</param>
|
/// <param name="hash">Byte array of the hash value.</param>
|
||||||
public string File(string filename, out byte[] hash)
|
public string File(string filename, out byte[] hash)
|
||||||
{
|
{
|
||||||
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
||||||
hash = sha1Provider.ComputeHash(fileStream);
|
hash = sha1Provider.ComputeHash(fileStream);
|
||||||
StringBuilder sha1Output = new StringBuilder();
|
StringBuilder sha1Output = new StringBuilder();
|
||||||
|
|
||||||
foreach(byte h in hash) sha1Output.Append(h.ToString("x2"));
|
foreach(byte h in hash) sha1Output.Append(h.ToString("x2"));
|
||||||
@@ -130,7 +130,7 @@ namespace DiscImageChef.Checksums
|
|||||||
/// <param name="hash">Byte array of the hash value.</param>
|
/// <param name="hash">Byte array of the hash value.</param>
|
||||||
public string Data(byte[] data, uint len, out byte[] hash)
|
public string Data(byte[] data, uint len, out byte[] hash)
|
||||||
{
|
{
|
||||||
hash = sha1Provider.ComputeHash(data, 0, (int)len);
|
hash = sha1Provider.ComputeHash(data, 0, (int)len);
|
||||||
StringBuilder sha1Output = new StringBuilder();
|
StringBuilder sha1Output = new StringBuilder();
|
||||||
|
|
||||||
foreach(byte h in hash) sha1Output.Append(h.ToString("x2"));
|
foreach(byte h in hash) sha1Output.Append(h.ToString("x2"));
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ namespace DiscImageChef.Checksums
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes the SHA256 hash provider
|
/// Initializes the SHA256 hash provider
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Init()
|
public Sha256Context()
|
||||||
{
|
{
|
||||||
sha256Provider = SHA256.Create();
|
sha256Provider = SHA256.Create();
|
||||||
}
|
}
|
||||||
@@ -99,7 +99,7 @@ namespace DiscImageChef.Checksums
|
|||||||
public byte[] File(string filename)
|
public byte[] File(string filename)
|
||||||
{
|
{
|
||||||
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
||||||
byte[] result = sha256Provider.ComputeHash(fileStream);
|
byte[] result = sha256Provider.ComputeHash(fileStream);
|
||||||
fileStream.Close();
|
fileStream.Close();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -111,8 +111,8 @@ namespace DiscImageChef.Checksums
|
|||||||
/// <param name="hash">Byte array of the hash value.</param>
|
/// <param name="hash">Byte array of the hash value.</param>
|
||||||
public string File(string filename, out byte[] hash)
|
public string File(string filename, out byte[] hash)
|
||||||
{
|
{
|
||||||
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
||||||
hash = sha256Provider.ComputeHash(fileStream);
|
hash = sha256Provider.ComputeHash(fileStream);
|
||||||
StringBuilder sha256Output = new StringBuilder();
|
StringBuilder sha256Output = new StringBuilder();
|
||||||
|
|
||||||
foreach(byte h in hash) sha256Output.Append(h.ToString("x2"));
|
foreach(byte h in hash) sha256Output.Append(h.ToString("x2"));
|
||||||
@@ -130,7 +130,7 @@ namespace DiscImageChef.Checksums
|
|||||||
/// <param name="hash">Byte array of the hash value.</param>
|
/// <param name="hash">Byte array of the hash value.</param>
|
||||||
public string Data(byte[] data, uint len, out byte[] hash)
|
public string Data(byte[] data, uint len, out byte[] hash)
|
||||||
{
|
{
|
||||||
hash = sha256Provider.ComputeHash(data, 0, (int)len);
|
hash = sha256Provider.ComputeHash(data, 0, (int)len);
|
||||||
StringBuilder sha256Output = new StringBuilder();
|
StringBuilder sha256Output = new StringBuilder();
|
||||||
|
|
||||||
foreach(byte h in hash) sha256Output.Append(h.ToString("x2"));
|
foreach(byte h in hash) sha256Output.Append(h.ToString("x2"));
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ namespace DiscImageChef.Checksums
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes the SHA384 hash provider
|
/// Initializes the SHA384 hash provider
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Init()
|
public Sha384Context()
|
||||||
{
|
{
|
||||||
sha384Provider = SHA384.Create();
|
sha384Provider = SHA384.Create();
|
||||||
}
|
}
|
||||||
@@ -99,7 +99,7 @@ namespace DiscImageChef.Checksums
|
|||||||
public byte[] File(string filename)
|
public byte[] File(string filename)
|
||||||
{
|
{
|
||||||
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
||||||
byte[] result = sha384Provider.ComputeHash(fileStream);
|
byte[] result = sha384Provider.ComputeHash(fileStream);
|
||||||
fileStream.Close();
|
fileStream.Close();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -111,8 +111,8 @@ namespace DiscImageChef.Checksums
|
|||||||
/// <param name="hash">Byte array of the hash value.</param>
|
/// <param name="hash">Byte array of the hash value.</param>
|
||||||
public string File(string filename, out byte[] hash)
|
public string File(string filename, out byte[] hash)
|
||||||
{
|
{
|
||||||
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
||||||
hash = sha384Provider.ComputeHash(fileStream);
|
hash = sha384Provider.ComputeHash(fileStream);
|
||||||
StringBuilder sha384Output = new StringBuilder();
|
StringBuilder sha384Output = new StringBuilder();
|
||||||
|
|
||||||
foreach(byte h in hash) sha384Output.Append(h.ToString("x2"));
|
foreach(byte h in hash) sha384Output.Append(h.ToString("x2"));
|
||||||
@@ -130,7 +130,7 @@ namespace DiscImageChef.Checksums
|
|||||||
/// <param name="hash">Byte array of the hash value.</param>
|
/// <param name="hash">Byte array of the hash value.</param>
|
||||||
public string Data(byte[] data, uint len, out byte[] hash)
|
public string Data(byte[] data, uint len, out byte[] hash)
|
||||||
{
|
{
|
||||||
hash = sha384Provider.ComputeHash(data, 0, (int)len);
|
hash = sha384Provider.ComputeHash(data, 0, (int)len);
|
||||||
StringBuilder sha384Output = new StringBuilder();
|
StringBuilder sha384Output = new StringBuilder();
|
||||||
|
|
||||||
foreach(byte h in hash) sha384Output.Append(h.ToString("x2"));
|
foreach(byte h in hash) sha384Output.Append(h.ToString("x2"));
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ namespace DiscImageChef.Checksums
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes the SHA512 hash provider
|
/// Initializes the SHA512 hash provider
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Init()
|
public Sha512Context()
|
||||||
{
|
{
|
||||||
sha512Provider = SHA512.Create();
|
sha512Provider = SHA512.Create();
|
||||||
}
|
}
|
||||||
@@ -99,7 +99,7 @@ namespace DiscImageChef.Checksums
|
|||||||
public byte[] File(string filename)
|
public byte[] File(string filename)
|
||||||
{
|
{
|
||||||
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
||||||
byte[] result = sha512Provider.ComputeHash(fileStream);
|
byte[] result = sha512Provider.ComputeHash(fileStream);
|
||||||
fileStream.Close();
|
fileStream.Close();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -111,8 +111,8 @@ namespace DiscImageChef.Checksums
|
|||||||
/// <param name="hash">Byte array of the hash value.</param>
|
/// <param name="hash">Byte array of the hash value.</param>
|
||||||
public string File(string filename, out byte[] hash)
|
public string File(string filename, out byte[] hash)
|
||||||
{
|
{
|
||||||
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
||||||
hash = sha512Provider.ComputeHash(fileStream);
|
hash = sha512Provider.ComputeHash(fileStream);
|
||||||
StringBuilder sha512Output = new StringBuilder();
|
StringBuilder sha512Output = new StringBuilder();
|
||||||
|
|
||||||
foreach(byte h in hash) sha512Output.Append(h.ToString("x2"));
|
foreach(byte h in hash) sha512Output.Append(h.ToString("x2"));
|
||||||
@@ -130,7 +130,7 @@ namespace DiscImageChef.Checksums
|
|||||||
/// <param name="hash">Byte array of the hash value.</param>
|
/// <param name="hash">Byte array of the hash value.</param>
|
||||||
public string Data(byte[] data, uint len, out byte[] hash)
|
public string Data(byte[] data, uint len, out byte[] hash)
|
||||||
{
|
{
|
||||||
hash = sha512Provider.ComputeHash(data, 0, (int)len);
|
hash = sha512Provider.ComputeHash(data, 0, (int)len);
|
||||||
StringBuilder sha512Output = new StringBuilder();
|
StringBuilder sha512Output = new StringBuilder();
|
||||||
|
|
||||||
foreach(byte h in hash) sha512Output.Append(h.ToString("x2"));
|
foreach(byte h in hash) sha512Output.Append(h.ToString("x2"));
|
||||||
|
|||||||
@@ -49,12 +49,12 @@ namespace DiscImageChef.Checksums
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class SpamSumContext : IChecksum
|
public class SpamSumContext : IChecksum
|
||||||
{
|
{
|
||||||
const uint ROLLING_WINDOW = 7;
|
const uint ROLLING_WINDOW = 7;
|
||||||
const uint MIN_BLOCKSIZE = 3;
|
const uint MIN_BLOCKSIZE = 3;
|
||||||
const uint HASH_PRIME = 0x01000193;
|
const uint HASH_PRIME = 0x01000193;
|
||||||
const uint HASH_INIT = 0x28021967;
|
const uint HASH_INIT = 0x28021967;
|
||||||
const uint NUM_BLOCKHASHES = 31;
|
const uint NUM_BLOCKHASHES = 31;
|
||||||
const uint SPAMSUM_LENGTH = 64;
|
const uint SPAMSUM_LENGTH = 64;
|
||||||
const uint FUZZY_MAX_RESULT = 2 * SPAMSUM_LENGTH + 20;
|
const uint FUZZY_MAX_RESULT = 2 * SPAMSUM_LENGTH + 20;
|
||||||
//"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
//"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||||
readonly byte[] b64 =
|
readonly byte[] b64 =
|
||||||
@@ -67,30 +67,70 @@ namespace DiscImageChef.Checksums
|
|||||||
|
|
||||||
FuzzyState self;
|
FuzzyState self;
|
||||||
|
|
||||||
void roll_init()
|
|
||||||
{
|
|
||||||
self.Roll = new RollState {Window = new byte[ROLLING_WINDOW]};
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes the SpamSum structures
|
/// Initializes the SpamSum structures
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Init()
|
public SpamSumContext()
|
||||||
{
|
{
|
||||||
self = new FuzzyState {Bh = new BlockhashContext[NUM_BLOCKHASHES]};
|
self =
|
||||||
|
new FuzzyState {Bh = new BlockhashContext[NUM_BLOCKHASHES]};
|
||||||
for(int i = 0; i < NUM_BLOCKHASHES; i++) self.Bh[i].Digest = new byte[SPAMSUM_LENGTH];
|
for(int i = 0; i < NUM_BLOCKHASHES; i++) self.Bh[i].Digest = new byte[SPAMSUM_LENGTH];
|
||||||
|
|
||||||
self.Bhstart = 0;
|
self.Bhstart = 0;
|
||||||
self.Bhend = 1;
|
self.Bhend = 1;
|
||||||
self.Bh[0].H = HASH_INIT;
|
self.Bh[0].H = HASH_INIT;
|
||||||
self.Bh[0].Halfh = HASH_INIT;
|
self.Bh[0].Halfh = HASH_INIT;
|
||||||
self.Bh[0].Digest[0] = 0;
|
self.Bh[0].Digest[0] = 0;
|
||||||
self.Bh[0].Halfdigest = 0;
|
self.Bh[0].Halfdigest = 0;
|
||||||
self.Bh[0].Dlen = 0;
|
self.Bh[0].Dlen = 0;
|
||||||
self.TotalSize = 0;
|
self.TotalSize = 0;
|
||||||
roll_init();
|
roll_init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Updates the hash with data.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="data">Data buffer.</param>
|
||||||
|
/// <param name="len">Length of buffer to hash.</param>
|
||||||
|
public void Update(byte[] data, uint len)
|
||||||
|
{
|
||||||
|
self.TotalSize += len;
|
||||||
|
for(int i = 0; i < len; i++) fuzzy_engine_step(data[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Updates the hash with data.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="data">Data buffer.</param>
|
||||||
|
public void Update(byte[] data)
|
||||||
|
{
|
||||||
|
Update(data, (uint)data.Length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a byte array of the hash value.
|
||||||
|
/// </summary>
|
||||||
|
public byte[] Final()
|
||||||
|
{
|
||||||
|
// SpamSum does not have a binary representation, or so it seems
|
||||||
|
throw new NotImplementedException("SpamSum does not have a binary representation.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a base64 representation of the hash value.
|
||||||
|
/// </summary>
|
||||||
|
public string End()
|
||||||
|
{
|
||||||
|
FuzzyDigest(out byte[] result);
|
||||||
|
|
||||||
|
return CToString(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
void roll_init()
|
||||||
|
{
|
||||||
|
self.Roll = new RollState {Window = new byte[ROLLING_WINDOW]};
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* a rolling hash, based on the Adler checksum. By using a rolling hash
|
* a rolling hash, based on the Adler checksum. By using a rolling hash
|
||||||
* we can perform auto resynchronisation after inserts/deletes
|
* we can perform auto resynchronisation after inserts/deletes
|
||||||
@@ -116,7 +156,7 @@ namespace DiscImageChef.Checksums
|
|||||||
* in theory should have no effect. This AND has been removed
|
* in theory should have no effect. This AND has been removed
|
||||||
* for performance (jk) */
|
* for performance (jk) */
|
||||||
self.Roll.H3 <<= 5;
|
self.Roll.H3 <<= 5;
|
||||||
self.Roll.H3 ^= c;
|
self.Roll.H3 ^= c;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint roll_sum()
|
uint roll_sum()
|
||||||
@@ -144,13 +184,13 @@ namespace DiscImageChef.Checksums
|
|||||||
if(self.Bhend == 0) // assert
|
if(self.Bhend == 0) // assert
|
||||||
throw new Exception("Assertion failed");
|
throw new Exception("Assertion failed");
|
||||||
|
|
||||||
obh = self.Bhend - 1;
|
obh = self.Bhend - 1;
|
||||||
nbh = self.Bhend;
|
nbh = self.Bhend;
|
||||||
self.Bh[nbh].H = self.Bh[obh].H;
|
self.Bh[nbh].H = self.Bh[obh].H;
|
||||||
self.Bh[nbh].Halfh = self.Bh[obh].Halfh;
|
self.Bh[nbh].Halfh = self.Bh[obh].Halfh;
|
||||||
self.Bh[nbh].Digest[0] = 0;
|
self.Bh[nbh].Digest[0] = 0;
|
||||||
self.Bh[nbh].Halfdigest = 0;
|
self.Bh[nbh].Halfdigest = 0;
|
||||||
self.Bh[nbh].Dlen = 0;
|
self.Bh[nbh].Dlen = 0;
|
||||||
++self.Bhend;
|
++self.Bhend;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,6 +205,7 @@ namespace DiscImageChef.Checksums
|
|||||||
* blocksize. */ return;
|
* blocksize. */ return;
|
||||||
if(self.Bh[self.Bhstart + 1].Dlen < SPAMSUM_LENGTH / 2)
|
if(self.Bh[self.Bhstart + 1].Dlen < SPAMSUM_LENGTH / 2)
|
||||||
/* Estimate adjustment would select this blocksize. */ return;
|
/* Estimate adjustment would select this blocksize. */ return;
|
||||||
|
|
||||||
/* At this point we are clearly no longer interested in the
|
/* At this point we are clearly no longer interested in the
|
||||||
* start_blocksize. Get rid of it. */
|
* start_blocksize. Get rid of it. */
|
||||||
++self.Bhstart;
|
++self.Bhstart;
|
||||||
@@ -173,7 +214,7 @@ namespace DiscImageChef.Checksums
|
|||||||
void fuzzy_engine_step(byte c)
|
void fuzzy_engine_step(byte c)
|
||||||
{
|
{
|
||||||
ulong h;
|
ulong h;
|
||||||
uint i;
|
uint i;
|
||||||
/* At each character we update the rolling hash and the normal hashes.
|
/* At each character we update the rolling hash and the normal hashes.
|
||||||
* When the rolling hash hits a reset value then we emit a normal hash
|
* When the rolling hash hits a reset value then we emit a normal hash
|
||||||
* as a element of the signature and reset the normal hash. */
|
* as a element of the signature and reset the normal hash. */
|
||||||
@@ -182,7 +223,7 @@ namespace DiscImageChef.Checksums
|
|||||||
|
|
||||||
for(i = self.Bhstart; i < self.Bhend; ++i)
|
for(i = self.Bhstart; i < self.Bhend; ++i)
|
||||||
{
|
{
|
||||||
self.Bh[i].H = sum_hash(c, self.Bh[i].H);
|
self.Bh[i].H = sum_hash(c, self.Bh[i].H);
|
||||||
self.Bh[i].Halfh = sum_hash(c, self.Bh[i].Halfh);
|
self.Bh[i].Halfh = sum_hash(c, self.Bh[i].Halfh);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,12 +234,13 @@ namespace DiscImageChef.Checksums
|
|||||||
/* Once this condition is false for one bs, it is
|
/* Once this condition is false for one bs, it is
|
||||||
* automatically false for all further bs. I.e. if
|
* automatically false for all further bs. I.e. if
|
||||||
* h === -1 (mod 2*bs) then h === -1 (mod bs). */ break;
|
* h === -1 (mod 2*bs) then h === -1 (mod bs). */ break;
|
||||||
|
|
||||||
/* We have hit a reset point. We now emit hashes which are
|
/* We have hit a reset point. We now emit hashes which are
|
||||||
* based on all characters in the piece of the message between
|
* based on all characters in the piece of the message between
|
||||||
* the last reset point and this one */
|
* the last reset point and this one */
|
||||||
if(0 == self.Bh[i].Dlen) fuzzy_try_fork_blockhash();
|
if(0 == self.Bh[i].Dlen) fuzzy_try_fork_blockhash();
|
||||||
self.Bh[i].Digest[self.Bh[i].Dlen] = b64[self.Bh[i].H % 64];
|
self.Bh[i].Digest[self.Bh[i].Dlen] = b64[self.Bh[i].H % 64];
|
||||||
self.Bh[i].Halfdigest = b64[self.Bh[i].Halfh % 64];
|
self.Bh[i].Halfdigest = b64[self.Bh[i].Halfh % 64];
|
||||||
if(self.Bh[i].Dlen < SPAMSUM_LENGTH - 1)
|
if(self.Bh[i].Dlen < SPAMSUM_LENGTH - 1)
|
||||||
{
|
{
|
||||||
/* We can have a problem with the tail overflowing. The
|
/* We can have a problem with the tail overflowing. The
|
||||||
@@ -208,45 +250,25 @@ namespace DiscImageChef.Checksums
|
|||||||
* last few pieces of the message into a single piece
|
* last few pieces of the message into a single piece
|
||||||
* */
|
* */
|
||||||
self.Bh[i].Digest[++self.Bh[i].Dlen] = 0;
|
self.Bh[i].Digest[++self.Bh[i].Dlen] = 0;
|
||||||
self.Bh[i].H = HASH_INIT;
|
self.Bh[i].H = HASH_INIT;
|
||||||
if(self.Bh[i].Dlen >= SPAMSUM_LENGTH / 2) continue;
|
if(self.Bh[i].Dlen >= SPAMSUM_LENGTH / 2) continue;
|
||||||
|
|
||||||
self.Bh[i].Halfh = HASH_INIT;
|
self.Bh[i].Halfh = HASH_INIT;
|
||||||
self.Bh[i].Halfdigest = 0;
|
self.Bh[i].Halfdigest = 0;
|
||||||
}
|
}
|
||||||
else fuzzy_try_reduce_blockhash();
|
else fuzzy_try_reduce_blockhash();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Updates the hash with data.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="data">Data buffer.</param>
|
|
||||||
/// <param name="len">Length of buffer to hash.</param>
|
|
||||||
public void Update(byte[] data, uint len)
|
|
||||||
{
|
|
||||||
self.TotalSize += len;
|
|
||||||
for(int i = 0; i < len; i++) fuzzy_engine_step(data[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Updates the hash with data.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="data">Data buffer.</param>
|
|
||||||
public void Update(byte[] data)
|
|
||||||
{
|
|
||||||
Update(data, (uint)data.Length);
|
|
||||||
}
|
|
||||||
|
|
||||||
// CLAUNIA: Flags seems to never be used in ssdeep, so I just removed it for code simplicity
|
// CLAUNIA: Flags seems to never be used in ssdeep, so I just removed it for code simplicity
|
||||||
uint FuzzyDigest(out byte[] result)
|
uint FuzzyDigest(out byte[] result)
|
||||||
{
|
{
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
uint bi = self.Bhstart;
|
uint bi = self.Bhstart;
|
||||||
uint h = roll_sum();
|
uint h = roll_sum();
|
||||||
int i, resultOff;
|
int i, resultOff;
|
||||||
int remain = (int)(FUZZY_MAX_RESULT - 1); /* Exclude terminating '\0'. */
|
int remain = (int)(FUZZY_MAX_RESULT - 1); /* Exclude terminating '\0'. */
|
||||||
result = new byte[FUZZY_MAX_RESULT];
|
result = new byte[FUZZY_MAX_RESULT];
|
||||||
/* Verify that our elimination was not overeager. */
|
/* Verify that our elimination was not overeager. */
|
||||||
if(!(bi == 0 || (ulong)SSDEEP_BS(bi) / 2 * SPAMSUM_LENGTH < self.TotalSize))
|
if(!(bi == 0 || (ulong)SSDEEP_BS(bi) / 2 * SPAMSUM_LENGTH < self.TotalSize))
|
||||||
throw new Exception("Assertion failed");
|
throw new Exception("Assertion failed");
|
||||||
@@ -259,6 +281,7 @@ namespace DiscImageChef.Checksums
|
|||||||
++bi;
|
++bi;
|
||||||
if(bi >= NUM_BLOCKHASHES) throw new OverflowException("The input exceeds data types.");
|
if(bi >= NUM_BLOCKHASHES) throw new OverflowException("The input exceeds data types.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Adapt blocksize guess to actual digest length. */
|
/* Adapt blocksize guess to actual digest length. */
|
||||||
while(bi >= self.Bhend) --bi;
|
while(bi >= self.Bhend) --bi;
|
||||||
while(bi > self.Bhstart && self.Bh[bi].Dlen < SPAMSUM_LENGTH / 2) --bi;
|
while(bi > self.Bhstart && self.Bh[bi].Dlen < SPAMSUM_LENGTH / 2) --bi;
|
||||||
@@ -282,14 +305,15 @@ namespace DiscImageChef.Checksums
|
|||||||
|
|
||||||
Array.Copy(self.Bh[bi].Digest, 0, result, resultOff, i);
|
Array.Copy(self.Bh[bi].Digest, 0, result, resultOff, i);
|
||||||
resultOff += i;
|
resultOff += i;
|
||||||
remain -= i;
|
remain -= i;
|
||||||
if(h != 0)
|
if(h != 0)
|
||||||
{
|
{
|
||||||
if(remain <= 0) throw new Exception("Assertion failed");
|
if(remain <= 0) throw new Exception("Assertion failed");
|
||||||
|
|
||||||
result[resultOff] = b64[self.Bh[bi].H % 64];
|
result[resultOff] = b64[self.Bh[bi].H % 64];
|
||||||
if(i < 3 || result[resultOff] != result[resultOff - 1] || result[resultOff] != result[resultOff - 2] ||
|
if(i < 3 || result[resultOff] != result[resultOff - 1] ||
|
||||||
result[resultOff] != result[resultOff - 3])
|
result[resultOff] != result[resultOff - 2] ||
|
||||||
|
result[resultOff] != result[resultOff - 3])
|
||||||
{
|
{
|
||||||
++resultOff;
|
++resultOff;
|
||||||
--remain;
|
--remain;
|
||||||
@@ -300,8 +324,9 @@ namespace DiscImageChef.Checksums
|
|||||||
if(remain <= 0) throw new Exception("Assertion failed");
|
if(remain <= 0) throw new Exception("Assertion failed");
|
||||||
|
|
||||||
result[resultOff] = self.Bh[bi].Digest[i];
|
result[resultOff] = self.Bh[bi].Digest[i];
|
||||||
if(i < 3 || result[resultOff] != result[resultOff - 1] || result[resultOff] != result[resultOff - 2] ||
|
if(i < 3 || result[resultOff] != result[resultOff - 1] ||
|
||||||
result[resultOff] != result[resultOff - 3])
|
result[resultOff] != result[resultOff - 2] ||
|
||||||
|
result[resultOff] != result[resultOff - 3])
|
||||||
{
|
{
|
||||||
++resultOff;
|
++resultOff;
|
||||||
--remain;
|
--remain;
|
||||||
@@ -320,16 +345,17 @@ namespace DiscImageChef.Checksums
|
|||||||
|
|
||||||
Array.Copy(self.Bh[bi].Digest, 0, result, resultOff, i);
|
Array.Copy(self.Bh[bi].Digest, 0, result, resultOff, i);
|
||||||
resultOff += i;
|
resultOff += i;
|
||||||
remain -= i;
|
remain -= i;
|
||||||
|
|
||||||
if(h != 0)
|
if(h != 0)
|
||||||
{
|
{
|
||||||
if(remain <= 0) throw new Exception("Assertion failed");
|
if(remain <= 0) throw new Exception("Assertion failed");
|
||||||
|
|
||||||
h = self.Bh[bi].Halfh;
|
h = self.Bh[bi].Halfh;
|
||||||
result[resultOff] = b64[h % 64];
|
result[resultOff] = b64[h % 64];
|
||||||
if(i < 3 || result[resultOff] != result[resultOff - 1] ||
|
if(i < 3 || result[resultOff] != result[resultOff - 1] ||
|
||||||
result[resultOff] != result[resultOff - 2] || result[resultOff] != result[resultOff - 3])
|
result[resultOff] != result[resultOff - 2] ||
|
||||||
|
result[resultOff] != result[resultOff - 3])
|
||||||
{
|
{
|
||||||
++resultOff;
|
++resultOff;
|
||||||
--remain;
|
--remain;
|
||||||
@@ -343,8 +369,9 @@ namespace DiscImageChef.Checksums
|
|||||||
if(remain <= 0) throw new Exception("Assertion failed");
|
if(remain <= 0) throw new Exception("Assertion failed");
|
||||||
|
|
||||||
result[resultOff] = (byte)i;
|
result[resultOff] = (byte)i;
|
||||||
if(i < 3 || result[resultOff] != result[resultOff - 1] ||
|
if(i < 3 || result[resultOff] != result[resultOff - 1] ||
|
||||||
result[resultOff] != result[resultOff - 2] || result[resultOff] != result[resultOff - 3])
|
result[resultOff] != result[resultOff - 2] ||
|
||||||
|
result[resultOff] != result[resultOff - 3])
|
||||||
{
|
{
|
||||||
++resultOff;
|
++resultOff;
|
||||||
--remain;
|
--remain;
|
||||||
@@ -355,7 +382,7 @@ namespace DiscImageChef.Checksums
|
|||||||
else if(h != 0)
|
else if(h != 0)
|
||||||
{
|
{
|
||||||
if(self.Bh[bi].Dlen != 0) throw new Exception("Assertion failed");
|
if(self.Bh[bi].Dlen != 0) throw new Exception("Assertion failed");
|
||||||
if(remain <= 0) throw new Exception("Assertion failed");
|
if(remain <= 0) throw new Exception("Assertion failed");
|
||||||
|
|
||||||
result[resultOff++] = b64[self.Bh[bi].H % 64];
|
result[resultOff++] = b64[self.Bh[bi].H % 64];
|
||||||
/* No need to bother with FUZZY_FLAG_ELIMSEQ, because this
|
/* No need to bother with FUZZY_FLAG_ELIMSEQ, because this
|
||||||
@@ -367,25 +394,6 @@ namespace DiscImageChef.Checksums
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a byte array of the hash value.
|
|
||||||
/// </summary>
|
|
||||||
public byte[] Final()
|
|
||||||
{
|
|
||||||
// SpamSum does not have a binary representation, or so it seems
|
|
||||||
throw new NotImplementedException("SpamSum does not have a binary representation.");
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns a base64 representation of the hash value.
|
|
||||||
/// </summary>
|
|
||||||
public string End()
|
|
||||||
{
|
|
||||||
FuzzyDigest(out byte[] result);
|
|
||||||
|
|
||||||
return CToString(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the hash of a file
|
/// Gets the hash of a file
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -417,7 +425,6 @@ namespace DiscImageChef.Checksums
|
|||||||
public static string Data(byte[] data, uint len, out byte[] hash)
|
public static string Data(byte[] data, uint len, out byte[] hash)
|
||||||
{
|
{
|
||||||
SpamSumContext fuzzyContext = new SpamSumContext();
|
SpamSumContext fuzzyContext = new SpamSumContext();
|
||||||
fuzzyContext.Init();
|
|
||||||
|
|
||||||
fuzzyContext.Update(data, len);
|
fuzzyContext.Update(data, len);
|
||||||
|
|
||||||
@@ -469,8 +476,8 @@ namespace DiscImageChef.Checksums
|
|||||||
* output hash to stay compatible with ssdeep output. */
|
* output hash to stay compatible with ssdeep output. */
|
||||||
struct BlockhashContext
|
struct BlockhashContext
|
||||||
{
|
{
|
||||||
public uint H;
|
public uint H;
|
||||||
public uint Halfh;
|
public uint Halfh;
|
||||||
public byte[] Digest;
|
public byte[] Digest;
|
||||||
// SPAMSUM_LENGTH
|
// SPAMSUM_LENGTH
|
||||||
public byte Halfdigest;
|
public byte Halfdigest;
|
||||||
@@ -479,11 +486,11 @@ namespace DiscImageChef.Checksums
|
|||||||
|
|
||||||
struct FuzzyState
|
struct FuzzyState
|
||||||
{
|
{
|
||||||
public uint Bhstart;
|
public uint Bhstart;
|
||||||
public uint Bhend;
|
public uint Bhend;
|
||||||
public BlockhashContext[] Bh;
|
public BlockhashContext[] Bh;
|
||||||
//NUM_BLOCKHASHES
|
//NUM_BLOCKHASHES
|
||||||
public ulong TotalSize;
|
public ulong TotalSize;
|
||||||
public RollState Roll;
|
public RollState Roll;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user