(view source code of stringhash.cs as plain text)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace StringHash
{
class StringHash
{
static readonly string progver = "1.01";
static int Main( string[] args )
{
string hash = string.Empty;
string str = string.Empty;
#region Command Line Parsing
if ( args.Contains( "/?" ) )
{
return ShowHelp( );
}
List<string> arguments = new List<string>( args );
if ( Console.IsInputRedirected )
{
arguments.Insert( 0, "/S:" + Console.In.ReadToEnd( ) );
}
if ( arguments.Count != 2 )
{
return ShowHelp( "Missing or invalid command line arguments" );
}
foreach ( string arg in arguments )
{
switch ( arg.Substring( 0, 3 ).ToUpper( ) )
{
case "/A:":
if ( !string.IsNullOrWhiteSpace( hash ) )
{
return ShowHelp( "Duplicate hash algorithm specification" );
}
hash = arg.Substring( 3 ).ToUpper( );
if ( string.IsNullOrWhiteSpace( hash ) )
{
return ShowHelp( "Missing hash specification" );
}
break;
case "/S:":
if ( !string.IsNullOrWhiteSpace( str ) )
{
return ShowHelp( "Duplicate string specification" );
}
str = arg.Substring( 3 );
if ( string.IsNullOrWhiteSpace( str ) )
{
return ShowHelp( "Empty string" );
}
break;
default:
return ShowHelp( "Invalid command line argument \"{0}\"", arg );
}
}
#endregion Command Line Parsing
HashAlgorithm hashalgorithm;
switch ( hash )
{
case "MD5":
hashalgorithm = new MD5CryptoServiceProvider( );
break;
case "SHA1":
case "SHA-1":
hashalgorithm = new SHA1CryptoServiceProvider( );
break;
case "SHA256":
case "SHA-256":
hashalgorithm = new SHA256CryptoServiceProvider( );
break;
case "SHA384":
case "SHA-384":
hashalgorithm = new SHA384CryptoServiceProvider( );
break;
case "SHA512":
case "SHA-512":
hashalgorithm = new SHA512CryptoServiceProvider( );
break;
default:
return ShowHelp( "Invalid hash type \"{0}\"", hash );
}
string result = BitConverter.ToString( hashalgorithm.ComputeHash( StrToByteArray( str ) ) );
hashalgorithm.Clear( );
StringBuilder sb = new StringBuilder( result.ToLowerInvariant( ) );
Console.WriteLine( sb.Replace( "-", "" ) );
return 0;
}
// C# to convert a string to a byte array
// http://www.chilkatsoft.com/faq/dotnetstrtobytes.html
public static byte[] StrToByteArray( string instring )
{
ASCIIEncoding encoding = new ASCIIEncoding( );
return encoding.GetBytes( instring );
}
public static int ShowHelp( params string[] errmsg )
{
#region Error Message
if ( errmsg.Length > 0 )
{
List<string> errargs = new List<string>( errmsg );
errargs.RemoveAt( 0 );
Console.Error.WriteLine( );
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.Write( "ERROR:\t" );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
Console.ResetColor( );
}
#endregion Error Message
/*
StringHash.exe, Version 1.01
Get the MD5 or SHA* hash value for the specified string
Usage: STRINGHASH.EXE /A:hashAlgorithm /S:"string"
or: some_command | STRINGHASH.EXE /A:hashAlgorithm
Where: hashAlgorithm is either MD5, SHA1, SHA256, SHA384 or SHA512
string must be enclosed in doublequotes if it contains
spaces or special characters
some_command console program whose output serves as the string
Note: Return code ("ErrorLevel") -1 in case of errors, otherwise 0.
Written by Rob van der Woude
http://www.robvanderwoude.com
*/
#region Display Help Text
Console.Error.WriteLine( );
Console.Error.WriteLine( "StringHash.exe, Version {0}", progver );
Console.Error.WriteLine( "Get the MD5 or SHA* hash value for the specified string" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Usage: STRINGHASH.EXE /A:hashAlgorithm /S:\"string\"" );
Console.Error.WriteLine( );
Console.Error.WriteLine( " or: some_command | STRINGHASH.EXE /A:hashAlgorithm" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Where: hashAlgorithm is either MD5, SHA1, SHA256, SHA384 or SHA512" );
Console.Error.WriteLine( " string must be enclosed in doublequotes if it contains" );
Console.Error.WriteLine( " spaces or special characters" );
Console.Error.WriteLine( " some_command console program whose output serves as the string" );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Note: Return code (\"ErrorLevel\") -1 in case of errors, otherwise 0." );
Console.Error.WriteLine( );
Console.Error.WriteLine( "Written by Rob van der Woude" );
Console.Error.WriteLine( "https://www.robvanderwoude.com" );
#endregion Display Help Text
return -1;
}
}
}
page last modified: 2024-04-16; loaded in 0.0079 seconds