HashAlgorithm.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls
  5. {
  6. /// <summary>RFC 5246 7.4.1.4.1</summary>
  7. public abstract class HashAlgorithm
  8. {
  9. public const short none = 0;
  10. public const short md5 = 1;
  11. public const short sha1 = 2;
  12. public const short sha224 = 3;
  13. public const short sha256 = 4;
  14. public const short sha384 = 5;
  15. public const short sha512 = 6;
  16. /*
  17. * RFC 8422
  18. */
  19. public const short Intrinsic = 8;
  20. public static string GetName(short hashAlgorithm)
  21. {
  22. switch (hashAlgorithm)
  23. {
  24. case none:
  25. return "none";
  26. case md5:
  27. return "md5";
  28. case sha1:
  29. return "sha1";
  30. case sha224:
  31. return "sha224";
  32. case sha256:
  33. return "sha256";
  34. case sha384:
  35. return "sha384";
  36. case sha512:
  37. return "sha512";
  38. case Intrinsic:
  39. return "Intrinsic";
  40. default:
  41. return "UNKNOWN";
  42. }
  43. }
  44. public static int GetOutputSize(short hashAlgorithm)
  45. {
  46. switch (hashAlgorithm)
  47. {
  48. case md5:
  49. return 16;
  50. case sha1:
  51. return 20;
  52. case sha224:
  53. return 28;
  54. case sha256:
  55. return 32;
  56. case sha384:
  57. return 48;
  58. case sha512:
  59. return 64;
  60. default:
  61. return -1;
  62. }
  63. }
  64. public static string GetText(short hashAlgorithm)
  65. {
  66. return GetName(hashAlgorithm) + "(" + hashAlgorithm + ")";
  67. }
  68. public static bool IsPrivate(short hashAlgorithm)
  69. {
  70. return 224 <= hashAlgorithm && hashAlgorithm <= 255;
  71. }
  72. public static bool IsRecognized(short hashAlgorithm)
  73. {
  74. switch (hashAlgorithm)
  75. {
  76. case md5:
  77. case sha1:
  78. case sha224:
  79. case sha256:
  80. case sha384:
  81. case sha512:
  82. case Intrinsic:
  83. return true;
  84. default:
  85. return false;
  86. }
  87. }
  88. }
  89. }
  90. #pragma warning restore
  91. #endif