返回介绍

使用哈希代码确保数据完整性

发布于 2025-02-23 23:16:21 字数 5675 浏览 0 评论 0 收藏 0

哈希值是用于唯一标识数据的固定长度的数字值。 哈希值以小得多的数字值表示大量数据,因此与数字签名配合使用。 对哈希值进行签名比对较大的值进行签名更为高效。 对于验证通过不安全通道发送的数据的完整性,哈希值也很有用。 当被发送出去确定数据是否已更改时,将接收数据的哈希值与数据的哈希值相比较。

本主题介绍如何通过使用 System.Security.Cryptography 命名空间中的类生成和验证哈希代码。

生成哈希

托管哈希类可以对字节数组或托管流对象进行哈希处理。 以下示例使用 SHA1 哈希算法为字符串创建哈希值。 该示例使用 UnicodeEncoding 类将字符串转换为通过使用 SHA1Managed 类进行哈希处理的字节数组。 然后向控制台显示哈希值。

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

class Class1
{
  static void Main(string[] args)
  {
    byte[] HashValue;

    string MessageString = "This is the original message!";

    //Create a new instance of the UnicodeEncoding class to 
    //convert the string into an array of Unicode bytes.
    UnicodeEncoding UE = new UnicodeEncoding();

    //Convert the string into an array of bytes.
    byte[] MessageBytes = UE.GetBytes(MessageString);

    //Create a new instance of the SHA1Managed class to create 
    //the hash value.
    SHA1Managed SHhash = new SHA1Managed();

    //Create the hash value from the array of bytes.
    HashValue = SHhash.ComputeHash(MessageBytes);

    //Display the hash value to the console. 
    foreach (byte b in HashValue)
    {
      Console.Write("{0} ", b);
    }
  }
}
Imports System
Imports System.Security.Cryptography
Imports System.Text

Module Program
  Sub Main()
    Dim HashValue() As Byte

    Dim MessageString As String = "This is the original message!"

    'Create a new instance of the UnicodeEncoding class to 
    'convert the string into an array of Unicode bytes.
    Dim UE As New UnicodeEncoding()

    'Convert the string into an array of bytes.
    Dim MessageBytes As Byte() = UE.GetBytes(MessageString)

    'Create a new instance of the SHA1Managed class to create 
    'the hash value.
    Dim SHhash As New SHA1Managed()

    'Create the hash value from the array of bytes.
    HashValue = SHhash.ComputeHash(MessageBytes)

    'Display the hash value to the console. 
    Dim b As Byte
    For Each b In HashValue
      Console.Write("{0} ", b)
    Next b
  End Sub
End Module

此代码将向控制台显示以下字符串:

59 4 248 102 77 97 142 201 210 12 224 93 25 41 100 197 213 134 130 135

验证哈希

可将数据与哈希值进行比较,以确定其完整性。 通常,在某个特定时间对数据进行哈希运算,并以某种方式保护哈希值。 稍后,可以再次对数据进行哈希运算,并与受保护的值进行比较。 如果哈希值匹配,则数据未更改。 如果值不匹配,则数据已损坏。 要使此系统发挥作用,必须对受保护的哈希加密或对所有的不受信任方保密。

以下示例将字符串旧的哈希值与新的哈希值进行比较。 此示例循环访问哈希值的每个字节并进行比较。

using System;
using System.Security.Cryptography;
using System.Text;

class Class1
{
  static void Main()
  {
    //This hash value is produced from "This is the original message!" 
    //using SHA1Managed.  
    byte[] SentHashValue = { 59, 4, 248, 102, 77, 97, 142, 201, 210, 12, 224, 93, 25, 41, 100, 197, 213, 134, 130, 135 };

    //This is the string that corresponds to the previous hash value.
    string MessageString = "This is the original message!";

    byte[] CompareHashValue;

    //Create a new instance of the UnicodeEncoding class to 
    //convert the string into an array of Unicode bytes.
    UnicodeEncoding UE = new UnicodeEncoding();

    //Convert the string into an array of bytes.
    byte[] MessageBytes = UE.GetBytes(MessageString);

    //Create a new instance of the SHA1Managed class to create 
    //the hash value.
    SHA1Managed SHhash = new SHA1Managed();

    //Create the hash value from the array of bytes.
    CompareHashValue = SHhash.ComputeHash(MessageBytes);

    bool Same = true;

    //Compare the values of the two byte arrays.
    for (int x = 0; x < SentHashValue.Length; x++)
    {
      if (SentHashValue[x] != CompareHashValue[x])
      {
        Same = false;
      }
    }
    //Display whether or not the hash values are the same.
    if (Same)
    {
      Console.WriteLine("The hash codes match.");
    }
    else
    {
      Console.WriteLine("The hash codes do not match.");
    }
  }
}
Imports System
Imports System.Security.Cryptography
Imports System.Text

Module Module1
  Sub Main()
    'This hash value is produced from "This is the original message!" 
    'using SHA1Managed.  
    Dim SentHashValue As Byte() = {59, 4, 248, 102, 77, 97, 142, 201, 210, 12, 224, 93, 25, 41, 100, 197, 213, 134, 130, 135}

    'This is the string that corresponds to the previous hash value.
    Dim MessageString As String = "This is the original message!"

    Dim CompareHashValue() As Byte

    'Create a new instance of the UnicodeEncoding class to 
    'convert the string into an array of Unicode bytes.
    Dim UE As New UnicodeEncoding()

    'Convert the string into an array of bytes.
    Dim MessageBytes As Byte() = UE.GetBytes(MessageString)

    'Create a new instance of the SHA1Managed class to create 
    'the hash value.
    Dim SHhash As New SHA1Managed()

    'Create the hash value from the array of bytes.
    CompareHashValue = SHhash.ComputeHash(MessageBytes)

    Dim Same As Boolean = True

    'Compare the values of the two byte arrays.
    Dim x As Integer
    For x = 0 To SentHashValue.Length - 1
      If SentHashValue(x) <> CompareHashValue(x) Then
        Same = False
      End If
    Next x
    'Display whether or not the hash values are the same.
    If Same Then
      Console.WriteLine("The hash codes match.")
    Else
      Console.WriteLine("The hash codes do not match.")
    End If
  End Sub
End Module

如果两个哈希值匹配,则此代码将向控制台现实以下内容:

The hash codes match.  

如果不匹配,则此代码显示以下信息:

The hash codes do not match.  

另请参阅

Cryptographic Services

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文