Prettify code for github publishing

This commit is contained in:
2015-02-03 04:15:33 +00:00
parent 4442ed1d19
commit 01f512b978
6 changed files with 91 additions and 69 deletions

View File

@@ -26,8 +26,8 @@ namespace SharpHash.Checksums
{
public class Adler32Context
{
private UInt16 sum1, sum2;
private const UInt16 AdlerModule = 65521;
UInt16 sum1, sum2;
const UInt16 AdlerModule = 65521;
/// <summary>
/// Initializes the Adler-32 sums
@@ -134,7 +134,7 @@ namespace SharpHash.Checksums
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of the data buffer to hash.</param>
/// <param name="hash">Byte array of the hash value.</param>
public string Data(byte[] data, uint len, out byte[] hash)
public static string Data(byte[] data, uint len, out byte[] hash)
{
UInt16 localSum1, localSum2;
UInt32 finalSum;
@@ -167,7 +167,7 @@ namespace SharpHash.Checksums
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="hash">Byte array of the hash value.</param>
public string Data(byte[] data, out byte[] hash)
public static string Data(byte[] data, out byte[] hash)
{
return Data(data, (uint)data.Length, out hash);
}

View File

@@ -26,9 +26,9 @@ namespace SharpHash.Checksums
{
public class Fletcher32Context
{
private UInt16 sum1, sum2;
private byte oddValue;
private bool inodd;
UInt16 sum1, sum2;
byte oddValue;
bool inodd;
/// <summary>
/// Initializes the Fletcher32 sums
@@ -175,7 +175,7 @@ namespace SharpHash.Checksums
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);
@@ -186,7 +186,7 @@ namespace SharpHash.Checksums
}
else
{
for (int i = 0; i < fileStream.Length-1; i+=2)
for (int i = 0; i < fileStream.Length - 1; i += 2)
{
blockBytes = new byte[2];
fileStream.Read(blockBytes, 0, 2);
@@ -224,7 +224,7 @@ namespace SharpHash.Checksums
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of the data buffer to hash.</param>
/// <param name="hash">Byte array of the hash value.</param>
public string Data(byte[] data, uint len, out byte[] hash)
public static string Data(byte[] data, uint len, out byte[] hash)
{
UInt16 localSum1, localSum2, block;
UInt32 finalSum;
@@ -235,7 +235,7 @@ namespace SharpHash.Checksums
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);
localSum1 = (UInt16)((localSum1 + block) % 0xFFFF);
@@ -244,7 +244,7 @@ namespace SharpHash.Checksums
}
else
{
for (int i = 0; i < len-1; i+=2)
for (int i = 0; i < len - 1; i += 2)
{
block = BigEndianBitConverter.ToUInt16(data, i);
localSum1 = (UInt16)((localSum1 + block) % 0xFFFF);
@@ -279,7 +279,7 @@ namespace SharpHash.Checksums
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="hash">Byte array of the hash value.</param>
public string Data(byte[] data, out byte[] hash)
public static string Data(byte[] data, out byte[] hash)
{
return Data(data, (uint)data.Length, out hash);
}
@@ -287,7 +287,7 @@ namespace SharpHash.Checksums
public class Fletcher16Context
{
private byte sum1, sum2;
byte sum1, sum2;
/// <summary>
/// Initializes the Fletcher16 sums
@@ -372,7 +372,7 @@ namespace SharpHash.Checksums
localSum2 = 0xFF;
block = 0;
for (int i = 0; i < fileStream.Length; i+=2)
for (int i = 0; i < fileStream.Length; i += 2)
{
block = (byte)fileStream.ReadByte();
localSum1 = (byte)((localSum1 + block) % 0xFF);
@@ -399,7 +399,7 @@ namespace SharpHash.Checksums
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of the data buffer to hash.</param>
/// <param name="hash">Byte array of the hash value.</param>
public string Data(byte[] data, uint len, out byte[] hash)
public static string Data(byte[] data, uint len, out byte[] hash)
{
byte localSum1, localSum2;
UInt16 finalSum;
@@ -432,7 +432,7 @@ namespace SharpHash.Checksums
/// </summary>
/// <param name="data">Data buffer.</param>
/// <param name="hash">Byte array of the hash value.</param>
public string Data(byte[] data, out byte[] hash)
public static string Data(byte[] data, out byte[] hash)
{
return Data(data, (uint)data.Length, out hash);
}

View File

@@ -73,7 +73,7 @@ namespace SharpHash.Checksums
/// </summary>
public string End()
{
_md5Provider.TransformFinalBlock(new byte[0], 0, 0);
_md5Provider.TransformFinalBlock(new byte[0], 0, 0);
StringBuilder md5Output = new StringBuilder();
for (int i = 0; i < _md5Provider.Hash.Length; i++)

View File

@@ -37,7 +37,7 @@ namespace SharpHash.Checksums
/// </summary>
public void Init()
{
_sha3Provider = new SHA3Unmanaged(512);;
_sha3Provider = new SHA3Unmanaged(512);
}
/// <summary>

View File

@@ -45,18 +45,21 @@ namespace SharpHash.Checksums
const UInt32 SPAMSUM_LENGTH = 64;
const UInt32 FUZZY_MAX_RESULT = (2 * SPAMSUM_LENGTH + 20);
//"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
readonly byte[] b64 = {0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E,
0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33,
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F};
readonly byte[] b64 =
{0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E,
0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33,
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F
};
struct roll_state
{
public byte[] window; // ROLLING_WINDOW
public byte[] window;
// ROLLING_WINDOW
public UInt32 h1;
public UInt32 h2;
public UInt32 h3;
@@ -72,7 +75,8 @@ namespace SharpHash.Checksums
{
public UInt32 h;
public UInt32 halfh;
public byte[] digest; // SPAMSUM_LENGTH
public byte[] digest;
// SPAMSUM_LENGTH
public byte halfdigest;
public UInt32 dlen;
}
@@ -81,7 +85,8 @@ namespace SharpHash.Checksums
{
public UInt32 bhstart;
public UInt32 bhend;
public blockhash_context[] bh; //NUM_BLOCKHASHES
public blockhash_context[] bh;
//NUM_BLOCKHASHES
public UInt64 total_size;
public roll_state roll;
}
@@ -101,7 +106,7 @@ namespace SharpHash.Checksums
{
self = new fuzzy_state();
self.bh = new blockhash_context[NUM_BLOCKHASHES];
for(int i = 0; i < NUM_BLOCKHASHES; i++)
for (int i = 0; i < NUM_BLOCKHASHES; i++)
self.bh[i].digest = new byte[SPAMSUM_LENGTH];
self.bhstart = 0;
@@ -227,14 +232,16 @@ namespace SharpHash.Checksums
/* 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) {
if (0 == self.bh[i].dlen)
{
/* Can only happen 30 times. */
/* First step for this blocksize. Clone next. */
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];
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
* easiest way to cope with this is to only reset the
* normal hash if we have room for more characters in
@@ -243,11 +250,13 @@ namespace SharpHash.Checksums
* */
self.bh[i].digest[++(self.bh[i].dlen)] = 0;
self.bh[i].h = HASH_INIT;
if (self.bh[i].dlen < SPAMSUM_LENGTH / 2) {
if (self.bh[i].dlen < SPAMSUM_LENGTH / 2)
{
self.bh[i].halfh = HASH_INIT;
self.bh[i].halfdigest = 0;
}
} else
}
else
fuzzy_try_reduce_blockhash();
}
}
@@ -283,15 +292,17 @@ namespace SharpHash.Checksums
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 || (UInt64)SSDEEP_BS(bi) / 2 * SPAMSUM_LENGTH < self.total_size))
if (!(bi == 0 || (UInt64)SSDEEP_BS(bi) / 2 * SPAMSUM_LENGTH < self.total_size))
throw new Exception("Assertion failed");
result_off = 0;
/* Initial blocksize guess. */
while ((UInt64)SSDEEP_BS(bi) * SPAMSUM_LENGTH < self.total_size) {
while ((UInt64)SSDEEP_BS(bi) * SPAMSUM_LENGTH < self.total_size)
{
++bi;
if (bi >= NUM_BLOCKHASHES) {
if (bi >= NUM_BLOCKHASHES)
{
throw new OverflowException("The input exceeds data types.");
}
}
@@ -300,7 +311,7 @@ namespace SharpHash.Checksums
--bi;
while (bi > self.bhstart && self.bh[bi].dlen < SPAMSUM_LENGTH / 2)
--bi;
if((bi > 0 && self.bh[bi].dlen < SPAMSUM_LENGTH / 2))
if ((bi > 0 && self.bh[bi].dlen < SPAMSUM_LENGTH / 2))
throw new Exception("Assertion failed");
sb.AppendFormat("{0}:", SSDEEP_BS(bi));
@@ -328,21 +339,25 @@ namespace SharpHash.Checksums
if (remain <= 0)
throw new Exception("Assertion failed");
result[result_off] = b64[self.bh[bi].h % 64];
if(i < 3 ||
result[result_off] != result[result_off-1] ||
result[result_off] != result[result_off-2] ||
result[result_off] != result[result_off-3]) {
if (i < 3 ||
result[result_off] != result[result_off - 1] ||
result[result_off] != result[result_off - 2] ||
result[result_off] != result[result_off - 3])
{
++result_off;
--remain;
}
} else if (self.bh[bi].digest[i] != 0) {
}
else if (self.bh[bi].digest[i] != 0)
{
if (remain <= 0)
throw new Exception("Assertion failed");
result[result_off] = self.bh[bi].digest[i];
if(i < 3 ||
result[result_off] != result[result_off-1] ||
result[result_off] != result[result_off-2] ||
result[result_off] != result[result_off-3]) {
if (i < 3 ||
result[result_off] != result[result_off - 1] ||
result[result_off] != result[result_off - 2] ||
result[result_off] != result[result_off - 3])
{
++result_off;
--remain;
}
@@ -361,34 +376,41 @@ namespace SharpHash.Checksums
result_off += i;
remain -= i;
if (h != 0) {
if (h != 0)
{
if (remain <= 0)
throw new Exception("Assertion failed");
h = self.bh[bi].halfh;
result[result_off] = b64[h % 64];
if(i < 3 ||
result[result_off] != result[result_off-1] ||
result[result_off] != result[result_off-2] ||
result[result_off] != result[result_off-3]) {
if (i < 3 ||
result[result_off] != result[result_off - 1] ||
result[result_off] != result[result_off - 2] ||
result[result_off] != result[result_off - 3])
{
++result_off;
--remain;
}
} else {
}
else
{
i = self.bh[bi].halfdigest;
if (i != 0) {
if (i != 0)
{
if (remain <= 0)
throw new Exception("Assertion failed");
result[result_off] = (byte)i;
if(i < 3 ||
result[result_off] != result[result_off-1] ||
result[result_off] != result[result_off-2] ||
result[result_off] != result[result_off-3]) {
if (i < 3 ||
result[result_off] != result[result_off - 1] ||
result[result_off] != result[result_off - 2] ||
result[result_off] != result[result_off - 3])
{
++result_off;
--remain;
}
}
}
} else if (h != 0)
}
else if (h != 0)
{
if (self.bh[bi].dlen != 0)
throw new Exception("Assertion failed");
@@ -427,7 +449,7 @@ namespace SharpHash.Checksums
/// Gets the hash of a file
/// </summary>
/// <param name="filename">File path.</param>
public byte[] File(string filename)
public static byte[] File(string filename)
{
// SpamSum does not have a binary representation, or so it seems
throw new NotImplementedException("SpamSum does not have a binary representation.");
@@ -438,7 +460,7 @@ namespace SharpHash.Checksums
/// </summary>
/// <param name="filename">File path.</param>
/// <param name="hash">Byte array of the hash value.</param>
public string File(string filename, out byte[] hash)
public static string File(string filename, out byte[] hash)
{
// SpamSum does not have a binary representation, or so it seems
throw new NotImplementedException("Not yet implemented.");

View File

@@ -32,10 +32,10 @@ namespace SharpHash
public static void Main(string[] args)
{
object[] attributes = typeof(MainClass).Assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
string AssemblyTitle = ((AssemblyTitleAttribute) attributes[0]).Title;
string AssemblyTitle = ((AssemblyTitleAttribute)attributes[0]).Title;
attributes = typeof(MainClass).Assembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
Version AssemblyVersion = typeof(MainClass).Assembly.GetName().Version;
string AssemblyCopyright = ((AssemblyCopyrightAttribute) attributes[0]).Copyright;
string AssemblyCopyright = ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
Console.WriteLine("{0} {1}", AssemblyTitle, AssemblyVersion);
Console.WriteLine("{0}", AssemblyCopyright);
@@ -47,7 +47,7 @@ namespace SharpHash
return;
}
if(!File.Exists(args[0]))
if (!File.Exists(args[0]))
{
Console.WriteLine("Specified file cannot be found.");
return;
@@ -55,11 +55,11 @@ namespace SharpHash
FileStream fileStream = new FileStream(args[0], FileMode.Open, FileAccess.Read);
Int64 bufferSize = 131072;
byte[] dataBuffer = new byte[bufferSize];
const Int64 bufferSize = 131072;
byte[] dataBuffer;
Console.WriteLine("Checking for magic's file executable in path");
bool thereIsMagic = false;
bool thereIsMagic;
try
{
@@ -276,7 +276,7 @@ namespace SharpHash
fileStream.Close();
}
private static string stringify(byte[] hash)
static string stringify(byte[] hash)
{
StringBuilder hashOutput = new StringBuilder();