Added option to output XML with hashes and file information
This commit is contained in:
130
SharpHash/FileHash.cs
Normal file
130
SharpHash/FileHash.cs
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
//
|
||||||
|
// XmlHash.cs
|
||||||
|
//
|
||||||
|
// Author:
|
||||||
|
// Natalia Portillo <claunia@claunia.com>
|
||||||
|
//
|
||||||
|
// Copyright (c) 2015 © Claunia.com
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace SharpHash
|
||||||
|
{
|
||||||
|
public struct FileHash
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// File attributes
|
||||||
|
/// </summary>
|
||||||
|
public FileAttributes attributes;
|
||||||
|
/// <summary>
|
||||||
|
/// File creation time (UTC)
|
||||||
|
/// </summary>
|
||||||
|
public DateTime ctime;
|
||||||
|
/// <summary>
|
||||||
|
/// File last access time (UTC)
|
||||||
|
/// </summary>
|
||||||
|
public DateTime atime;
|
||||||
|
/// <summary>
|
||||||
|
/// File last modification time (UTC)
|
||||||
|
/// </summary>
|
||||||
|
public DateTime mtime;
|
||||||
|
/// <summary>
|
||||||
|
/// File path
|
||||||
|
/// </summary>
|
||||||
|
public string path;
|
||||||
|
/// <summary>
|
||||||
|
/// Filename
|
||||||
|
/// </summary>
|
||||||
|
public string name;
|
||||||
|
/// <summary>
|
||||||
|
/// File length
|
||||||
|
/// </summary>
|
||||||
|
public long length;
|
||||||
|
/// <summary>
|
||||||
|
/// File Adler32 checksum
|
||||||
|
/// </summary>
|
||||||
|
public byte[] adler32;
|
||||||
|
/// <summary>
|
||||||
|
/// File CRC16 checksum
|
||||||
|
/// </summary>
|
||||||
|
public byte[] crc16;
|
||||||
|
/// <summary>
|
||||||
|
/// File CRC32 checksum
|
||||||
|
/// </summary>
|
||||||
|
public byte[] crc32;
|
||||||
|
/// <summary>
|
||||||
|
/// File CRC64 checksum
|
||||||
|
/// </summary>
|
||||||
|
public byte[] crc64;
|
||||||
|
/// <summary>
|
||||||
|
/// File Fletcher-16 checksum
|
||||||
|
/// </summary>
|
||||||
|
public byte[] fletcher16;
|
||||||
|
/// <summary>
|
||||||
|
/// File Fletcher-32 checksum
|
||||||
|
/// </summary>
|
||||||
|
public byte[] fletcher32;
|
||||||
|
/// <summary>
|
||||||
|
/// File MD5 hash
|
||||||
|
/// </summary>
|
||||||
|
public byte[] md5;
|
||||||
|
/// <summary>
|
||||||
|
/// File RIPEMD160 hash
|
||||||
|
/// </summary>
|
||||||
|
public byte[] ripemd160;
|
||||||
|
/// <summary>
|
||||||
|
/// File SHA1 hash
|
||||||
|
/// </summary>
|
||||||
|
public byte[] sha1;
|
||||||
|
/// <summary>
|
||||||
|
/// File SHA2-256 hash
|
||||||
|
/// </summary>
|
||||||
|
public byte[] sha256;
|
||||||
|
/// <summary>
|
||||||
|
/// File SHA2-384 hash
|
||||||
|
/// </summary>
|
||||||
|
public byte[] sha384;
|
||||||
|
/// <summary>
|
||||||
|
/// File SHA2-512 hash
|
||||||
|
/// </summary>
|
||||||
|
public byte[] sha512;
|
||||||
|
/// <summary>
|
||||||
|
/// File SHA3-512 hash
|
||||||
|
/// </summary>
|
||||||
|
public byte[] sha3;
|
||||||
|
/// <summary>
|
||||||
|
/// File SpamSum hash
|
||||||
|
/// </summary>
|
||||||
|
public string spamsum;
|
||||||
|
/// <summary>
|
||||||
|
/// Description given by libmagic
|
||||||
|
/// </summary>
|
||||||
|
public string magic;
|
||||||
|
/// <summary>
|
||||||
|
/// Apple OSType pair given by libmagic
|
||||||
|
/// </summary>
|
||||||
|
public string applePair;
|
||||||
|
/// <summary>
|
||||||
|
/// MIME type given by libmagic
|
||||||
|
/// </summary>
|
||||||
|
public string mimeType;
|
||||||
|
/// <summary>
|
||||||
|
/// MIME encoding given by libmagic
|
||||||
|
/// </summary>
|
||||||
|
public string mimeEncoding;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -42,19 +42,40 @@ namespace SharpHash
|
|||||||
Console.WriteLine("{0}", AssemblyCopyright);
|
Console.WriteLine("{0}", AssemblyCopyright);
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
|
|
||||||
if (args.Length != 1)
|
string filename;
|
||||||
|
bool outputXml;
|
||||||
|
|
||||||
|
if (args.Length == 2 && args[0] == "--xml")
|
||||||
|
{
|
||||||
|
filename = args[1];
|
||||||
|
outputXml = true;
|
||||||
|
}
|
||||||
|
else if (args.Length != 1)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Please specify file to hash.");
|
Console.WriteLine("Please specify file to hash.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
filename = args[0];
|
||||||
|
|
||||||
if (!File.Exists(args[0]))
|
if (!File.Exists(filename))
|
||||||
{
|
{
|
||||||
Console.WriteLine("Specified file cannot be found.");
|
Console.WriteLine("Specified file cannot be found.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
FileStream fileStream = new FileStream(args[0], FileMode.Open, FileAccess.Read);
|
FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
|
||||||
|
FileInfo fi = new FileInfo(filename);
|
||||||
|
|
||||||
|
FileHash fh = new FileHash();
|
||||||
|
|
||||||
|
fh.atime = fi.LastAccessTimeUtc;
|
||||||
|
fh.attributes = fi.Attributes;
|
||||||
|
fh.ctime = fi.CreationTimeUtc;
|
||||||
|
fh.length = fi.Length;
|
||||||
|
fh.mtime = fi.LastWriteTimeUtc;
|
||||||
|
fh.name = fi.Name;
|
||||||
|
fh.path = Path.GetDirectoryName(fi.FullName);
|
||||||
|
|
||||||
const Int64 bufferSize = 131072;
|
const Int64 bufferSize = 131072;
|
||||||
byte[] dataBuffer = new byte[bufferSize];
|
byte[] dataBuffer = new byte[bufferSize];
|
||||||
@@ -82,8 +103,6 @@ namespace SharpHash
|
|||||||
Console.WriteLine("magic's file not found in path");
|
Console.WriteLine("magic's file not found in path");
|
||||||
}
|
}
|
||||||
|
|
||||||
string magic = "", applePair = "", mimeType = "", mimeEncoding = "";
|
|
||||||
|
|
||||||
if (thereIsMagic)
|
if (thereIsMagic)
|
||||||
{
|
{
|
||||||
Process magicProcess = new Process();
|
Process magicProcess = new Process();
|
||||||
@@ -92,24 +111,24 @@ namespace SharpHash
|
|||||||
magicProcess.StartInfo.RedirectStandardError = true;
|
magicProcess.StartInfo.RedirectStandardError = true;
|
||||||
magicProcess.StartInfo.FileName = "file";
|
magicProcess.StartInfo.FileName = "file";
|
||||||
|
|
||||||
magicProcess.StartInfo.Arguments = "--brief --preserve-date " + args[0];
|
magicProcess.StartInfo.Arguments = "--brief --preserve-date " + filename;
|
||||||
magicProcess.Start();
|
magicProcess.Start();
|
||||||
magic = magicProcess.StandardOutput.ReadToEnd();
|
fh.magic = magicProcess.StandardOutput.ReadToEnd();
|
||||||
magicProcess.WaitForExit();
|
magicProcess.WaitForExit();
|
||||||
|
|
||||||
magicProcess.StartInfo.Arguments = "--brief --preserve-date --apple " + args[0];
|
magicProcess.StartInfo.Arguments = "--brief --preserve-date --apple " + filename;
|
||||||
magicProcess.Start();
|
magicProcess.Start();
|
||||||
applePair = magicProcess.StandardOutput.ReadToEnd();
|
fh.applePair = magicProcess.StandardOutput.ReadToEnd();
|
||||||
magicProcess.WaitForExit();
|
magicProcess.WaitForExit();
|
||||||
|
|
||||||
magicProcess.StartInfo.Arguments = "--brief --preserve-date --mime-type " + args[0];
|
magicProcess.StartInfo.Arguments = "--brief --preserve-date --mime-type " + filename;
|
||||||
magicProcess.Start();
|
magicProcess.Start();
|
||||||
mimeType = magicProcess.StandardOutput.ReadToEnd();
|
fh.mimeType = magicProcess.StandardOutput.ReadToEnd();
|
||||||
magicProcess.WaitForExit();
|
magicProcess.WaitForExit();
|
||||||
|
|
||||||
magicProcess.StartInfo.Arguments = "--brief --preserve-date --mime-encoding " + args[0];
|
magicProcess.StartInfo.Arguments = "--brief --preserve-date --mime-encoding " + filename;
|
||||||
magicProcess.Start();
|
magicProcess.Start();
|
||||||
mimeEncoding = magicProcess.StandardOutput.ReadToEnd();
|
fh.mimeEncoding = magicProcess.StandardOutput.ReadToEnd();
|
||||||
magicProcess.WaitForExit();
|
magicProcess.WaitForExit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -532,47 +551,64 @@ namespace SharpHash
|
|||||||
tSHA3.IsAlive || tSpamSum.IsAlive);
|
tSHA3.IsAlive || tSpamSum.IsAlive);
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] crc16Hash = crc16Context.Final();
|
fileStream.Close();
|
||||||
byte[] crc32Hash = crc32Context.Final();
|
|
||||||
byte[] crc64Hash = crc64Context.Final();
|
|
||||||
byte[] fletcher16Hash = fletcher16Context.Final();
|
|
||||||
byte[] fletcher32Hash = fletcher32Context.Final();
|
|
||||||
byte[] adler32Hash = adler32Context.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();
|
|
||||||
string spamsumHash = spamsumContext.End();
|
|
||||||
|
|
||||||
Console.WriteLine();
|
fh.crc16 = crc16Context.Final();
|
||||||
Console.WriteLine();
|
fh.crc32 = crc32Context.Final();
|
||||||
if (thereIsMagic)
|
fh.crc64 = crc64Context.Final();
|
||||||
|
fh.fletcher16 = fletcher16Context.Final();
|
||||||
|
fh.fletcher32 = fletcher32Context.Final();
|
||||||
|
fh.adler32 = adler32Context.Final();
|
||||||
|
fh.md5 = md5Context.Final();
|
||||||
|
fh.ripemd160 = ripemd160Context.Final();
|
||||||
|
fh.sha1 = sha1Context.Final();
|
||||||
|
fh.sha256 = sha256Context.Final();
|
||||||
|
fh.sha384 = sha384Context.Final();
|
||||||
|
fh.sha512 = sha512Context.Final();
|
||||||
|
fh.sha3 = sha3Context.Final();
|
||||||
|
fh.spamsum = spamsumContext.End();
|
||||||
|
|
||||||
|
if (outputXml)
|
||||||
{
|
{
|
||||||
Console.Write("magic's Description = {0}", magic);
|
Console.WriteLine();
|
||||||
Console.Write("Apple OSType Pair = {0}", applePair);
|
System.Xml.Serialization.XmlSerializer fhSerializer = new System.Xml.Serialization.XmlSerializer(fh.GetType());
|
||||||
Console.Write("MIME Type = {0}", mimeType);
|
fhSerializer.Serialize(Console.Out, fh);
|
||||||
Console.Write("MIME Encoding = {0}", mimeEncoding);
|
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
}
|
}
|
||||||
Console.WriteLine("CRC16: {0}", stringify(crc16Hash));
|
else
|
||||||
Console.WriteLine("CRC32: {0}", stringify(crc32Hash));
|
{
|
||||||
Console.WriteLine("CRC64: {0}", stringify(crc64Hash));
|
Console.WriteLine();
|
||||||
Console.WriteLine("Fletcher-16: {0}", stringify(fletcher16Hash));
|
Console.WriteLine();
|
||||||
Console.WriteLine("Fletcher-32: {0}", stringify(fletcher32Hash));
|
Console.WriteLine("File name: {0}", fh.name);
|
||||||
Console.WriteLine("Adler-32: {0}", stringify(adler32Hash));
|
Console.WriteLine("File path: {0}", fh.path);
|
||||||
Console.WriteLine("MD5: {0}", stringify(md5Hash));
|
Console.WriteLine("File length: {0}", fh.length);
|
||||||
Console.WriteLine("RIPEMD160: {0}", stringify(ripemd160Hash));
|
Console.WriteLine("File attributes: {0}", fh.attributes);
|
||||||
Console.WriteLine("SHA1: {0}", stringify(sha1Hash));
|
Console.WriteLine("File creation time: {0}", fh.ctime);
|
||||||
Console.WriteLine("SHA2-256: {0}", stringify(sha256Hash));
|
Console.WriteLine("File last modification time: {0}", fh.mtime);
|
||||||
Console.WriteLine("SHA2-384: {0}", stringify(sha384Hash));
|
Console.WriteLine("File last access time: {0}", fh.atime);
|
||||||
Console.WriteLine("SHA2-512: {0}", stringify(sha512Hash));
|
if (thereIsMagic)
|
||||||
Console.WriteLine("SHA3-512: {0}", stringify(sha3Hash));
|
{
|
||||||
Console.WriteLine("SpamSum: {0}", spamsumHash);
|
Console.Write("magic's Description = {0}", fh.magic);
|
||||||
|
Console.Write("Apple OSType Pair = {0}", fh.applePair);
|
||||||
fileStream.Close();
|
Console.Write("MIME Type = {0}", fh.mimeType);
|
||||||
|
Console.Write("MIME Encoding = {0}", fh.mimeEncoding);
|
||||||
|
Console.WriteLine();
|
||||||
|
}
|
||||||
|
Console.WriteLine("CRC16: {0}", stringify(fh.crc16));
|
||||||
|
Console.WriteLine("CRC32: {0}", stringify(fh.crc32));
|
||||||
|
Console.WriteLine("CRC64: {0}", stringify(fh.crc64));
|
||||||
|
Console.WriteLine("Fletcher-16: {0}", stringify(fh.fletcher16));
|
||||||
|
Console.WriteLine("Fletcher-32: {0}", stringify(fh.fletcher32));
|
||||||
|
Console.WriteLine("Adler-32: {0}", stringify(fh.adler32));
|
||||||
|
Console.WriteLine("MD5: {0}", stringify(fh.md5));
|
||||||
|
Console.WriteLine("RIPEMD160: {0}", stringify(fh.ripemd160));
|
||||||
|
Console.WriteLine("SHA1: {0}", stringify(fh.sha1));
|
||||||
|
Console.WriteLine("SHA2-256: {0}", stringify(fh.sha256));
|
||||||
|
Console.WriteLine("SHA2-384: {0}", stringify(fh.sha384));
|
||||||
|
Console.WriteLine("SHA2-512: {0}", stringify(fh.sha512));
|
||||||
|
Console.WriteLine("SHA3-512: {0}", stringify(fh.sha3));
|
||||||
|
Console.WriteLine("SpamSum: {0}", fh.spamsum);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static string stringify(byte[] hash)
|
static string stringify(byte[] hash)
|
||||||
|
|||||||
@@ -34,6 +34,8 @@
|
|||||||
<Reference Include="SHA3Managed">
|
<Reference Include="SHA3Managed">
|
||||||
<HintPath>..\packages\SHA3.0.9.2\lib\net40\SHA3Managed.dll</HintPath>
|
<HintPath>..\packages\SHA3.0.9.2\lib\net40\SHA3Managed.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
<Reference Include="System.Xml.Serialization" />
|
||||||
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Program.cs" />
|
<Compile Include="Program.cs" />
|
||||||
@@ -52,6 +54,7 @@
|
|||||||
<Compile Include="Checksums\FletcherContext.cs" />
|
<Compile Include="Checksums\FletcherContext.cs" />
|
||||||
<Compile Include="Checksums\Adler32Context.cs" />
|
<Compile Include="Checksums\Adler32Context.cs" />
|
||||||
<Compile Include="Checksums\SpamSumContext.cs" />
|
<Compile Include="Checksums\SpamSumContext.cs" />
|
||||||
|
<Compile Include="FileHash.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user