From cc56b730b3833b92e9b41d8c784f648b7bf25829 Mon Sep 17 00:00:00 2001 From: de4dot Date: Sat, 23 Nov 2013 21:59:45 +0100 Subject: [PATCH] Add CRC32 instance methods --- de4dot.code/deobfuscators/CRC32.cs | 127 ++++++++++++++++++++++++++++- 1 file changed, 126 insertions(+), 1 deletion(-) diff --git a/de4dot.code/deobfuscators/CRC32.cs b/de4dot.code/deobfuscators/CRC32.cs index 62f4050d..f9e706fd 100644 --- a/de4dot.code/deobfuscators/CRC32.cs +++ b/de4dot.code/deobfuscators/CRC32.cs @@ -18,7 +18,7 @@ */ namespace de4dot.code.deobfuscators { - class CRC32 { + struct CRC32 { static readonly uint[] table = new uint[256] { 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, @@ -86,6 +86,131 @@ namespace de4dot.code.deobfuscators { 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D, }; + uint checkSum; + + public void Initialize() { + this.checkSum = uint.MaxValue; + } + + public void Hash(byte[] data) { + if (data == null) + return; + foreach (var b in data) { + int i = (byte)(checkSum ^ b); + checkSum = (checkSum >> 8) ^ table[i]; + } + } + + public void Hash(sbyte a) { + int i = (byte)(checkSum ^ a); + checkSum = (checkSum >> 8) ^ table[i]; + } + + public void Hash(byte a) { + int i = (byte)(checkSum ^ a); + checkSum = (checkSum >> 8) ^ table[i]; + } + + public void Hash(short a) { + int i = (byte)(checkSum ^ (byte)a); + checkSum = (checkSum >> 8) ^ table[i]; + + i = (byte)(checkSum ^ (byte)(a >> 8)); + checkSum = (checkSum >> 8) ^ table[i]; + } + + public void Hash(ushort a) { + int i = (byte)(checkSum ^ (byte)a); + checkSum = (checkSum >> 8) ^ table[i]; + + i = (byte)(checkSum ^ (byte)(a >> 8)); + checkSum = (checkSum >> 8) ^ table[i]; + } + + public void Hash(int a) { + int i = (byte)(checkSum ^ (byte)a); + checkSum = (checkSum >> 8) ^ table[i]; + + i = (byte)(checkSum ^ (byte)(a >> 8)); + checkSum = (checkSum >> 8) ^ table[i]; + + i = (byte)(checkSum ^ (byte)(a >> 16)); + checkSum = (checkSum >> 8) ^ table[i]; + + i = (byte)(checkSum ^ (byte)(a >> 24)); + checkSum = (checkSum >> 8) ^ table[i]; + } + + public void Hash(uint a) { + int i = (byte)(checkSum ^ (byte)a); + checkSum = (checkSum >> 8) ^ table[i]; + + i = (byte)(checkSum ^ (byte)(a >> 8)); + checkSum = (checkSum >> 8) ^ table[i]; + + i = (byte)(checkSum ^ (byte)(a >> 16)); + checkSum = (checkSum >> 8) ^ table[i]; + + i = (byte)(checkSum ^ (byte)(a >> 24)); + checkSum = (checkSum >> 8) ^ table[i]; + } + + public void Hash(long a) { + int i = (byte)(checkSum ^ (byte)a); + checkSum = (checkSum >> 8) ^ table[i]; + + i = (byte)(checkSum ^ (byte)(a >> 8)); + checkSum = (checkSum >> 8) ^ table[i]; + + i = (byte)(checkSum ^ (byte)(a >> 16)); + checkSum = (checkSum >> 8) ^ table[i]; + + i = (byte)(checkSum ^ (byte)(a >> 24)); + checkSum = (checkSum >> 8) ^ table[i]; + + i = (byte)(checkSum ^ (byte)(a >> 32)); + checkSum = (checkSum >> 8) ^ table[i]; + + i = (byte)(checkSum ^ (byte)(a >> 40)); + checkSum = (checkSum >> 8) ^ table[i]; + + i = (byte)(checkSum ^ (byte)(a >> 48)); + checkSum = (checkSum >> 8) ^ table[i]; + + i = (byte)(checkSum ^ (byte)(a >> 56)); + checkSum = (checkSum >> 8) ^ table[i]; + } + + public void Hash(ulong a) { + int i = (byte)(checkSum ^ (byte)a); + checkSum = (checkSum >> 8) ^ table[i]; + + i = (byte)(checkSum ^ (byte)(a >> 8)); + checkSum = (checkSum >> 8) ^ table[i]; + + i = (byte)(checkSum ^ (byte)(a >> 16)); + checkSum = (checkSum >> 8) ^ table[i]; + + i = (byte)(checkSum ^ (byte)(a >> 24)); + checkSum = (checkSum >> 8) ^ table[i]; + + i = (byte)(checkSum ^ (byte)(a >> 32)); + checkSum = (checkSum >> 8) ^ table[i]; + + i = (byte)(checkSum ^ (byte)(a >> 40)); + checkSum = (checkSum >> 8) ^ table[i]; + + i = (byte)(checkSum ^ (byte)(a >> 48)); + checkSum = (checkSum >> 8) ^ table[i]; + + i = (byte)(checkSum ^ (byte)(a >> 56)); + checkSum = (checkSum >> 8) ^ table[i]; + } + + public uint GetHash() { + return ~checkSum; + } + public static uint CheckSum(byte[] data) { if (data == null) return 0;