Created
August 8, 2012 09:44
-
-
Save 5342/3293819 to your computer and use it in GitHub Desktop.
Command line tool to find / calculate SHA256 hash values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Security.Cryptography; | |
using System.IO; | |
namespace SHATool | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// Validate args | |
if (args.Length < 2) | |
{ | |
ShowUsage(); | |
return; | |
} | |
if (args[0].StartsWith("-f") && args.Length != 3) | |
{ | |
ShowUsage(); | |
return; | |
} | |
switch(args[0]) | |
{ | |
case "-d": | |
DirectoryHash(args, SearchOption.TopDirectoryOnly); | |
break; | |
case "-r": | |
DirectoryHash(args, SearchOption.AllDirectories); | |
break; | |
case "-fd": | |
DirectoryHash(args, SearchOption.TopDirectoryOnly, args[2]); | |
break; | |
case "-fr": | |
DirectoryHash(args, SearchOption.AllDirectories, args[2]); | |
break; | |
case "-h": | |
FileHash(args); | |
break; | |
default: | |
ShowUsage(); | |
break; | |
} | |
} | |
private static void ShowUsage() | |
{ | |
// This should be in a resource file | |
StringBuilder usage = new StringBuilder(); | |
usage.AppendLine("Creates a SHA256 sums using System.Security.Cryptography Assembly: mscorlib (in mscorlib.dll)\r\n"); | |
usage.AppendLine("SHATool [-h | -d | -r | -fd | -fr] source [hash]"); | |
usage.AppendLine(" -h Calculate a SHA256 hash for file"); | |
usage.AppendLine(" -d Calculate a SHA256 hash for all files in a directory"); | |
usage.AppendLine(" -r Calculate a SHA256 hash for all files in a directory recursive"); | |
usage.AppendLine(" -fd Find files that match the supplied SHA256 hash in a directory"); | |
usage.AppendLine(" -fr Find files that match the supplied SHA256 hash in a directory recursive"); | |
usage.AppendLine(" source The directory or file to search"); | |
usage.AppendLine(" hash SHA256 hash to search for"); | |
Console.WriteLine(usage.ToString()); | |
} | |
private static void FileHash(string[] args) | |
{ | |
if (!File.Exists(args[1])) | |
{ | |
Console.WriteLine("The file {0} was not found", args[1]); | |
return; | |
} | |
var hex = HashFile(args[1]); | |
Console.WriteLine("{0}\t{1}", args[1], hex); | |
} | |
private static void DirectoryHash(string[] args, SearchOption searchOption) | |
{ | |
DirectoryHash(args, searchOption, string.Empty); | |
} | |
private static void DirectoryHash(string[] args, SearchOption searchOption, string hashToFind) | |
{ | |
if(!Directory.Exists(args[1])) | |
{ | |
Console.WriteLine("{0} is not a vaild directory", args[1]); | |
return; | |
} | |
// Clean up the user entered hash | |
hashToFind = hashToFind.Trim().Replace("-", "").ToLower(); | |
ProcessFiles(args[1], searchOption, hashToFind); | |
} | |
private static void ProcessFiles(string folder, SearchOption searchOption, string hashToFind) | |
{ | |
try | |
{ | |
foreach (var file in Directory.GetFiles(folder)) | |
{ | |
Console.Title = file; | |
var hex = HashFile(file); | |
if (hex == hashToFind || hashToFind == string.Empty) | |
{ | |
Console.WriteLine("{0}\t{1}", file, hex); | |
} | |
} | |
if (searchOption == SearchOption.AllDirectories) | |
{ | |
foreach (var directory in Directory.GetDirectories(folder)) | |
{ | |
ProcessFiles(directory, searchOption, hashToFind); | |
} | |
} | |
} | |
catch (UnauthorizedAccessException) { /*Ignore*/} // Stuff we do not have access to | |
catch (DirectoryNotFoundException) { /*Ignore*/} // Symbolic links that are invalid | |
} | |
static SHA256 shaM = new SHA256Managed(); | |
private static string HashFile(string file) | |
{ | |
var result = shaM.ComputeHash(File.OpenRead(file)); | |
return BitConverter.ToString(result).Replace("-", "").ToLower(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment