SHA1 C# equivalent of this Java

后端 未结 2 1638
谎友^
谎友^ 2021-01-03 09:53

Looking for the same equivalent to this method in C#

try {
          MessageDigest md = MessageDigest.getInstance(\"SHA-1\");
          md.update(password.ge         


        
相关标签:
2条回答
  • 2021-01-03 10:02

    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");
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-03 10:10

    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.

    0 讨论(0)
提交回复
热议问题