Created
May 8, 2017 22:06
-
-
Save Lomeli12/82d46b2f73eb6b616985da332ff7aca4 to your computer and use it in GitHub Desktop.
Hasher Source
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.IO; | |
using System.Text; | |
using System.Reflection; | |
using System.Diagnostics; | |
using System.Windows.Forms; | |
using System.Collections.Generic; | |
using System.Security.Cryptography; | |
namespace hasher { | |
class Program { | |
static List<char> invalidChars = new List<char>(); | |
static readonly string usageFormat = " {0}\n {1}\n"; | |
static readonly string version = "Hasher version " + FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion; | |
static readonly string usage = "usage: hasher [--version] [-c | --clipboard] [? | --help] [-h | --hash <MD5 | SHA1 | SHA256 | SHA384 | SHA512>] [-f | --file] [-v | --verbose] <Thing to hash>\n"; | |
static bool verbose; | |
static bool help; | |
static bool askVersion; | |
static bool file; | |
static bool copy; | |
static string thingToHash; | |
enum LogType { INFO, WARN, ERROR}; | |
enum HashType { MD5, SHA1, SHA256, SHA384, SHA512, NONE }; | |
static HashType hashType; | |
static HashAlgorithm algorithm; | |
[STAThread] | |
public static void Main(string[] args) { | |
hashType = HashType.MD5; | |
invalidChars.AddRange(Path.GetInvalidFileNameChars()); | |
if (args == null || args.Length < 1) { | |
Console.WriteLine("No value or file given!"); | |
return; | |
} | |
try { | |
processArguments(args); | |
} catch (Exception ex) { | |
Console.WriteLine(ex.Message); | |
} | |
if (askVersion) { | |
Console.WriteLine(version); | |
return; | |
} | |
if (help) { | |
Console.WriteLine(usage); | |
Console.Write(string.Format(usageFormat, "-h, --hash", "Specify hashing algorithm. Accepts MD5, SHA1, SHA256, SHA384, and SHA512.")); | |
Console.Write(string.Format(usageFormat, "-f, --file", "Specify that a file path is being given.")); | |
Console.Write(string.Format(usageFormat, "-c, --clipboard", "Copy hash to clipboard once calculated.")); | |
Console.Write(string.Format(usageFormat, "-v, --verbose", "Give detailed log of every change.")); | |
Console.Write(string.Format(usageFormat, "--version", "Get version.")); | |
Console.Write(string.Format(usageFormat, "?, --help", "Display help and options")); | |
} | |
if (string.IsNullOrEmpty(thingToHash)) { | |
Console.WriteLine("No value or file given!"); | |
return; | |
} | |
cleanThingToHash(); | |
algorithm = getHashAlgorithm(); | |
string outHash; | |
if (file) { | |
if (!File.Exists(thingToHash)) { | |
Console.WriteLine("{0} doesn't exist!", thingToHash); | |
return; | |
} | |
log(LogType.INFO, "Calculating {0} hash of {1}", hashType, thingToHash); | |
using (var stream = File.OpenRead(thingToHash)) { | |
outHash = hashBytesToString(algorithm.ComputeHash(stream)); | |
} | |
} else { | |
log(LogType.INFO, "Calculating {0} hash of {1}", hashType, thingToHash); | |
outHash = hashBytesToString(algorithm.ComputeHash(Encoding.UTF8.GetBytes(thingToHash))); | |
} | |
if (!string.IsNullOrWhiteSpace(outHash)) { | |
Console.WriteLine(string.Format("{0} Hash: {1}", hashType, outHash)); | |
if (copy) { | |
log(LogType.INFO, "Copying hash to clipboard"); | |
Clipboard.SetText(outHash); | |
} | |
} else Console.WriteLine("Nothing has hashed..."); | |
} | |
static string hashBytesToString(byte[] bytes) { | |
log(LogType.INFO, "Converting hash bytes to string"); | |
var hashsb = new StringBuilder(); | |
foreach(byte b in bytes) hashsb.Append(b.ToString("X2")); | |
return hashsb.ToString(); | |
} | |
static HashAlgorithm getHashAlgorithm() { | |
switch (hashType) { | |
case HashType.SHA1: return SHA1.Create(); | |
case HashType.SHA256: return SHA256.Create(); | |
case HashType.SHA384: return SHA384.Create(); | |
case HashType.SHA512: return SHA512.Create(); | |
default: return MD5.Create(); | |
} | |
} | |
static void cleanThingToHash() { | |
if (!file) return; | |
log(LogType.INFO, "Removing any invalid characters from given path"); | |
if (thingToHash.EndsWith("" + Path.DirectorySeparatorChar, StringComparison.CurrentCultureIgnoreCase) || | |
thingToHash.EndsWith("" + Path.AltDirectorySeparatorChar, StringComparison.CurrentCultureIgnoreCase) || | |
thingToHash.EndsWith("\"", StringComparison.CurrentCultureIgnoreCase) || | |
thingToHash.EndsWith("/", StringComparison.CurrentCultureIgnoreCase)) | |
thingToHash = thingToHash.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar, '"'); | |
} | |
static string stripInvalidChars(string original) { | |
var newName = ""; | |
if (!string.IsNullOrWhiteSpace(original)) { | |
foreach(char c in original) | |
if (!invalidChars.Contains(c)) newName += c; | |
} | |
return newName; | |
} | |
static void processArguments(string[] args){ | |
for (int i = 0; i < args.Length; i++) { | |
var arg = args[i]; | |
if (arg.Equals("-h", StringComparison.CurrentCultureIgnoreCase) || arg.Equals("--hash", StringComparison.CurrentCultureIgnoreCase)) { | |
if ((i + 1) < args.Length) { | |
hashType = stringToHash(args[i + 1]); | |
if (hashType == HashType.NONE) throw new ArgumentException("Hash algorithm not specified!"); | |
log(LogType.INFO, "Setting hashing algorithm to {0}", hashType); | |
i++; | |
} else | |
throw new ArgumentException("File directory!"); | |
} else if (arg.Equals("-f", StringComparison.CurrentCultureIgnoreCase) || arg.Equals("--file", StringComparison.CurrentCultureIgnoreCase)) { | |
log(LogType.INFO, "Will look for file."); | |
file = true; | |
} else if (!copy && (arg.Equals("-c", StringComparison.CurrentCultureIgnoreCase) || arg.Equals("--clipboard", StringComparison.CurrentCultureIgnoreCase))) { | |
log(LogType.INFO, "Will copy hash to clipboard"); | |
copy = true; | |
} else if (!verbose && (arg.Equals("-v", StringComparison.CurrentCultureIgnoreCase) || arg.Equals("--verbose", StringComparison.CurrentCultureIgnoreCase))) { | |
verbose = true; | |
} else if (i == 0 && !help && (arg.Equals("?", StringComparison.CurrentCultureIgnoreCase) || arg.Equals("--help", StringComparison.CurrentCultureIgnoreCase))) { | |
help = true; | |
return; | |
} else if (arg.Equals("--version", StringComparison.CurrentCultureIgnoreCase)) { | |
askVersion = true; | |
return; | |
} else | |
thingToHash = arg; | |
} | |
} | |
static void log(LogType type, Object msg, params Object[] args) { | |
if (!verbose) | |
return; | |
var outmsg = msg.ToString(); | |
if (args != null && args.Length > 0) | |
outmsg = string.Format(outmsg, args); | |
Console.WriteLine(string.Format("[{0}]: {1}", type.ToString(), outmsg)); | |
} | |
static HashType stringToHash(string value) { | |
if (!string.IsNullOrWhiteSpace(value)) { | |
if (value.Equals("MD5", StringComparison.CurrentCultureIgnoreCase)) return HashType.MD5; | |
if (value.Equals("SHA1", StringComparison.CurrentCultureIgnoreCase)) return HashType.SHA1; | |
if (value.Equals("SHA256", StringComparison.CurrentCultureIgnoreCase)) return HashType.SHA256; | |
if (value.Equals("SHA384", StringComparison.CurrentCultureIgnoreCase)) return HashType.SHA384; | |
if (value.Equals("SHA512", StringComparison.CurrentCultureIgnoreCase)) return HashType.SHA512; | |
} | |
return HashType.NONE; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment