如何"sign"一个大字符串以后才能识别?

How to "sign" a big string to be identified later?

我建立了一个系统来让两个系统相互通信。 系统 A 将消息 M 放入数据库中,系统 B 处理该消息并将消息 M' 放入数据库中。 系统A是"subscribed"到M'。 对于 "sign" 这条消息(一个大的 xml 文件),我使用哈希码,所以我可以理解消息 M' = M。 这适用于大多数邮件,但其他邮件无法正常工作(我在记事本中打开了两个文件 M 和 M',它们是相同的)。

可能是系统 B 格式化(当然没有更改内容)消息,导致返回的哈希码不同。

听起来合理吗? 如何以更稳健的方式签署消息?

到目前为止,我正在使用 C#、.NET3.5 来执行此操作,而且我无法更改技术。

我正在以这种方式从 fs 读取 M(并生成哈希码):

_currentHashCode = File.ReadAllText(file.FullName).GetHashCode();

在 B 中完成所有处理后,B 通知我在一个对象中向我发送 M':

object messageObj;

....

int hash = messageObj.ToString().GetHashCode();

谢谢

GetHashCode() 不 return 加密哈希。您需要使用 System.Security.Cryptography space 中的一种机制以您想要的方式创建哈希。

From MSDN:

A hash code is intended for efficient insertion and lookup in collections that are based on a hash table. A hash code is not a permanent value. For this reason:

  • Do not serialize hash code values or store them in databases.
  • Do not use the hash code as the key to retrieve an object from a keyed collection.
  • Do not send hash codes across application domains or processes. In some cases, hash codes may be computed on a per-process or per-application domain basis.
  • Do not use the hash code instead of a value returned by a cryptographic hashing function if you need a cryptographically strong hash. For cryptographic hashes, use a class derived from the System.Security.Cryptography.HashAlgorithm or System.Security.Cryptography.KeyedHashAlgorithm class.
  • Do not test for equality of hash codes to determine whether two objects are equal. (Unequal objects can have identical hash codes.) To test for equality, call the ReferenceEquals or Equals method.