Last active
January 6, 2017 18:47
-
-
Save hassanselim0/d512a91e7a8eec4fe048 to your computer and use it in GitHub Desktop.
C# Command Line Script for hashing strings and files
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.Security.Cryptography; | |
var helpStr = @" | |
Hash Helper Tool! | |
Example Usage: | |
scriptcs Hash.csx -- SHA256 -s ""Example String"" | |
scriptcs Hash.csx -- SHA384 -f ""path/to/file"" | |
scriptcs Hash.csx -- SHA512 -i | |
Accepted Hash Algorithms: MD5, SHA1, SHA256, SHA384, SHA512. | |
"; | |
try | |
{ | |
if (Env.ScriptArgs.Count == 0) | |
{ | |
Console.WriteLine("Missing Arguments!"); | |
Console.WriteLine(helpStr); | |
Environment.Exit(0); | |
} | |
var algo = Env.ScriptArgs[0]; | |
var mode = Env.ScriptArgs.Count == 1 ? "-i" : Env.ScriptArgs[1]; | |
var hash = new byte[0]; | |
var algoInst = HashAlgorithm.Create(algo); | |
if (algoInst == null) | |
{ | |
Console.WriteLine("Invalid Hash Algorithm: " + algo); | |
Console.WriteLine(helpStr); | |
Environment.Exit(0); | |
} | |
var trim = false; | |
if (mode.Length == 3 && mode[2] == 't') | |
{ | |
trim = true; | |
mode = mode.Substring(0, 2); | |
} | |
string input = ""; | |
switch (mode) | |
{ | |
case "-f": | |
Console.WriteLine("Computing " + algo + " Hash for file: " + Env.ScriptArgs[2]); | |
if (trim) | |
hash = algoInst.ComputeHash(Encoding.UTF8.GetBytes(File.ReadAllText(Env.ScriptArgs[2]).Trim())); | |
else | |
hash = algoInst.ComputeHash(File.OpenRead(Env.ScriptArgs[2])); | |
break; | |
case "-s": | |
Console.WriteLine("Computing " + algo + " Hash for string: " + Env.ScriptArgs[2]); | |
input = Env.ScriptArgs[2]; | |
if (trim) input = input.Trim(); | |
hash = algoInst.ComputeHash(Encoding.UTF8.GetBytes(input)); | |
break; | |
case "-i": | |
Console.WriteLine("Computing " + algo + " Hash from standard input"); | |
input = Console.In.ReadToEnd(); | |
if (trim) input = input.Trim(); | |
hash = algoInst.ComputeHash(Encoding.UTF8.GetBytes(input)); | |
break; | |
default: | |
Console.WriteLine("Invalid Mode: " + mode); | |
Console.WriteLine(helpStr); | |
Environment.Exit(0); | |
break; | |
} | |
var hexStr = BitConverter.ToString(hash).Replace("-", ""); | |
var b64Str = Convert.ToBase64String(hash); | |
Console.ForegroundColor = ConsoleColor.Green; | |
Console.Write("Hex: "); | |
Console.ForegroundColor = ConsoleColor.Yellow; | |
Console.WriteLine(hexStr); | |
Console.ForegroundColor = ConsoleColor.Green; | |
Console.Write("B64: "); | |
Console.ForegroundColor = ConsoleColor.Yellow; | |
Console.WriteLine(b64Str); | |
Console.ResetColor(); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex); | |
Console.WriteLine(helpStr); | |
} | |
/* | |
To turn this into a command line tool, put the following in a hash.bat file anywhere in your PATH: | |
@echo off | |
scriptcs "path/to/Hash.csx" -- %* | |
That way you can just type: | |
hash SHA256 -s "Example String" | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment