Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for stringhash.cs

(view source code of stringhash.cs as plain text)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6.  
  7.  
  8. namespace StringHash
  9. {
  10. 	class StringHash
  11. 	{
  12. 		static readonly string progver = "1.01";
  13.  
  14.  
  15. 		static int Main( string[] args )
  16. 		{
  17. 			string hash = string.Empty;
  18. 			string str = string.Empty;
  19.  
  20.  
  21. 			#region Command Line Parsing
  22.  
  23. 			if ( args.Contains( "/?" ) )
  24. 			{
  25. 				return ShowHelp( );
  26. 			}
  27.  
  28. 			List<string> arguments = new List<string>( args );
  29.  
  30. 			if ( Console.IsInputRedirected )
  31. 			{
  32. 				arguments.Insert( 0, "/S:" + Console.In.ReadToEnd( ) );
  33. 			}
  34.  
  35. 			if ( arguments.Count != 2 )
  36. 			{
  37. 				return ShowHelp( "Missing or invalid command line arguments" );
  38. 			}
  39.  
  40. 			foreach ( string arg in arguments )
  41. 			{
  42. 				switch ( arg.Substring( 0, 3 ).ToUpper( ) )
  43. 				{
  44. 					case "/A:":
  45. 						if ( !string.IsNullOrWhiteSpace( hash ) )
  46. 						{
  47. 							return ShowHelp( "Duplicate hash algorithm specification" );
  48. 						}
  49. 						hash = arg.Substring( 3 ).ToUpper( );
  50. 						if ( string.IsNullOrWhiteSpace( hash ) )
  51. 						{
  52. 							return ShowHelp( "Missing hash specification" );
  53. 						}
  54. 						break;
  55. 					case "/S:":
  56. 						if ( !string.IsNullOrWhiteSpace( str ) )
  57. 						{
  58. 							return ShowHelp( "Duplicate string specification" );
  59. 						}
  60. 						str = arg.Substring( 3 );
  61. 						if ( string.IsNullOrWhiteSpace( str ) )
  62. 						{
  63. 							return ShowHelp( "Empty string" );
  64. 						}
  65. 						break;
  66. 					default:
  67. 						return ShowHelp( "Invalid command line argument \"{0}\"", arg );
  68. 				}
  69. 			}
  70.  
  71. 			#endregion Command Line Parsing
  72.  
  73.  
  74. 			HashAlgorithm hashalgorithm;
  75. 			switch ( hash )
  76. 			{
  77. 				case "MD5":
  78. 					hashalgorithm = new MD5CryptoServiceProvider( );
  79. 					break;
  80. 				case "SHA1":
  81. 				case "SHA-1":
  82. 					hashalgorithm = new SHA1CryptoServiceProvider( );
  83. 					break;
  84. 				case "SHA256":
  85. 				case "SHA-256":
  86. 					hashalgorithm = new SHA256CryptoServiceProvider( );
  87. 					break;
  88. 				case "SHA384":
  89. 				case "SHA-384":
  90. 					hashalgorithm = new SHA384CryptoServiceProvider( );
  91. 					break;
  92. 				case "SHA512":
  93. 				case "SHA-512":
  94. 					hashalgorithm = new SHA512CryptoServiceProvider( );
  95. 					break;
  96. 				default:
  97. 					return ShowHelp( "Invalid hash type \"{0}\"", hash );
  98. 			}
  99. 			string result = BitConverter.ToString( hashalgorithm.ComputeHash( StrToByteArray( str ) ) );
  100. 			hashalgorithm.Clear( );
  101.  
  102. 			StringBuilder sb = new StringBuilder( result.ToLowerInvariant( ) );
  103. 			Console.WriteLine( sb.Replace( "-", "" ) );
  104.  
  105. 			return 0;
  106. 		}
  107.  
  108.  
  109. 		// C# to convert a string to a byte array
  110. 		// http://www.chilkatsoft.com/faq/dotnetstrtobytes.html
  111. 		public static byte[] StrToByteArray( string instring )
  112. 		{
  113. 			ASCIIEncoding encoding = new ASCIIEncoding( );
  114. 			return encoding.GetBytes( instring );
  115. 		}
  116.  
  117.  
  118. 		public static int ShowHelp( params string[] errmsg )
  119. 		{
  120. 			#region Error Message
  121.  
  122. 			if ( errmsg.Length > 0 )
  123. 			{
  124. 				List<string> errargs = new List<string>( errmsg );
  125. 				errargs.RemoveAt( 0 );
  126. 				Console.Error.WriteLine( );
  127. 				Console.ForegroundColor = ConsoleColor.Red;
  128. 				Console.Error.Write( "ERROR:\t" );
  129. 				Console.ForegroundColor = ConsoleColor.White;
  130. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  131. 				Console.ResetColor( );
  132. 			}
  133.  
  134. 			#endregion Error Message
  135.  
  136. 			/*			
  137. 			StringHash.exe,  Version 1.01
  138. 			Get the MD5 or SHA* hash value for the specified string
  139.  
  140. 			Usage:  STRINGHASH.EXE  /A:hashAlgorithm  /S:"string"
  141.  
  142. 			   or:  some_command  |  STRINGHASH.EXE  /A:hashAlgorithm
  143.  
  144. 			Where:  hashAlgorithm  is either MD5, SHA1, SHA256, SHA384 or SHA512
  145. 					string         must be enclosed in doublequotes if it contains
  146. 			                       spaces or special characters
  147. 			        some_command   console program whose output serves as the string
  148.  
  149. 			Note:   Return code ("ErrorLevel") -1 in case of errors, otherwise 0.
  150.  
  151. 			Written by Rob van der Woude
  152. 			http://www.robvanderwoude.com
  153. 			*/
  154.  
  155. 			#region Display Help Text
  156.  
  157. 			Console.Error.WriteLine( );
  158.  
  159. 			Console.Error.WriteLine( "StringHash.exe,  Version {0}", progver );
  160.  
  161. 			Console.Error.WriteLine( "Get the MD5 or SHA* hash value for the specified string" );
  162.  
  163. 			Console.Error.WriteLine( );
  164.  
  165. 			Console.Error.WriteLine( "Usage:  STRINGHASH.EXE  /A:hashAlgorithm  /S:\"string\"" );
  166.  
  167. 			Console.Error.WriteLine( );
  168.  
  169. 			Console.Error.WriteLine( "   or:  some_command  |  STRINGHASH.EXE  /A:hashAlgorithm" );
  170.  
  171. 			Console.Error.WriteLine( );
  172.  
  173. 			Console.Error.WriteLine( "Where:  hashAlgorithm  is either MD5, SHA1, SHA256, SHA384 or SHA512" );
  174.  
  175. 			Console.Error.WriteLine( "        string         must be enclosed in doublequotes if it contains" );
  176.  
  177. 			Console.Error.WriteLine( "                       spaces or special characters" );
  178.  
  179. 			Console.Error.WriteLine( "        some_command   console program whose output serves as the string" );
  180.  
  181. 			Console.Error.WriteLine( );
  182.  
  183. 			Console.Error.WriteLine( "Note:   Return code (\"ErrorLevel\") -1 in case of errors, otherwise 0." );
  184.  
  185. 			Console.Error.WriteLine( );
  186.  
  187. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  188.  
  189. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  190.  
  191. 			#endregion Display Help Text
  192.  
  193.  
  194. 			return -1;
  195. 		}
  196. 	}
  197. }

page last modified: 2024-04-16; loaded in 0.0079 seconds