【问题标题】:SHA1 C# equivalent of this Java此 Java 的 SHA1 C# 等效项
【发布时间】:2011-06-16 17:29:17
【问题描述】:

在 C# 中寻找与此方法相同的等效项

try {
          MessageDigest md = MessageDigest.getInstance("SHA-1");
          md.update(password.getBytes());
          BigInteger hash = new BigInteger(1, md.digest());
          hashword = hash.toString(16);
      } catch (NoSuchAlgorithmException ex) {
          }
}
return hashword;

【问题讨论】:

    标签: c# java sha1


    【解决方案1】:

    看看Sha1CryptoServiceProvider。它提供了很好的灵活性。与System.Security.Cryptography 中的大多数算法一样,它提供了处理字节数组和流的方法。

    【讨论】:

      【解决方案2】:

      在 C# 中超级简单:

      using System;
      using System.Text;
      using System.Security.Cryptography;
      
      namespace CSharpSandbox
      {
          class Program
          {
              public static string HashPassword(string input)
              {
                  var sha1 = SHA1Managed.Create();
                  byte[] inputBytes = Encoding.ASCII.GetBytes(input);
                  byte[] outputBytes = sha1.ComputeHash(inputBytes);
                  return BitConverter.ToString(outputBytes).Replace("-", "").ToLower();
              }
      
              public static void Main(string[] args)
              {
                  string output = HashPassword("The quick brown fox jumps over the lazy dog");
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-15
        • 2010-09-22
        • 1970-01-01
        相关资源
        最近更新 更多