SHA1 C# equivalent of this Java

做~自己de王妃 提交于 2020-01-10 20:00:26

问题


Looking for the same equivalent to this method in 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;

回答1:


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



回答2:


Have a look at Sha1CryptoServiceProvider. It provides a good amount of flexibility. Like most of the algorithms in System.Security.Cryptography, it provides methods for handling byte arrays and streams.



来源:https://stackoverflow.com/questions/4819794/sha1-c-sharp-equivalent-of-this-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!