c# – 为什么SHA256的所有变体都显示为SHA256Managed?

前端之家收集整理的这篇文章主要介绍了c# – 为什么SHA256的所有变体都显示为SHA256Managed?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在编写一个扩展方法,通过删除大量的样板来简化哈希的创建,但问题是每当我单步执行代码时,我都会看到它总是选择SHA256Managed,无论我是否调用SHA256. Create(),SHA256Cng.Create(),SHA256Managed.Create()或SHA256CryptoServiceProvider.Create()

当我选择不同的哈希算法(如MD5)时,情况也是如此,但在MD5的情况下,它始终选择MD5CryptoServiceProvider而不管我实际使用的类…

这是为什么?

这是我的代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Utility.Methods
  9. {
  10. public enum HashType { MD5,SHA512,SHA256,SHA384,SHA1 }
  11. public enum HashSubType {Normal,Cng,Managed,CryptoServiceProvider}
  12.  
  13. public static class TextHasher
  14. {
  15. public static string Hash(this string input,HashType hash,HashSubType subType = HashSubType.Normal)
  16. {
  17. Func<HashAlgorithm,string> hashFunction = alg => HashingHelper(input,alg);
  18.  
  19. switch (subType)
  20. {
  21. case HashSubType.Normal:
  22. return hashFunction(NormalHashes(hash));
  23. case HashSubType.Cng:
  24. return hashFunction(CngHashes(hash));
  25. case HashSubType.Managed:
  26. return hashFunction(ManagedHashes(hash));
  27. case HashSubType.CryptoServiceProvider:
  28. return hashFunction(CSPHashes(hash));
  29. default: return "error"; // unreachable
  30. }
  31. }
  32.  
  33. private static string HashingHelper(string text,HashAlgorithm algorithm)
  34. {
  35. Func<string,byte[]> getHash = input => algorithm.ComputeHash(Encoding.UTF8.GetBytes(input));
  36.  
  37. var sb = new StringBuilder();
  38. Array.ForEach(getHash(text),b => sb.Append(b.ToString("X")));
  39.  
  40. return sb.ToString();
  41. }
  42.  
  43. private static HashAlgorithm NormalHashes(HashType hash)
  44. {
  45. switch (hash)
  46. {
  47. case HashType.MD5:
  48. return MD5.Create();
  49. case HashType.SHA1:
  50. return SHA1.Create();
  51. case HashType.SHA256:
  52. return SHA256.Create();
  53. case HashType.SHA384:
  54. return SHA384.Create();
  55. case HashType.SHA512:
  56. return SHA512.Create();
  57. default: return null; // unreachable
  58. }
  59. }
  60.  
  61. private static HashAlgorithm CngHashes(HashType hash)
  62. {
  63. switch (hash)
  64. {
  65. case HashType.MD5:
  66. return MD5Cng.Create();
  67. case HashType.SHA1:
  68. return SHA1Cng.Create();
  69. case HashType.SHA256:
  70. return SHA256Cng.Create();
  71. case HashType.SHA384:
  72. return SHA384Cng.Create();
  73. case HashType.SHA512:
  74. return SHA512Cng.Create();
  75. default: return null; // unreachable
  76. }
  77. }
  78.  
  79. private static HashAlgorithm ManagedHashes(HashType hash)
  80. {
  81. switch (hash)
  82. {
  83. case HashType.SHA1:
  84. return SHA1Managed.Create();
  85. case HashType.SHA256:
  86. return SHA256Managed.Create();
  87. case HashType.SHA384:
  88. return SHA384Managed.Create();
  89. case HashType.SHA512:
  90. return SHA512Managed.Create();
  91. default: return null; // unreachable
  92. }
  93. }
  94.  
  95. private static HashAlgorithm CSPHashes(HashType hash)
  96. {
  97. switch (hash)
  98. {
  99. case HashType.MD5:
  100. return MD5CryptoServiceProvider.Create();
  101. case HashType.SHA1:
  102. return SHA1CryptoServiceProvider.Create();
  103. case HashType.SHA256:
  104. return SHA256CryptoServiceProvider.Create();
  105. case HashType.SHA384:
  106. return SHA384CryptoServiceProvider.Create();
  107. case HashType.SHA512:
  108. return SHA512CryptoServiceProvider.Create();
  109. default: return null; // unreachable
  110. }
  111. }
  112. }
  113. }

那么,有什么帮助吗?

解决方法

那是因为你总是调用相同的静态方法 SHA256.Create.SHA256是一个抽象类,它的后代不提供替代方法.实际上,Resharper会向您发出警告,提示您正在从派生类型访问静态成员.

实际上,调用SHA256.Create与调用HashAlgorithm.Create相同.两个类在内部调用相同的实现,只是将结果转换为不同的类型.

SHA256.Create方法将创建在machine.config中指定的默认实现,并且可以在app.config中覆盖

如果要使用特定提供程序,请使用SHA256.Create(string)传递要使用的提供程序的名称.

例如:

  1. SHA256.Create("System.Security.Cryptography.SHA256Cng");
  2. HashAlgorithm.Create("System.Security.Cryptography.SHA256Cng");
  3. SHA256.Create("System.Security.Cryptography.SHA256CryptoServiceProvider");

编辑

HashAlgorithm.Create的文档指定了有效算法名称的列表. MSDN文章Mapping Algorithm Names to Cryptography Classes描述了如何将算法名称映射到其他提供程序(您自己的,第三方,硬件加速或其他)并使用它们而不是默认算法.

编辑2

也可以通过编程方式更改映射.因此,要将“Dog”映射到SHA512CryptoServiceProvider,您只需要编写:

  1. CryptoConfig.AddAlgorithm(
  2. typeof(System.Security.Cryptography.SHA512CryptoServiceProvider),"Dog");
  3. var t4 = HashAlgorithm.Create("Dog");

猜你在找的C#相关文章