How can I SHA512 a string in C#?

后端 未结 10 1289
伪装坚强ぢ
伪装坚强ぢ 2020-12-08 18:35

I am trying to write a function to take a string and sha512 it like so?

public string SHA512(string input)
{
     string hash;

     ~magic~

     return has         


        
10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-08 19:14

    Your code is correct, but you should dispose of the SHA512Managed instance:

    using (SHA512 shaM = new SHA512Managed())
    {
       hash = shaM.ComputeHash(data);
    }
    

    512 bits are 64 bytes.

    To convert a string to a byte array, you need to specify an encoding. UTF8 is okay if you want to create a hash code:

    var data = Encoding.UTF8.GetBytes("text");    
    using (...
    

提交回复
热议问题