Added hashing code
This commit is contained in:
@@ -21,6 +21,8 @@
|
||||
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace SharpHash
|
||||
{
|
||||
@@ -37,6 +39,170 @@ namespace SharpHash
|
||||
Console.WriteLine("{0} {1}", AssemblyTitle, AssemblyVersion);
|
||||
Console.WriteLine("{0}", AssemblyCopyright);
|
||||
Console.WriteLine();
|
||||
|
||||
if (args.Length != 1)
|
||||
{
|
||||
Console.WriteLine("Please specify file to hash.");
|
||||
return;
|
||||
}
|
||||
|
||||
if(!File.Exists(args[0]))
|
||||
{
|
||||
Console.WriteLine("Specified file cannot be found.");
|
||||
return;
|
||||
}
|
||||
|
||||
FileStream fileStream = new FileStream(args[0], FileMode.Open, FileAccess.Read);
|
||||
|
||||
Int64 bufferSize = 131072;
|
||||
byte[] dataBuffer = new byte[bufferSize];
|
||||
|
||||
Console.WriteLine("Initializing CRC16...");
|
||||
Checksums.CRC16Context crc16Context = new Checksums.CRC16Context();
|
||||
crc16Context.Init();
|
||||
|
||||
Console.WriteLine("Initializing CRC32...");
|
||||
Checksums.CRC32Context crc32Context = new Checksums.CRC32Context();
|
||||
crc32Context.Init();
|
||||
|
||||
Console.WriteLine("Initializing CRC64...");
|
||||
Checksums.CRC64Context crc64Context = new Checksums.CRC64Context();
|
||||
crc64Context.Init();
|
||||
|
||||
Console.WriteLine("Initializing Fletcher-16...");
|
||||
Checksums.Fletcher16Context fletcher16Context = new Checksums.Fletcher16Context();
|
||||
fletcher16Context.Init();
|
||||
|
||||
Console.WriteLine("Initializing Fletcher-32...");
|
||||
Checksums.Fletcher32Context fletcher32Context = new Checksums.Fletcher32Context();
|
||||
fletcher32Context.Init();
|
||||
|
||||
Console.WriteLine("Initializing MD5...");
|
||||
Checksums.MD5Context md5Context = new Checksums.MD5Context();
|
||||
md5Context.Init();
|
||||
|
||||
Console.WriteLine("Initializing RIPEMD160...");
|
||||
Checksums.RIPEMD160Context ripemd160Context = new Checksums.RIPEMD160Context();
|
||||
ripemd160Context.Init();
|
||||
|
||||
Console.WriteLine("Initializing SHA1...");
|
||||
Checksums.SHA1Context sha1Context = new Checksums.SHA1Context();
|
||||
sha1Context.Init();
|
||||
|
||||
Console.WriteLine("Initializing SHA2-256...");
|
||||
Checksums.SHA256Context sha256Context = new Checksums.SHA256Context();
|
||||
sha256Context.Init();
|
||||
|
||||
Console.WriteLine("Initializing SHA2-384...");
|
||||
Checksums.SHA384Context sha384Context = new Checksums.SHA384Context();
|
||||
sha384Context.Init();
|
||||
|
||||
Console.WriteLine("Initializing SHA2-512...");
|
||||
Checksums.SHA512Context sha512Context = new Checksums.SHA512Context();
|
||||
sha512Context.Init();
|
||||
|
||||
Console.WriteLine("Initializing SHA3-512...");
|
||||
Checksums.SHA3Context sha3Context = new Checksums.SHA3Context();
|
||||
sha3Context.Init();
|
||||
|
||||
if (fileStream.Length > bufferSize)
|
||||
{
|
||||
int offset;
|
||||
long remainder = fileStream.Length % bufferSize;
|
||||
|
||||
for (offset = 0; offset < (fileStream.Length - remainder); offset += (int)bufferSize)
|
||||
{
|
||||
Console.Write("\rHashing offset {0}", offset);
|
||||
dataBuffer = new byte[bufferSize];
|
||||
fileStream.Read(dataBuffer, 0, (int)bufferSize);
|
||||
crc16Context.Update(dataBuffer);
|
||||
crc32Context.Update(dataBuffer);
|
||||
crc64Context.Update(dataBuffer);
|
||||
fletcher16Context.Update(dataBuffer);
|
||||
fletcher32Context.Update(dataBuffer);
|
||||
md5Context.Update(dataBuffer);
|
||||
ripemd160Context.Update(dataBuffer);
|
||||
sha1Context.Update(dataBuffer);
|
||||
sha256Context.Update(dataBuffer);
|
||||
sha384Context.Update(dataBuffer);
|
||||
sha512Context.Update(dataBuffer);
|
||||
sha3Context.Update(dataBuffer);
|
||||
}
|
||||
|
||||
dataBuffer = new byte[remainder];
|
||||
|
||||
fileStream.Read(dataBuffer, 0, (int)remainder);
|
||||
crc16Context.Update(dataBuffer);
|
||||
crc32Context.Update(dataBuffer);
|
||||
crc64Context.Update(dataBuffer);
|
||||
fletcher16Context.Update(dataBuffer);
|
||||
fletcher32Context.Update(dataBuffer);
|
||||
md5Context.Update(dataBuffer);
|
||||
ripemd160Context.Update(dataBuffer);
|
||||
sha1Context.Update(dataBuffer);
|
||||
sha256Context.Update(dataBuffer);
|
||||
sha384Context.Update(dataBuffer);
|
||||
sha512Context.Update(dataBuffer);
|
||||
sha3Context.Update(dataBuffer);
|
||||
}
|
||||
else
|
||||
{
|
||||
dataBuffer = new byte[fileStream.Length];
|
||||
|
||||
fileStream.Read(dataBuffer, 0, (int)fileStream.Length);
|
||||
crc16Context.Update(dataBuffer);
|
||||
crc32Context.Update(dataBuffer);
|
||||
crc64Context.Update(dataBuffer);
|
||||
fletcher16Context.Update(dataBuffer);
|
||||
fletcher32Context.Update(dataBuffer);
|
||||
md5Context.Update(dataBuffer);
|
||||
ripemd160Context.Update(dataBuffer);
|
||||
sha1Context.Update(dataBuffer);
|
||||
sha256Context.Update(dataBuffer);
|
||||
sha384Context.Update(dataBuffer);
|
||||
sha512Context.Update(dataBuffer);
|
||||
sha3Context.Update(dataBuffer);
|
||||
}
|
||||
|
||||
byte[] crc16Hash = crc16Context.Final();
|
||||
byte[] crc32Hash = crc32Context.Final();
|
||||
byte[] crc64Hash = crc64Context.Final();
|
||||
byte[] fletcher16Hash = fletcher16Context.Final();
|
||||
byte[] fletcher32Hash = fletcher32Context.Final();
|
||||
byte[] md5Hash = md5Context.Final();
|
||||
byte[] ripemd160Hash = ripemd160Context.Final();
|
||||
byte[] sha1Hash = sha1Context.Final();
|
||||
byte[] sha256Hash = sha256Context.Final();
|
||||
byte[] sha384Hash = sha384Context.Final();
|
||||
byte[] sha512Hash = sha512Context.Final();
|
||||
byte[] sha3Hash = sha3Context.Final();
|
||||
|
||||
Console.WriteLine("CRC16: {0}", stringify(crc16Hash));
|
||||
Console.WriteLine("CRC32: {0}", stringify(crc32Hash));
|
||||
Console.WriteLine("CRC64: {0}", stringify(crc64Hash));
|
||||
Console.WriteLine("Fletcher-16: {0}", stringify(fletcher16Hash));
|
||||
Console.WriteLine("Fletcher-32: {0}", stringify(fletcher32Hash));
|
||||
Console.WriteLine("MD5: {0}", stringify(md5Hash));
|
||||
Console.WriteLine("RIPEMD160: {0}", stringify(ripemd160Hash));
|
||||
Console.WriteLine("SHA1: {0}", stringify(sha1Hash));
|
||||
Console.WriteLine("SHA2-256: {0}", stringify(sha256Hash));
|
||||
Console.WriteLine("SHA2-384: {0}", stringify(sha384Hash));
|
||||
Console.WriteLine("SHA2-512: {0}", stringify(sha512Hash));
|
||||
Console.WriteLine("SHA3-512: {0}", stringify(sha3Hash));
|
||||
|
||||
fileStream.Close();
|
||||
}
|
||||
|
||||
private static string stringify(byte[] hash)
|
||||
{
|
||||
StringBuilder hashOutput = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < hash.Length; i++)
|
||||
{
|
||||
hashOutput.Append(hash[i].ToString("x2"));
|
||||
}
|
||||
|
||||
return hashOutput.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user