SHA1 C#相当于这个Java

时间:2011-01-27 17:35:31

标签: c# java sha1

在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;

2 个答案:

答案 0 :(得分:3)

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");
        }
    }
}

答案 1 :(得分:1)

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