C#获取指定字符串的MD5和SHA*加密代码

From , 3 Years ago, written in C#, viewed 104 times.
URL https://pastebin.vip/view/251e16a2
  1. using System;
  2. using System.Text;
  3. using System.Security.Cryptography;
  4.  
  5. namespace StringHash
  6. {
  7.         class StringHash
  8.         {
  9.                 static int Main(string[] args)
  10.                 {
  11.                         try
  12.                         {
  13.                                 string hash = string.Empty;
  14.                                 string str = string.Empty;
  15.                                 string result;
  16.  
  17.                                 if (args.Length != 2)
  18.                                 {
  19.                                         return WriteError("Missing or invalid parameters");
  20.                                 }
  21.  
  22.                                 foreach (string arg in args)
  23.                                 {
  24.                                         switch (arg.Substring(0, 3).ToUpper())
  25.                                         {
  26.                                                 case "/A:":
  27.                                                 case "/H:":
  28.                                                         hash = arg.Substring(3).ToUpper();
  29.                                                         break;
  30.                                                 case "/S:":
  31.                                                         str = arg.Substring(3);
  32.                                                         break;
  33.                                                 default:
  34.                                                         return WriteError("Invalid parameter: " + arg);
  35.                                         }
  36.                                 }
  37.  
  38.                                 if (string.IsNullOrEmpty(hash) || string.IsNullOrEmpty(str))
  39.                                 {
  40.                                         return WriteError("Missing required parameters");
  41.                                 }
  42.  
  43.                                 HashAlgorithm ha;
  44.                                 switch (hash)
  45.                                 {
  46.                                         case "MD5":
  47.                                                 ha = new MD5CryptoServiceProvider();
  48.                                                 break;
  49.                                         case "SHA1":
  50.                                         case "SHA-1":
  51.                                                 ha = new SHA1CryptoServiceProvider();
  52.                                                 break;
  53.                                         case "SHA256":
  54.                                         case "SHA-256":
  55.                                                 ha = new SHA256CryptoServiceProvider();
  56.                                                 break;
  57.                                         case "SHA384":
  58.                                         case "SHA-384":
  59.                                                 ha = new SHA384CryptoServiceProvider();
  60.                                                 break;
  61.                                         case "SHA512":
  62.                                         case "SHA-512":
  63.                                                 ha = new SHA512CryptoServiceProvider();
  64.                                                 break;
  65.                                         default:
  66.                                                 return WriteError("Invalid hash type");
  67.                                 }
  68.                                 result = BitConverter.ToString(ha.ComputeHash(StrToByteArray(str)));
  69.                                 ha.Clear();
  70.  
  71.                                 StringBuilder sb = new StringBuilder(result.ToLowerInvariant());
  72.                                 Console.OpenStandardOutput();
  73.                                 Console.WriteLine(sb.Replace("-", ""));
  74.  
  75.                                 return 0;
  76.                         }
  77.                         catch (Exception e)
  78.                         {
  79.                                 return WriteError(e);
  80.                         }
  81.                 }
  82.  
  83.  
  84.                 // C# to convert a string to a byte array
  85.                 // http://www.chilkatsoft.com/faq/dotnetstrtobytes.html
  86.                 public static byte[] StrToByteArray(string instring)
  87.                 {
  88.                         System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
  89.                         return encoding.GetBytes(instring);
  90.                 }
  91.  
  92.  
  93.                 public static int WriteError(Exception e)
  94.                 {
  95.                         return WriteError(e == null ? null : e.Message);
  96.                 }
  97.  
  98.  
  99.                 public static int WriteError(string errorMessage)
  100.                 {
  101.                         Console.OpenStandardError();
  102.                         if (string.IsNullOrEmpty(errorMessage) == false)
  103.                         {
  104.                                 Console.WriteLine();
  105.                                 Console.WriteLine("ERROR: {0}", errorMessage);
  106.                         }
  107.                         Console.WriteLine();
  108.                         Console.WriteLine("StringHash,  Version 1.00");
  109.                         Console.WriteLine("Get the MD5 or SHA* hash value for the specified string");
  110.                         Console.WriteLine();
  111.                         Console.WriteLine("Usage:  STRINGHASH  /A:hashAlgorithm  /S:\"string\"");
  112.                         Console.WriteLine();
  113.                         Console.WriteLine("Where:  hashAlgorithm  is either MD5, SHA1, SHA256, SHA384 or SHA512");
  114.                         Console.WriteLine("        string         must be enclosed in doublequotes if it contains spaces");
  115.                         Console.WriteLine();
  116.                         Console.WriteLine("Written by Rob van der Woude");
  117.                         Console.WriteLine("http://www.robvanderwoude.com");
  118.                         Console.OpenStandardOutput();
  119.                         return 1;
  120.                 }
  121.         }
  122. }
  123. //csharp/7351

Reply to "C#获取指定字符串的MD5和SHA*加密代码"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website