diff --git a/Adler32Context.cs b/Adler32Context.cs index 083ae31..a950b84 100644 --- a/Adler32Context.cs +++ b/Adler32Context.cs @@ -42,12 +42,12 @@ namespace DiscImageChef.Checksums public class Adler32Context : IChecksum { const ushort ADLER_MODULE = 65521; - ushort sum1, sum2; + ushort sum1, sum2; /// /// Initializes the Adler-32 sums /// - public void Init() + public Adler32Context() { sum1 = 1; sum2 = 0; @@ -63,7 +63,7 @@ namespace DiscImageChef.Checksums for(int i = 0; i < len; i++) { 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 /// public byte[] Final() { - uint finalSum = (uint)((sum2 << 16) | sum1); + uint finalSum = (uint)((sum2 << 16) | sum1); BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian; return BigEndianBitConverter.GetBytes(finalSum); } @@ -91,7 +91,7 @@ namespace DiscImageChef.Checksums /// public string End() { - uint finalSum = (uint)((sum2 << 16) | sum1); + uint finalSum = (uint)((sum2 << 16) | sum1); StringBuilder adlerOutput = new StringBuilder(); BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian; @@ -119,8 +119,8 @@ namespace DiscImageChef.Checksums public static string File(string filename, out byte[] hash) { FileStream fileStream = new FileStream(filename, FileMode.Open); - ushort localSum1, localSum2; - uint finalSum; + ushort localSum1, localSum2; + uint finalSum; localSum1 = 1; localSum2 = 0; @@ -128,13 +128,13 @@ namespace DiscImageChef.Checksums for(int i = 0; i < fileStream.Length; i++) { localSum1 = (ushort)((localSum1 + fileStream.ReadByte()) % ADLER_MODULE); - localSum2 = (ushort)((localSum2 + localSum1) % ADLER_MODULE); + localSum2 = (ushort)((localSum2 + localSum1) % ADLER_MODULE); } finalSum = (uint)((localSum2 << 16) | localSum1); BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian; - hash = BigEndianBitConverter.GetBytes(finalSum); + hash = BigEndianBitConverter.GetBytes(finalSum); StringBuilder adlerOutput = new StringBuilder(); @@ -154,21 +154,21 @@ namespace DiscImageChef.Checksums public static string Data(byte[] data, uint len, out byte[] hash) { ushort localSum1, localSum2; - uint finalSum; + uint finalSum; localSum1 = 1; localSum2 = 0; 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); } finalSum = (uint)((localSum2 << 16) | localSum1); BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian; - hash = BigEndianBitConverter.GetBytes(finalSum); + hash = BigEndianBitConverter.GetBytes(finalSum); StringBuilder adlerOutput = new StringBuilder(); diff --git a/CRC16Context.cs b/CRC16Context.cs index ec65593..f779a37 100644 --- a/CRC16Context.cs +++ b/CRC16Context.cs @@ -43,14 +43,14 @@ namespace DiscImageChef.Checksums { const ushort CRC16_POLY = 0xA001; const ushort CRC16_SEED = 0x0000; - ushort hashInt; - ushort[] table; + readonly ushort[] table; + ushort hashInt; /// /// Initializes the CRC16 table and seed /// - public void Init() + public Crc16Context() { hashInt = CRC16_SEED; @@ -58,9 +58,11 @@ namespace DiscImageChef.Checksums for(int i = 0; i < 256; i++) { ushort entry = (ushort)i; - for(int j = 0; j < 8; j++) - if((entry & 1) == 1) entry = (ushort)((entry >> 1) ^ CRC16_POLY); - else entry = (ushort)(entry >> 1); + for(int j = 0; j < 8; j++) + if((entry & 1) == 1) + entry = (ushort)((entry >> 1) ^ CRC16_POLY); + else + entry = (ushort)(entry >> 1); table[i] = entry; } @@ -102,7 +104,7 @@ namespace DiscImageChef.Checksums StringBuilder crc16Output = new StringBuilder(); 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")); return crc16Output.ToString(); @@ -126,7 +128,7 @@ namespace DiscImageChef.Checksums public static string File(string filename, out byte[] hash) { FileStream fileStream = new FileStream(filename, FileMode.Open); - ushort localhashInt; + ushort localhashInt; localhashInt = CRC16_SEED; @@ -134,9 +136,11 @@ namespace DiscImageChef.Checksums for(int i = 0; i < 256; i++) { ushort entry = (ushort)i; - for(int j = 0; j < 8; j++) - if((entry & 1) == 1) entry = (ushort)((entry >> 1) ^ CRC16_POLY); - else entry = (ushort)(entry >> 1); + for(int j = 0; j < 8; j++) + if((entry & 1) == 1) + entry = (ushort)((entry >> 1) ^ CRC16_POLY); + else + entry = (ushort)(entry >> 1); localTable[i] = entry; } @@ -146,7 +150,7 @@ namespace DiscImageChef.Checksums (ushort)((localhashInt >> 8) ^ localTable[fileStream.ReadByte() ^ (localhashInt & 0xff)]); BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian; - hash = BigEndianBitConverter.GetBytes(localhashInt); + hash = BigEndianBitConverter.GetBytes(localhashInt); StringBuilder crc16Output = new StringBuilder(); @@ -186,9 +190,11 @@ namespace DiscImageChef.Checksums for(int i = 0; i < 256; i++) { ushort entry = (ushort)i; - for(int j = 0; j < 8; j++) - if((entry & 1) == 1) entry = (ushort)((entry >> 1) ^ polynomial); - else entry = (ushort)(entry >> 1); + for(int j = 0; j < 8; j++) + if((entry & 1) == 1) + entry = (ushort)((entry >> 1) ^ polynomial); + else + entry = (ushort)(entry >> 1); localTable[i] = entry; } @@ -197,7 +203,7 @@ namespace DiscImageChef.Checksums localhashInt = (ushort)((localhashInt >> 8) ^ localTable[data[i] ^ (localhashInt & 0xff)]); BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian; - hash = BigEndianBitConverter.GetBytes(localhashInt); + hash = BigEndianBitConverter.GetBytes(localhashInt); StringBuilder crc16Output = new StringBuilder(); diff --git a/CRC32Context.cs b/CRC32Context.cs index e231996..03e7ace 100644 --- a/CRC32Context.cs +++ b/CRC32Context.cs @@ -43,14 +43,14 @@ namespace DiscImageChef.Checksums { const uint CRC32_POLY = 0xEDB88320; const uint CRC32_SEED = 0xFFFFFFFF; - uint hashInt; - uint[] table; + readonly uint[] table; + uint hashInt; /// /// Initializes the CRC32 table and seed /// - public void Init() + public Crc32Context() { hashInt = CRC32_SEED; @@ -58,9 +58,11 @@ namespace DiscImageChef.Checksums 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) ^ CRC32_POLY; - else entry = entry >> 1; + for(int j = 0; j < 8; j++) + if((entry & 1) == 1) + entry = (entry >> 1) ^ CRC32_POLY; + else + entry = entry >> 1; table[i] = entry; } @@ -102,7 +104,7 @@ namespace DiscImageChef.Checksums StringBuilder crc32Output = new StringBuilder(); 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")); return crc32Output.ToString(); @@ -126,7 +128,7 @@ namespace DiscImageChef.Checksums public static string File(string filename, out byte[] hash) { FileStream fileStream = new FileStream(filename, FileMode.Open); - uint localhashInt; + uint localhashInt; localhashInt = CRC32_SEED; @@ -134,25 +136,27 @@ namespace DiscImageChef.Checksums 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) ^ CRC32_POLY; - else entry = entry >> 1; + for(int j = 0; j < 8; j++) + if((entry & 1) == 1) + entry = (entry >> 1) ^ CRC32_POLY; + else + entry = entry >> 1; localTable[i] = entry; } for(int i = 0; i < fileStream.Length; i++) { - localhashInt = (localhashInt >> 8) ^ localTable[fileStream.ReadByte() ^ (localhashInt & 0xff)]; - if((localhashInt ^ CRC32_SEED) == 0xB883C628 || (localhashInt ^CRC32_SEED) == 0x28C683B8) - { + localhashInt = (localhashInt >> 8) ^ + localTable[fileStream.ReadByte() ^ (localhashInt & 0xff)]; + if((localhashInt ^ CRC32_SEED) == 0xB883C628 || + (localhashInt ^ CRC32_SEED) == 0x28C683B8) System.Console.WriteLine("CRC found at position {0}", fileStream.Position); - } } - localhashInt ^= CRC32_SEED; - BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian; - hash = BigEndianBitConverter.GetBytes(localhashInt); + localhashInt ^= CRC32_SEED; + BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian; + hash = BigEndianBitConverter.GetBytes(localhashInt); StringBuilder crc32Output = new StringBuilder(); @@ -192,9 +196,11 @@ namespace DiscImageChef.Checksums 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; + for(int j = 0; j < 8; j++) + if((entry & 1) == 1) + entry = (entry >> 1) ^ polynomial; + else + entry = entry >> 1; localTable[i] = entry; } @@ -202,9 +208,9 @@ namespace DiscImageChef.Checksums for(int i = 0; i < len; i++) localhashInt = (localhashInt >> 8) ^ localTable[data[i] ^ (localhashInt & 0xff)]; - localhashInt ^= CRC32_SEED; - BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian; - hash = BigEndianBitConverter.GetBytes(localhashInt); + localhashInt ^= CRC32_SEED; + BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian; + hash = BigEndianBitConverter.GetBytes(localhashInt); StringBuilder crc32Output = new StringBuilder(); diff --git a/CRC64Context.cs b/CRC64Context.cs index b1bc62b..9213628 100644 --- a/CRC64Context.cs +++ b/CRC64Context.cs @@ -43,14 +43,14 @@ namespace DiscImageChef.Checksums { const ulong CRC64_POLY = 0xC96C5795D7870F42; const ulong CRC64_SEED = 0xFFFFFFFFFFFFFFFF; - ulong hashInt; - ulong[] table; + readonly ulong[] table; + ulong hashInt; /// /// Initializes the CRC64 table and seed /// - public void Init() + public Crc64Context() { hashInt = CRC64_SEED; @@ -58,9 +58,11 @@ namespace DiscImageChef.Checksums for(int i = 0; i < 256; i++) { ulong entry = (ulong)i; - for(int j = 0; j < 8; j++) - if((entry & 1) == 1) entry = (entry >> 1) ^ CRC64_POLY; - else entry = entry >> 1; + for(int j = 0; j < 8; j++) + if((entry & 1) == 1) + entry = (entry >> 1) ^ CRC64_POLY; + else + entry = entry >> 1; table[i] = entry; } @@ -126,7 +128,7 @@ namespace DiscImageChef.Checksums public static string File(string filename, out byte[] hash) { FileStream fileStream = new FileStream(filename, FileMode.Open); - ulong localhashInt; + ulong localhashInt; localhashInt = CRC64_SEED; @@ -134,9 +136,11 @@ namespace DiscImageChef.Checksums for(int i = 0; i < 256; i++) { ulong entry = (ulong)i; - for(int j = 0; j < 8; j++) - if((entry & 1) == 1) entry = (entry >> 1) ^ CRC64_POLY; - else entry = entry >> 1; + for(int j = 0; j < 8; j++) + if((entry & 1) == 1) + entry = (entry >> 1) ^ CRC64_POLY; + else + entry = entry >> 1; localTable[i] = entry; } @@ -144,9 +148,9 @@ namespace DiscImageChef.Checksums for(int i = 0; i < fileStream.Length; i++) localhashInt = (localhashInt >> 8) ^ localTable[(ulong)fileStream.ReadByte() ^ (localhashInt & 0xffL)]; - localhashInt ^= CRC64_SEED; - BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian; - hash = BigEndianBitConverter.GetBytes(localhashInt); + localhashInt ^= CRC64_SEED; + BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian; + hash = BigEndianBitConverter.GetBytes(localhashInt); StringBuilder crc64Output = new StringBuilder(); @@ -186,9 +190,11 @@ namespace DiscImageChef.Checksums for(int i = 0; i < 256; i++) { ulong entry = (ulong)i; - for(int j = 0; j < 8; j++) - if((entry & 1) == 1) entry = (entry >> 1) ^ polynomial; - else entry = entry >> 1; + for(int j = 0; j < 8; j++) + if((entry & 1) == 1) + entry = (entry >> 1) ^ polynomial; + else + entry = entry >> 1; localTable[i] = entry; } @@ -196,9 +202,9 @@ namespace DiscImageChef.Checksums for(int i = 0; i < len; i++) localhashInt = (localhashInt >> 8) ^ localTable[data[i] ^ (localhashInt & 0xff)]; - localhashInt ^= CRC64_SEED; - BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian; - hash = BigEndianBitConverter.GetBytes(localhashInt); + localhashInt ^= CRC64_SEED; + BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian; + hash = BigEndianBitConverter.GetBytes(localhashInt); StringBuilder crc64Output = new StringBuilder(); diff --git a/FletcherContext.cs b/FletcherContext.cs index a79c739..f8eb3f7 100644 --- a/FletcherContext.cs +++ b/FletcherContext.cs @@ -40,19 +40,19 @@ namespace DiscImageChef.Checksums { public class Fletcher32Context : IChecksum { - bool inodd; - byte oddValue; + bool inodd; + byte oddValue; ushort sum1, sum2; /// /// Initializes the Fletcher32 sums /// - public void Init() + public Fletcher32Context() { - sum1 = 0xFFFF; - sum2 = 0xFFFF; + sum1 = 0xFFFF; + sum2 = 0xFFFF; oddValue = 0; - inodd = false; + inodd = false; } /// @@ -67,13 +67,13 @@ namespace DiscImageChef.Checksums if(len % 2 != 0) { oddValue = data[len - 1]; - inodd = true; + inodd = true; for(int i = 0; i < len - 1; i += 2) { block = BigEndianBitConverter.ToUInt16(data, i); - sum1 = (ushort)((sum1 + block) % 0xFFFF); - sum2 = (ushort)((sum2 + sum1) % 0xFFFF); + sum1 = (ushort)((sum1 + block) % 0xFFFF); + sum2 = (ushort)((sum2 + sum1) % 0xFFFF); } } else @@ -82,32 +82,32 @@ namespace DiscImageChef.Checksums for(int i = 0; i < len; i += 2) { block = BigEndianBitConverter.ToUInt16(data, i); - sum1 = (ushort)((sum1 + block) % 0xFFFF); - sum2 = (ushort)((sum2 + sum1) % 0xFFFF); + sum1 = (ushort)((sum1 + block) % 0xFFFF); + sum2 = (ushort)((sum2 + sum1) % 0xFFFF); } } // Carrying odd else { byte[] oddData = new byte[2]; - oddData[0] = oddValue; - oddData[1] = data[0]; + oddData[0] = oddValue; + oddData[1] = data[0]; block = BigEndianBitConverter.ToUInt16(oddData, 0); - sum1 = (ushort)((sum1 + block) % 0xFFFF); - sum2 = (ushort)((sum2 + sum1) % 0xFFFF); + sum1 = (ushort)((sum1 + block) % 0xFFFF); + sum2 = (ushort)((sum2 + sum1) % 0xFFFF); // Even size, carrying odd if(len % 2 == 0) { oddValue = data[len - 1]; - inodd = true; + inodd = true; for(int i = 1; i < len - 1; i += 2) { block = BigEndianBitConverter.ToUInt16(data, i); - sum1 = (ushort)((sum1 + block) % 0xFFFF); - sum2 = (ushort)((sum2 + sum1) % 0xFFFF); + sum1 = (ushort)((sum1 + block) % 0xFFFF); + sum2 = (ushort)((sum2 + sum1) % 0xFFFF); } } else @@ -116,8 +116,8 @@ namespace DiscImageChef.Checksums for(int i = 1; i < len; i += 2) { block = BigEndianBitConverter.ToUInt16(data, i); - sum1 = (ushort)((sum1 + block) % 0xFFFF); - sum2 = (ushort)((sum2 + sum1) % 0xFFFF); + sum1 = (ushort)((sum1 + block) % 0xFFFF); + sum2 = (ushort)((sum2 + sum1) % 0xFFFF); } } } @@ -146,7 +146,7 @@ namespace DiscImageChef.Checksums /// public string End() { - uint finalSum = (uint)(sum1 + (sum2 << 16)); + uint finalSum = (uint)(sum1 + (sum2 << 16)); StringBuilder fletcherOutput = new StringBuilder(); 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) { FileStream fileStream = new FileStream(filename, FileMode.Open); - ushort localSum1, localSum2, block; - uint finalSum; - byte[] blockBytes; + ushort localSum1, localSum2, block; + uint finalSum; + byte[] blockBytes; localSum1 = 0xFFFF; localSum2 = 0xFFFF; 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]; fileStream.Read(blockBytes, 0, 2); - block = BigEndianBitConverter.ToUInt16(blockBytes, 0); - localSum1 = (ushort)((localSum1 + block) % 0xFFFF); + block = BigEndianBitConverter.ToUInt16(blockBytes, 0); + localSum1 = (ushort)((localSum1 + block) % 0xFFFF); localSum2 = (ushort)((localSum2 + localSum1) % 0xFFFF); } else @@ -195,17 +195,17 @@ namespace DiscImageChef.Checksums { blockBytes = new byte[2]; fileStream.Read(blockBytes, 0, 2); - block = BigEndianBitConverter.ToUInt16(blockBytes, 0); - localSum1 = (ushort)((localSum1 + block) % 0xFFFF); + block = BigEndianBitConverter.ToUInt16(blockBytes, 0); + localSum1 = (ushort)((localSum1 + block) % 0xFFFF); localSum2 = (ushort)((localSum2 + localSum1) % 0xFFFF); } byte[] oddData = new byte[2]; - oddData[0] = (byte)fileStream.ReadByte(); - oddData[1] = 0; + oddData[0] = (byte)fileStream.ReadByte(); + oddData[1] = 0; - block = BigEndianBitConverter.ToUInt16(oddData, 0); - localSum1 = (ushort)((localSum1 + block) % 0xFFFF); + block = BigEndianBitConverter.ToUInt16(oddData, 0); + localSum1 = (ushort)((localSum1 + block) % 0xFFFF); localSum2 = (ushort)((localSum2 + localSum1) % 0xFFFF); } @@ -229,33 +229,33 @@ namespace DiscImageChef.Checksums public static string Data(byte[] data, uint len, out byte[] hash) { ushort localSum1, localSum2, block; - uint finalSum; + uint finalSum; localSum1 = 0xFFFF; localSum2 = 0xFFFF; - if(len % 2 == 0) + if(len % 2 == 0) for(int i = 0; i < len; i += 2) { - block = BigEndianBitConverter.ToUInt16(data, i); - localSum1 = (ushort)((localSum1 + block) % 0xFFFF); + block = BigEndianBitConverter.ToUInt16(data, i); + localSum1 = (ushort)((localSum1 + block) % 0xFFFF); localSum2 = (ushort)((localSum2 + localSum1) % 0xFFFF); } else { for(int i = 0; i < len - 1; i += 2) { - block = BigEndianBitConverter.ToUInt16(data, i); - localSum1 = (ushort)((localSum1 + block) % 0xFFFF); + block = BigEndianBitConverter.ToUInt16(data, i); + localSum1 = (ushort)((localSum1 + block) % 0xFFFF); localSum2 = (ushort)((localSum2 + localSum1) % 0xFFFF); } byte[] oddData = new byte[2]; - oddData[0] = data[len - 1]; - oddData[1] = 0; + oddData[0] = data[len - 1]; + oddData[1] = 0; - block = BigEndianBitConverter.ToUInt16(oddData, 0); - localSum1 = (ushort)((localSum1 + block) % 0xFFFF); + block = BigEndianBitConverter.ToUInt16(oddData, 0); + localSum1 = (ushort)((localSum1 + block) % 0xFFFF); localSum2 = (ushort)((localSum2 + localSum1) % 0xFFFF); } @@ -304,7 +304,7 @@ namespace DiscImageChef.Checksums for(int i = 0; i < len; i++) { sum1 = (byte)((sum1 + data[i]) % 0xFF); - sum2 = (byte)((sum2 + sum1) % 0xFF); + sum2 = (byte)((sum2 + sum1) % 0xFF); } } @@ -331,7 +331,7 @@ namespace DiscImageChef.Checksums /// public string End() { - ushort finalSum = (ushort)(sum1 + (sum2 << 8)); + ushort finalSum = (ushort)(sum1 + (sum2 << 8)); StringBuilder fletcherOutput = new StringBuilder(); 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) { FileStream fileStream = new FileStream(filename, FileMode.Open); - byte localSum1, localSum2, block; - ushort finalSum; + byte localSum1, localSum2, block; + ushort finalSum; localSum1 = 0xFF; localSum2 = 0xFF; for(int i = 0; i < fileStream.Length; i += 2) { - block = (byte)fileStream.ReadByte(); - localSum1 = (byte)((localSum1 + block) % 0xFF); + block = (byte)fileStream.ReadByte(); + localSum1 = (byte)((localSum1 + block) % 0xFF); localSum2 = (byte)((localSum2 + localSum1) % 0xFF); } @@ -390,7 +390,7 @@ namespace DiscImageChef.Checksums /// Byte array of the hash value. public static string Data(byte[] data, uint len, out byte[] hash) { - byte localSum1, localSum2; + byte localSum1, localSum2; ushort finalSum; localSum1 = 0xFF; @@ -398,7 +398,7 @@ namespace DiscImageChef.Checksums for(int i = 0; i < len; i++) { - localSum1 = (byte)((localSum1 + data[i]) % 0xFF); + localSum1 = (byte)((localSum1 + data[i]) % 0xFF); localSum2 = (byte)((localSum2 + localSum1) % 0xFF); } diff --git a/IChecksum.cs b/IChecksum.cs index a29a96b..862bbd8 100644 --- a/IChecksum.cs +++ b/IChecksum.cs @@ -34,11 +34,6 @@ namespace DiscImageChef.Checksums { public interface IChecksum { - /// - /// Initializes the algorithm - /// - void Init(); - /// /// Updates the hash with data. /// diff --git a/MD5Context.cs b/MD5Context.cs index 6fd0dc2..90a9c5b 100644 --- a/MD5Context.cs +++ b/MD5Context.cs @@ -46,7 +46,7 @@ namespace DiscImageChef.Checksums /// /// Initializes the MD5 hash provider /// - public void Init() + public Md5Context() { md5Provider = MD5.Create(); } @@ -99,7 +99,7 @@ namespace DiscImageChef.Checksums public byte[] File(string filename) { FileStream fileStream = new FileStream(filename, FileMode.Open); - byte[] result = md5Provider.ComputeHash(fileStream); + byte[] result = md5Provider.ComputeHash(fileStream); fileStream.Close(); return result; } @@ -111,8 +111,8 @@ namespace DiscImageChef.Checksums /// Byte array of the hash value. public string File(string filename, out byte[] hash) { - FileStream fileStream = new FileStream(filename, FileMode.Open); - hash = md5Provider.ComputeHash(fileStream); + FileStream fileStream = new FileStream(filename, FileMode.Open); + hash = md5Provider.ComputeHash(fileStream); StringBuilder md5Output = new StringBuilder(); foreach(byte h in hash) md5Output.Append(h.ToString("x2")); @@ -130,7 +130,7 @@ namespace DiscImageChef.Checksums /// Byte array of the hash value. 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(); foreach(byte h in hash) md5Output.Append(h.ToString("x2")); diff --git a/RIPEMD160Context.cs b/RIPEMD160Context.cs index 120f2cb..404f9e3 100644 --- a/RIPEMD160Context.cs +++ b/RIPEMD160Context.cs @@ -46,7 +46,7 @@ namespace DiscImageChef.Checksums /// /// Initializes the RIPEMD160 hash provider /// - public void Init() + public Ripemd160Context() { ripemd160Provider = RIPEMD160.Create(); } @@ -99,7 +99,7 @@ namespace DiscImageChef.Checksums public byte[] File(string filename) { FileStream fileStream = new FileStream(filename, FileMode.Open); - byte[] result = ripemd160Provider.ComputeHash(fileStream); + byte[] result = ripemd160Provider.ComputeHash(fileStream); fileStream.Close(); return result; } @@ -111,8 +111,8 @@ namespace DiscImageChef.Checksums /// Byte array of the hash value. public string File(string filename, out byte[] hash) { - FileStream fileStream = new FileStream(filename, FileMode.Open); - hash = ripemd160Provider.ComputeHash(fileStream); + FileStream fileStream = new FileStream(filename, FileMode.Open); + hash = ripemd160Provider.ComputeHash(fileStream); StringBuilder ripemd160Output = new StringBuilder(); foreach(byte h in hash) ripemd160Output.Append(h.ToString("x2")); @@ -130,7 +130,7 @@ namespace DiscImageChef.Checksums /// Byte array of the hash value. 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(); foreach(byte h in hash) ripemd160Output.Append(h.ToString("x2")); diff --git a/SHA1Context.cs b/SHA1Context.cs index f62887a..23c5cc8 100644 --- a/SHA1Context.cs +++ b/SHA1Context.cs @@ -46,7 +46,7 @@ namespace DiscImageChef.Checksums /// /// Initializes the SHA1 hash provider /// - public void Init() + public Sha1Context() { sha1Provider = SHA1.Create(); } @@ -99,7 +99,7 @@ namespace DiscImageChef.Checksums public byte[] File(string filename) { FileStream fileStream = new FileStream(filename, FileMode.Open); - byte[] result = sha1Provider.ComputeHash(fileStream); + byte[] result = sha1Provider.ComputeHash(fileStream); fileStream.Close(); return result; } @@ -111,8 +111,8 @@ namespace DiscImageChef.Checksums /// Byte array of the hash value. public string File(string filename, out byte[] hash) { - FileStream fileStream = new FileStream(filename, FileMode.Open); - hash = sha1Provider.ComputeHash(fileStream); + FileStream fileStream = new FileStream(filename, FileMode.Open); + hash = sha1Provider.ComputeHash(fileStream); StringBuilder sha1Output = new StringBuilder(); foreach(byte h in hash) sha1Output.Append(h.ToString("x2")); @@ -130,7 +130,7 @@ namespace DiscImageChef.Checksums /// Byte array of the hash value. 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(); foreach(byte h in hash) sha1Output.Append(h.ToString("x2")); diff --git a/SHA256Context.cs b/SHA256Context.cs index 43e1073..e0056fb 100644 --- a/SHA256Context.cs +++ b/SHA256Context.cs @@ -46,7 +46,7 @@ namespace DiscImageChef.Checksums /// /// Initializes the SHA256 hash provider /// - public void Init() + public Sha256Context() { sha256Provider = SHA256.Create(); } @@ -99,7 +99,7 @@ namespace DiscImageChef.Checksums public byte[] File(string filename) { FileStream fileStream = new FileStream(filename, FileMode.Open); - byte[] result = sha256Provider.ComputeHash(fileStream); + byte[] result = sha256Provider.ComputeHash(fileStream); fileStream.Close(); return result; } @@ -111,8 +111,8 @@ namespace DiscImageChef.Checksums /// Byte array of the hash value. public string File(string filename, out byte[] hash) { - FileStream fileStream = new FileStream(filename, FileMode.Open); - hash = sha256Provider.ComputeHash(fileStream); + FileStream fileStream = new FileStream(filename, FileMode.Open); + hash = sha256Provider.ComputeHash(fileStream); StringBuilder sha256Output = new StringBuilder(); foreach(byte h in hash) sha256Output.Append(h.ToString("x2")); @@ -130,7 +130,7 @@ namespace DiscImageChef.Checksums /// Byte array of the hash value. 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(); foreach(byte h in hash) sha256Output.Append(h.ToString("x2")); diff --git a/SHA384Context.cs b/SHA384Context.cs index 1fd534c..cd66c8b 100644 --- a/SHA384Context.cs +++ b/SHA384Context.cs @@ -46,7 +46,7 @@ namespace DiscImageChef.Checksums /// /// Initializes the SHA384 hash provider /// - public void Init() + public Sha384Context() { sha384Provider = SHA384.Create(); } @@ -99,7 +99,7 @@ namespace DiscImageChef.Checksums public byte[] File(string filename) { FileStream fileStream = new FileStream(filename, FileMode.Open); - byte[] result = sha384Provider.ComputeHash(fileStream); + byte[] result = sha384Provider.ComputeHash(fileStream); fileStream.Close(); return result; } @@ -111,8 +111,8 @@ namespace DiscImageChef.Checksums /// Byte array of the hash value. public string File(string filename, out byte[] hash) { - FileStream fileStream = new FileStream(filename, FileMode.Open); - hash = sha384Provider.ComputeHash(fileStream); + FileStream fileStream = new FileStream(filename, FileMode.Open); + hash = sha384Provider.ComputeHash(fileStream); StringBuilder sha384Output = new StringBuilder(); foreach(byte h in hash) sha384Output.Append(h.ToString("x2")); @@ -130,7 +130,7 @@ namespace DiscImageChef.Checksums /// Byte array of the hash value. 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(); foreach(byte h in hash) sha384Output.Append(h.ToString("x2")); diff --git a/SHA512Context.cs b/SHA512Context.cs index 5beb4cc..d90e0ff 100644 --- a/SHA512Context.cs +++ b/SHA512Context.cs @@ -46,7 +46,7 @@ namespace DiscImageChef.Checksums /// /// Initializes the SHA512 hash provider /// - public void Init() + public Sha512Context() { sha512Provider = SHA512.Create(); } @@ -99,7 +99,7 @@ namespace DiscImageChef.Checksums public byte[] File(string filename) { FileStream fileStream = new FileStream(filename, FileMode.Open); - byte[] result = sha512Provider.ComputeHash(fileStream); + byte[] result = sha512Provider.ComputeHash(fileStream); fileStream.Close(); return result; } @@ -111,8 +111,8 @@ namespace DiscImageChef.Checksums /// Byte array of the hash value. public string File(string filename, out byte[] hash) { - FileStream fileStream = new FileStream(filename, FileMode.Open); - hash = sha512Provider.ComputeHash(fileStream); + FileStream fileStream = new FileStream(filename, FileMode.Open); + hash = sha512Provider.ComputeHash(fileStream); StringBuilder sha512Output = new StringBuilder(); foreach(byte h in hash) sha512Output.Append(h.ToString("x2")); @@ -130,7 +130,7 @@ namespace DiscImageChef.Checksums /// Byte array of the hash value. 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(); foreach(byte h in hash) sha512Output.Append(h.ToString("x2")); diff --git a/SpamSumContext.cs b/SpamSumContext.cs index 1487832..b36a137 100644 --- a/SpamSumContext.cs +++ b/SpamSumContext.cs @@ -49,12 +49,12 @@ namespace DiscImageChef.Checksums /// public class SpamSumContext : IChecksum { - const uint ROLLING_WINDOW = 7; - const uint MIN_BLOCKSIZE = 3; - const uint HASH_PRIME = 0x01000193; - const uint HASH_INIT = 0x28021967; - const uint NUM_BLOCKHASHES = 31; - const uint SPAMSUM_LENGTH = 64; + const uint ROLLING_WINDOW = 7; + const uint MIN_BLOCKSIZE = 3; + const uint HASH_PRIME = 0x01000193; + const uint HASH_INIT = 0x28021967; + const uint NUM_BLOCKHASHES = 31; + const uint SPAMSUM_LENGTH = 64; const uint FUZZY_MAX_RESULT = 2 * SPAMSUM_LENGTH + 20; //"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; readonly byte[] b64 = @@ -67,30 +67,70 @@ namespace DiscImageChef.Checksums FuzzyState self; - void roll_init() - { - self.Roll = new RollState {Window = new byte[ROLLING_WINDOW]}; - } - /// /// Initializes the SpamSum structures /// - 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]; - self.Bhstart = 0; - self.Bhend = 1; - self.Bh[0].H = HASH_INIT; - self.Bh[0].Halfh = HASH_INIT; - self.Bh[0].Digest[0] = 0; + self.Bhstart = 0; + self.Bhend = 1; + self.Bh[0].H = HASH_INIT; + self.Bh[0].Halfh = HASH_INIT; + self.Bh[0].Digest[0] = 0; self.Bh[0].Halfdigest = 0; - self.Bh[0].Dlen = 0; - self.TotalSize = 0; + self.Bh[0].Dlen = 0; + self.TotalSize = 0; roll_init(); } + /// + /// Updates the hash with data. + /// + /// Data buffer. + /// Length of buffer to hash. + public void Update(byte[] data, uint len) + { + self.TotalSize += len; + for(int i = 0; i < len; i++) fuzzy_engine_step(data[i]); + } + + /// + /// Updates the hash with data. + /// + /// Data buffer. + public void Update(byte[] data) + { + Update(data, (uint)data.Length); + } + + /// + /// Returns a byte array of the hash value. + /// + public byte[] Final() + { + // SpamSum does not have a binary representation, or so it seems + throw new NotImplementedException("SpamSum does not have a binary representation."); + } + + /// + /// Returns a base64 representation of the hash value. + /// + 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 * 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 * for performance (jk) */ self.Roll.H3 <<= 5; - self.Roll.H3 ^= c; + self.Roll.H3 ^= c; } uint roll_sum() @@ -144,13 +184,13 @@ namespace DiscImageChef.Checksums if(self.Bhend == 0) // assert throw new Exception("Assertion failed"); - obh = self.Bhend - 1; - nbh = self.Bhend; - self.Bh[nbh].H = self.Bh[obh].H; - self.Bh[nbh].Halfh = self.Bh[obh].Halfh; - self.Bh[nbh].Digest[0] = 0; + obh = self.Bhend - 1; + nbh = self.Bhend; + self.Bh[nbh].H = self.Bh[obh].H; + self.Bh[nbh].Halfh = self.Bh[obh].Halfh; + self.Bh[nbh].Digest[0] = 0; self.Bh[nbh].Halfdigest = 0; - self.Bh[nbh].Dlen = 0; + self.Bh[nbh].Dlen = 0; ++self.Bhend; } @@ -165,6 +205,7 @@ namespace DiscImageChef.Checksums * blocksize. */ return; if(self.Bh[self.Bhstart + 1].Dlen < SPAMSUM_LENGTH / 2) /* Estimate adjustment would select this blocksize. */ return; + /* At this point we are clearly no longer interested in the * start_blocksize. Get rid of it. */ ++self.Bhstart; @@ -173,7 +214,7 @@ namespace DiscImageChef.Checksums void fuzzy_engine_step(byte c) { ulong h; - uint i; + uint i; /* 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 * 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) { - 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); } @@ -193,12 +234,13 @@ namespace DiscImageChef.Checksums /* Once this condition is false for one bs, it is * automatically false for all further bs. I.e. if * h === -1 (mod 2*bs) then h === -1 (mod bs). */ break; + /* We have hit a reset point. We now emit hashes which are * based on all characters in the piece of the message between * the last reset point and this one */ 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].Halfdigest = b64[self.Bh[i].Halfh % 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]; if(self.Bh[i].Dlen < SPAMSUM_LENGTH - 1) { /* 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 * */ 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; - self.Bh[i].Halfh = HASH_INIT; + self.Bh[i].Halfh = HASH_INIT; self.Bh[i].Halfdigest = 0; } else fuzzy_try_reduce_blockhash(); } } - /// - /// Updates the hash with data. - /// - /// Data buffer. - /// Length of buffer to hash. - public void Update(byte[] data, uint len) - { - self.TotalSize += len; - for(int i = 0; i < len; i++) fuzzy_engine_step(data[i]); - } - - /// - /// Updates the hash with data. - /// - /// Data buffer. - 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 uint FuzzyDigest(out byte[] result) { StringBuilder sb = new StringBuilder(); - uint bi = self.Bhstart; - uint h = roll_sum(); - int i, resultOff; - int remain = (int)(FUZZY_MAX_RESULT - 1); /* Exclude terminating '\0'. */ - result = new byte[FUZZY_MAX_RESULT]; + uint bi = self.Bhstart; + uint h = roll_sum(); + int i, resultOff; + int remain = (int)(FUZZY_MAX_RESULT - 1); /* Exclude terminating '\0'. */ + result = new byte[FUZZY_MAX_RESULT]; /* Verify that our elimination was not overeager. */ if(!(bi == 0 || (ulong)SSDEEP_BS(bi) / 2 * SPAMSUM_LENGTH < self.TotalSize)) throw new Exception("Assertion failed"); @@ -259,6 +281,7 @@ namespace DiscImageChef.Checksums ++bi; if(bi >= NUM_BLOCKHASHES) throw new OverflowException("The input exceeds data types."); } + /* Adapt blocksize guess to actual digest length. */ while(bi >= self.Bhend) --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); resultOff += i; - remain -= i; + remain -= i; if(h != 0) { if(remain <= 0) throw new Exception("Assertion failed"); result[resultOff] = b64[self.Bh[bi].H % 64]; - if(i < 3 || result[resultOff] != result[resultOff - 1] || result[resultOff] != result[resultOff - 2] || - result[resultOff] != result[resultOff - 3]) + if(i < 3 || result[resultOff] != result[resultOff - 1] || + result[resultOff] != result[resultOff - 2] || + result[resultOff] != result[resultOff - 3]) { ++resultOff; --remain; @@ -300,8 +324,9 @@ namespace DiscImageChef.Checksums if(remain <= 0) throw new Exception("Assertion failed"); result[resultOff] = self.Bh[bi].Digest[i]; - if(i < 3 || result[resultOff] != result[resultOff - 1] || result[resultOff] != result[resultOff - 2] || - result[resultOff] != result[resultOff - 3]) + if(i < 3 || result[resultOff] != result[resultOff - 1] || + result[resultOff] != result[resultOff - 2] || + result[resultOff] != result[resultOff - 3]) { ++resultOff; --remain; @@ -320,16 +345,17 @@ namespace DiscImageChef.Checksums Array.Copy(self.Bh[bi].Digest, 0, result, resultOff, i); resultOff += i; - remain -= i; + remain -= i; if(h != 0) { if(remain <= 0) throw new Exception("Assertion failed"); - h = self.Bh[bi].Halfh; + h = self.Bh[bi].Halfh; result[resultOff] = b64[h % 64]; - if(i < 3 || result[resultOff] != result[resultOff - 1] || - result[resultOff] != result[resultOff - 2] || result[resultOff] != result[resultOff - 3]) + if(i < 3 || result[resultOff] != result[resultOff - 1] || + result[resultOff] != result[resultOff - 2] || + result[resultOff] != result[resultOff - 3]) { ++resultOff; --remain; @@ -343,8 +369,9 @@ namespace DiscImageChef.Checksums if(remain <= 0) throw new Exception("Assertion failed"); result[resultOff] = (byte)i; - if(i < 3 || result[resultOff] != result[resultOff - 1] || - result[resultOff] != result[resultOff - 2] || result[resultOff] != result[resultOff - 3]) + if(i < 3 || result[resultOff] != result[resultOff - 1] || + result[resultOff] != result[resultOff - 2] || + result[resultOff] != result[resultOff - 3]) { ++resultOff; --remain; @@ -355,7 +382,7 @@ namespace DiscImageChef.Checksums else if(h != 0) { 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]; /* No need to bother with FUZZY_FLAG_ELIMSEQ, because this @@ -367,25 +394,6 @@ namespace DiscImageChef.Checksums return 0; } - /// - /// Returns a byte array of the hash value. - /// - public byte[] Final() - { - // SpamSum does not have a binary representation, or so it seems - throw new NotImplementedException("SpamSum does not have a binary representation."); - } - - /// - /// Returns a base64 representation of the hash value. - /// - public string End() - { - FuzzyDigest(out byte[] result); - - return CToString(result); - } - /// /// Gets the hash of a file /// @@ -417,7 +425,6 @@ namespace DiscImageChef.Checksums public static string Data(byte[] data, uint len, out byte[] hash) { SpamSumContext fuzzyContext = new SpamSumContext(); - fuzzyContext.Init(); fuzzyContext.Update(data, len); @@ -469,8 +476,8 @@ namespace DiscImageChef.Checksums * output hash to stay compatible with ssdeep output. */ struct BlockhashContext { - public uint H; - public uint Halfh; + public uint H; + public uint Halfh; public byte[] Digest; // SPAMSUM_LENGTH public byte Halfdigest; @@ -479,11 +486,11 @@ namespace DiscImageChef.Checksums struct FuzzyState { - public uint Bhstart; - public uint Bhend; + public uint Bhstart; + public uint Bhend; public BlockhashContext[] Bh; //NUM_BLOCKHASHES - public ulong TotalSize; + public ulong TotalSize; public RollState Roll; } }