如何从整数生成 MD5 哈希(32/64 个字符)

Cha*_*mar 1 .net c# int hash md5

我已经谷歌搜索并检查过

如何从整数生成 MD5 哈希(32/64 个字符)?

我得到的是从字符串或字节数组生成 MD5 哈希字符串的示例。但就我而言,我需要从整数中获取 MD5 哈希。

我知道该GetHashCode()方法可以获取整数的哈希码。但是这种方法不适用于我的情况。

我是否需要将整数转换为字符串或字节数组以获得预期的 MD5 哈希字符串?

请建议我该怎么做?

Dmi*_*nko 5

像这样的东西:

  int source = 123;
  String hash;

  // Do not omit "using" - should be disposed
  using (var md5 = System.Security.Cryptography.MD5.Create()) 
  {
    hash = String.Concat(md5.ComputeHash(BitConverter
      .GetBytes(source))
      .Select(x => x.ToString("x2")));
  }

  // Test
  // d119fabe038bc5d0496051658fd205e6
  Console.Write(hash);
Run Code Online (Sandbox Code Playgroud)