Prettify code for github publishing
This commit is contained in:
@@ -26,8 +26,8 @@ namespace SharpHash.Checksums
|
|||||||
{
|
{
|
||||||
public class Adler32Context
|
public class Adler32Context
|
||||||
{
|
{
|
||||||
private UInt16 sum1, sum2;
|
UInt16 sum1, sum2;
|
||||||
private const UInt16 AdlerModule = 65521;
|
const UInt16 AdlerModule = 65521;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes the Adler-32 sums
|
/// Initializes the Adler-32 sums
|
||||||
@@ -134,7 +134,7 @@ namespace SharpHash.Checksums
|
|||||||
/// <param name="data">Data buffer.</param>
|
/// <param name="data">Data buffer.</param>
|
||||||
/// <param name="len">Length of the data buffer to hash.</param>
|
/// <param name="len">Length of the data buffer to hash.</param>
|
||||||
/// <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 static string Data(byte[] data, uint len, out byte[] hash)
|
||||||
{
|
{
|
||||||
UInt16 localSum1, localSum2;
|
UInt16 localSum1, localSum2;
|
||||||
UInt32 finalSum;
|
UInt32 finalSum;
|
||||||
@@ -167,7 +167,7 @@ namespace SharpHash.Checksums
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="data">Data buffer.</param>
|
/// <param name="data">Data buffer.</param>
|
||||||
/// <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, out byte[] hash)
|
public static string Data(byte[] data, out byte[] hash)
|
||||||
{
|
{
|
||||||
return Data(data, (uint)data.Length, out hash);
|
return Data(data, (uint)data.Length, out hash);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,9 +26,9 @@ namespace SharpHash.Checksums
|
|||||||
{
|
{
|
||||||
public class Fletcher32Context
|
public class Fletcher32Context
|
||||||
{
|
{
|
||||||
private UInt16 sum1, sum2;
|
UInt16 sum1, sum2;
|
||||||
private byte oddValue;
|
byte oddValue;
|
||||||
private bool inodd;
|
bool inodd;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes the Fletcher32 sums
|
/// Initializes the Fletcher32 sums
|
||||||
@@ -224,7 +224,7 @@ namespace SharpHash.Checksums
|
|||||||
/// <param name="data">Data buffer.</param>
|
/// <param name="data">Data buffer.</param>
|
||||||
/// <param name="len">Length of the data buffer to hash.</param>
|
/// <param name="len">Length of the data buffer to hash.</param>
|
||||||
/// <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 static string Data(byte[] data, uint len, out byte[] hash)
|
||||||
{
|
{
|
||||||
UInt16 localSum1, localSum2, block;
|
UInt16 localSum1, localSum2, block;
|
||||||
UInt32 finalSum;
|
UInt32 finalSum;
|
||||||
@@ -279,7 +279,7 @@ namespace SharpHash.Checksums
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="data">Data buffer.</param>
|
/// <param name="data">Data buffer.</param>
|
||||||
/// <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, out byte[] hash)
|
public static string Data(byte[] data, out byte[] hash)
|
||||||
{
|
{
|
||||||
return Data(data, (uint)data.Length, out hash);
|
return Data(data, (uint)data.Length, out hash);
|
||||||
}
|
}
|
||||||
@@ -287,7 +287,7 @@ namespace SharpHash.Checksums
|
|||||||
|
|
||||||
public class Fletcher16Context
|
public class Fletcher16Context
|
||||||
{
|
{
|
||||||
private byte sum1, sum2;
|
byte sum1, sum2;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes the Fletcher16 sums
|
/// Initializes the Fletcher16 sums
|
||||||
@@ -399,7 +399,7 @@ namespace SharpHash.Checksums
|
|||||||
/// <param name="data">Data buffer.</param>
|
/// <param name="data">Data buffer.</param>
|
||||||
/// <param name="len">Length of the data buffer to hash.</param>
|
/// <param name="len">Length of the data buffer to hash.</param>
|
||||||
/// <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 static string Data(byte[] data, uint len, out byte[] hash)
|
||||||
{
|
{
|
||||||
byte localSum1, localSum2;
|
byte localSum1, localSum2;
|
||||||
UInt16 finalSum;
|
UInt16 finalSum;
|
||||||
@@ -432,7 +432,7 @@ namespace SharpHash.Checksums
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="data">Data buffer.</param>
|
/// <param name="data">Data buffer.</param>
|
||||||
/// <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, out byte[] hash)
|
public static string Data(byte[] data, out byte[] hash)
|
||||||
{
|
{
|
||||||
return Data(data, (uint)data.Length, out hash);
|
return Data(data, (uint)data.Length, out hash);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ namespace SharpHash.Checksums
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void Init()
|
public void Init()
|
||||||
{
|
{
|
||||||
_sha3Provider = new SHA3Unmanaged(512);;
|
_sha3Provider = new SHA3Unmanaged(512);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -45,18 +45,21 @@ namespace SharpHash.Checksums
|
|||||||
const UInt32 SPAMSUM_LENGTH = 64;
|
const UInt32 SPAMSUM_LENGTH = 64;
|
||||||
const UInt32 FUZZY_MAX_RESULT = (2 * SPAMSUM_LENGTH + 20);
|
const UInt32 FUZZY_MAX_RESULT = (2 * SPAMSUM_LENGTH + 20);
|
||||||
//"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
//"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||||
readonly byte[] b64 = {0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
|
readonly byte[] b64 =
|
||||||
|
{0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
|
||||||
0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
|
0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
|
||||||
0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
|
0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
|
||||||
0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
|
0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
|
||||||
0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E,
|
0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E,
|
||||||
0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
|
0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
|
||||||
0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33,
|
0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33,
|
||||||
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F};
|
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F
|
||||||
|
};
|
||||||
|
|
||||||
struct roll_state
|
struct roll_state
|
||||||
{
|
{
|
||||||
public byte[] window; // ROLLING_WINDOW
|
public byte[] window;
|
||||||
|
// ROLLING_WINDOW
|
||||||
public UInt32 h1;
|
public UInt32 h1;
|
||||||
public UInt32 h2;
|
public UInt32 h2;
|
||||||
public UInt32 h3;
|
public UInt32 h3;
|
||||||
@@ -72,7 +75,8 @@ namespace SharpHash.Checksums
|
|||||||
{
|
{
|
||||||
public UInt32 h;
|
public UInt32 h;
|
||||||
public UInt32 halfh;
|
public UInt32 halfh;
|
||||||
public byte[] digest; // SPAMSUM_LENGTH
|
public byte[] digest;
|
||||||
|
// SPAMSUM_LENGTH
|
||||||
public byte halfdigest;
|
public byte halfdigest;
|
||||||
public UInt32 dlen;
|
public UInt32 dlen;
|
||||||
}
|
}
|
||||||
@@ -81,7 +85,8 @@ namespace SharpHash.Checksums
|
|||||||
{
|
{
|
||||||
public UInt32 bhstart;
|
public UInt32 bhstart;
|
||||||
public UInt32 bhend;
|
public UInt32 bhend;
|
||||||
public blockhash_context[] bh; //NUM_BLOCKHASHES
|
public blockhash_context[] bh;
|
||||||
|
//NUM_BLOCKHASHES
|
||||||
public UInt64 total_size;
|
public UInt64 total_size;
|
||||||
public roll_state roll;
|
public roll_state roll;
|
||||||
}
|
}
|
||||||
@@ -227,14 +232,16 @@ namespace SharpHash.Checksums
|
|||||||
/* 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) {
|
if (0 == self.bh[i].dlen)
|
||||||
|
{
|
||||||
/* Can only happen 30 times. */
|
/* Can only happen 30 times. */
|
||||||
/* First step for this blocksize. Clone next. */
|
/* First step for this blocksize. Clone next. */
|
||||||
fuzzy_try_fork_blockhash();
|
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
|
||||||
* easiest way to cope with this is to only reset the
|
* easiest way to cope with this is to only reset the
|
||||||
* normal hash if we have room for more characters in
|
* 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].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) {
|
if (self.bh[i].dlen < SPAMSUM_LENGTH / 2)
|
||||||
|
{
|
||||||
self.bh[i].halfh = HASH_INIT;
|
self.bh[i].halfh = HASH_INIT;
|
||||||
self.bh[i].halfdigest = 0;
|
self.bh[i].halfdigest = 0;
|
||||||
}
|
}
|
||||||
} else
|
}
|
||||||
|
else
|
||||||
fuzzy_try_reduce_blockhash();
|
fuzzy_try_reduce_blockhash();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -289,9 +298,11 @@ namespace SharpHash.Checksums
|
|||||||
result_off = 0;
|
result_off = 0;
|
||||||
|
|
||||||
/* Initial blocksize guess. */
|
/* Initial blocksize guess. */
|
||||||
while ((UInt64)SSDEEP_BS(bi) * SPAMSUM_LENGTH < self.total_size) {
|
while ((UInt64)SSDEEP_BS(bi) * SPAMSUM_LENGTH < self.total_size)
|
||||||
|
{
|
||||||
++bi;
|
++bi;
|
||||||
if (bi >= NUM_BLOCKHASHES) {
|
if (bi >= NUM_BLOCKHASHES)
|
||||||
|
{
|
||||||
throw new OverflowException("The input exceeds data types.");
|
throw new OverflowException("The input exceeds data types.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -331,18 +342,22 @@ namespace SharpHash.Checksums
|
|||||||
if (i < 3 ||
|
if (i < 3 ||
|
||||||
result[result_off] != result[result_off - 1] ||
|
result[result_off] != result[result_off - 1] ||
|
||||||
result[result_off] != result[result_off - 2] ||
|
result[result_off] != result[result_off - 2] ||
|
||||||
result[result_off] != result[result_off-3]) {
|
result[result_off] != result[result_off - 3])
|
||||||
|
{
|
||||||
++result_off;
|
++result_off;
|
||||||
--remain;
|
--remain;
|
||||||
}
|
}
|
||||||
} else if (self.bh[bi].digest[i] != 0) {
|
}
|
||||||
|
else if (self.bh[bi].digest[i] != 0)
|
||||||
|
{
|
||||||
if (remain <= 0)
|
if (remain <= 0)
|
||||||
throw new Exception("Assertion failed");
|
throw new Exception("Assertion failed");
|
||||||
result[result_off] = self.bh[bi].digest[i];
|
result[result_off] = self.bh[bi].digest[i];
|
||||||
if (i < 3 ||
|
if (i < 3 ||
|
||||||
result[result_off] != result[result_off - 1] ||
|
result[result_off] != result[result_off - 1] ||
|
||||||
result[result_off] != result[result_off - 2] ||
|
result[result_off] != result[result_off - 2] ||
|
||||||
result[result_off] != result[result_off-3]) {
|
result[result_off] != result[result_off - 3])
|
||||||
|
{
|
||||||
++result_off;
|
++result_off;
|
||||||
--remain;
|
--remain;
|
||||||
}
|
}
|
||||||
@@ -361,7 +376,8 @@ namespace SharpHash.Checksums
|
|||||||
result_off += i;
|
result_off += i;
|
||||||
remain -= i;
|
remain -= i;
|
||||||
|
|
||||||
if (h != 0) {
|
if (h != 0)
|
||||||
|
{
|
||||||
if (remain <= 0)
|
if (remain <= 0)
|
||||||
throw new Exception("Assertion failed");
|
throw new Exception("Assertion failed");
|
||||||
h = self.bh[bi].halfh;
|
h = self.bh[bi].halfh;
|
||||||
@@ -369,26 +385,32 @@ namespace SharpHash.Checksums
|
|||||||
if (i < 3 ||
|
if (i < 3 ||
|
||||||
result[result_off] != result[result_off - 1] ||
|
result[result_off] != result[result_off - 1] ||
|
||||||
result[result_off] != result[result_off - 2] ||
|
result[result_off] != result[result_off - 2] ||
|
||||||
result[result_off] != result[result_off-3]) {
|
result[result_off] != result[result_off - 3])
|
||||||
|
{
|
||||||
++result_off;
|
++result_off;
|
||||||
--remain;
|
--remain;
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
i = self.bh[bi].halfdigest;
|
i = self.bh[bi].halfdigest;
|
||||||
if (i != 0) {
|
if (i != 0)
|
||||||
|
{
|
||||||
if (remain <= 0)
|
if (remain <= 0)
|
||||||
throw new Exception("Assertion failed");
|
throw new Exception("Assertion failed");
|
||||||
result[result_off] = (byte)i;
|
result[result_off] = (byte)i;
|
||||||
if (i < 3 ||
|
if (i < 3 ||
|
||||||
result[result_off] != result[result_off - 1] ||
|
result[result_off] != result[result_off - 1] ||
|
||||||
result[result_off] != result[result_off - 2] ||
|
result[result_off] != result[result_off - 2] ||
|
||||||
result[result_off] != result[result_off-3]) {
|
result[result_off] != result[result_off - 3])
|
||||||
|
{
|
||||||
++result_off;
|
++result_off;
|
||||||
--remain;
|
--remain;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (h != 0)
|
}
|
||||||
|
else if (h != 0)
|
||||||
{
|
{
|
||||||
if (self.bh[bi].dlen != 0)
|
if (self.bh[bi].dlen != 0)
|
||||||
throw new Exception("Assertion failed");
|
throw new Exception("Assertion failed");
|
||||||
@@ -427,7 +449,7 @@ namespace SharpHash.Checksums
|
|||||||
/// Gets the hash of a file
|
/// Gets the hash of a file
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="filename">File path.</param>
|
/// <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
|
// SpamSum does not have a binary representation, or so it seems
|
||||||
throw new NotImplementedException("SpamSum does not have a binary representation.");
|
throw new NotImplementedException("SpamSum does not have a binary representation.");
|
||||||
@@ -438,7 +460,7 @@ namespace SharpHash.Checksums
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="filename">File path.</param>
|
/// <param name="filename">File path.</param>
|
||||||
/// <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 static string File(string filename, out byte[] hash)
|
||||||
{
|
{
|
||||||
// SpamSum does not have a binary representation, or so it seems
|
// SpamSum does not have a binary representation, or so it seems
|
||||||
throw new NotImplementedException("Not yet implemented.");
|
throw new NotImplementedException("Not yet implemented.");
|
||||||
|
|||||||
@@ -55,11 +55,11 @@ namespace SharpHash
|
|||||||
|
|
||||||
FileStream fileStream = new FileStream(args[0], FileMode.Open, FileAccess.Read);
|
FileStream fileStream = new FileStream(args[0], FileMode.Open, FileAccess.Read);
|
||||||
|
|
||||||
Int64 bufferSize = 131072;
|
const Int64 bufferSize = 131072;
|
||||||
byte[] dataBuffer = new byte[bufferSize];
|
byte[] dataBuffer;
|
||||||
|
|
||||||
Console.WriteLine("Checking for magic's file executable in path");
|
Console.WriteLine("Checking for magic's file executable in path");
|
||||||
bool thereIsMagic = false;
|
bool thereIsMagic;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -276,7 +276,7 @@ namespace SharpHash
|
|||||||
fileStream.Close();
|
fileStream.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string stringify(byte[] hash)
|
static string stringify(byte[] hash)
|
||||||
{
|
{
|
||||||
StringBuilder hashOutput = new StringBuilder();
|
StringBuilder hashOutput = new StringBuilder();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user