我怎样才能SHA512在C#中的字符串? [英] How can I SHA512 a string in C#?

查看:400
本文介绍了我怎样才能SHA512在C#中的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个函数取一个字符串,SHA512它像这样?

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 hash;
}

应该是什么魔力呢?

What should the magic be?

P.S。
我试过以下,但保存只要64字节结束了(而不是128如预期)。

P.S. I tried the following, but hash kept ending up as 64bytes long (not 128 as expected).

byte[] data = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
byte[] hash;
SHA512 shaM = new SHA512Managed();
hash = shaM.ComputeHash(data);

在此先感谢!

推荐答案

您code是正确的,但你应该处置SHA512Managed例如:

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

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

512位是64个字节。

512 bits are 64 bytes.

做一个字符串转换为一个字节数组,你需要指定的编码。 UTF8是好的,如果你想创建一个哈希code:

Do 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 (...

这篇关于我怎样才能SHA512在C#中的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆