如何在 C# 中对字符串进行 SHA512 处理? [英] How can I SHA512 a string in C#?

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

问题描述

我正在尝试编写一个函数来获取一个字符串并像这样 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;
}

魔法应该是什么?

推荐答案

你的代码是正确的,但你应该处理 SHA512Managed 实例:

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

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

512 位是 64 字节.

512 bits are 64 bytes.

要将字符串转换为字节数组,需要指定编码.如果你想创建一个哈希码,UTF8 是可以的:

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

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

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