using System; using System.Text; using System.Security.Cryptography; namespace StringHash { class StringHash { static int Main(string[] args) { try { string hash = "MD5"; string str = "onitroad"; string result; HashAlgorithm ha; switch (hash) { case "MD5": // 在c#中计算字符串的md5值 ha = new MD5CryptoServiceProvider(); break; case "SHA1": case "SHA-1": // 在c#中计算字符串的sha值 ha = new SHA1CryptoServiceProvider(); break; case "SHA256": case "SHA-256": // 在c#中计算字符串的sha256值 ha = new SHA256CryptoServiceProvider(); break; case "SHA384": case "SHA-384": ha = new SHA384CryptoServiceProvider(); break; case "SHA512": case "SHA-512": ha = new SHA512CryptoServiceProvider(); break; default: return 1; } result = BitConverter.ToString(ha.ComputeHash(StrToByteArray(str))); ha.Clear(); StringBuilder sb = new StringBuilder(result.ToLowerInvariant()); Console.OpenStandardOutput(); Console.WriteLine(sb.Replace("-", "")); return 0; } catch (Exception e) { } return 0; } // C# 將字符串转换为字节数组 public static byte[] StrToByteArray(string instring) { System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); return encoding.GetBytes(instring); } } }
日期:2020-04-11 22:50:21 来源:oir作者:oir