c# 将字符串转换sha-1哈希值。
时间: 2024-02-28 08:54:27 浏览: 28
在 C# 中,可以使用 System.Security.Cryptography 命名空间中的 SHA1 类来计算字符串的 SHA-1 哈希值。下面是一个示例代码:
```csharp
using System;
using System.Security.Cryptography;
using System.Text;
class Program
{
static void Main(string[] args)
{
string input = "hello world";
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
using (SHA1 sha1 = SHA1.Create())
{
byte[] hashBytes = sha1.ComputeHash(inputBytes);
string hashString = BitConverter.ToString(hashBytes).Replace("-", "");
Console.WriteLine("SHA1 hash of '{0}': {1}", input, hashString);
}
}
}
```
这段代码将字符串 "hello world" 转换为 UTF-8 编码的字节数组,然后使用 SHA1 类的 ComputeHash 方法计算哈希值。最后,使用 BitConverter 和 Replace 方法将哈希值转换为十六进制字符串。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)