SignatureAndHashAlgorithm.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Tls
  6. {
  7. /**
  8. * RFC 5246 7.4.1.4.1
  9. */
  10. public class SignatureAndHashAlgorithm
  11. {
  12. protected readonly byte mHash;
  13. protected readonly byte mSignature;
  14. /**
  15. * @param hash {@link HashAlgorithm}
  16. * @param signature {@link SignatureAlgorithm}
  17. */
  18. public SignatureAndHashAlgorithm(byte hash, byte signature)
  19. {
  20. if (!TlsUtilities.IsValidUint8(hash))
  21. {
  22. throw new ArgumentException("should be a uint8", "hash");
  23. }
  24. if (!TlsUtilities.IsValidUint8(signature))
  25. {
  26. throw new ArgumentException("should be a uint8", "signature");
  27. }
  28. if (signature == SignatureAlgorithm.anonymous)
  29. {
  30. throw new ArgumentException("MUST NOT be \"anonymous\"", "signature");
  31. }
  32. this.mHash = hash;
  33. this.mSignature = signature;
  34. }
  35. /**
  36. * @return {@link HashAlgorithm}
  37. */
  38. public virtual byte Hash
  39. {
  40. get { return mHash; }
  41. }
  42. /**
  43. * @return {@link SignatureAlgorithm}
  44. */
  45. public virtual byte Signature
  46. {
  47. get { return mSignature; }
  48. }
  49. public override bool Equals(object obj)
  50. {
  51. if (!(obj is SignatureAndHashAlgorithm))
  52. {
  53. return false;
  54. }
  55. SignatureAndHashAlgorithm other = (SignatureAndHashAlgorithm)obj;
  56. return other.Hash == Hash && other.Signature == Signature;
  57. }
  58. public override int GetHashCode()
  59. {
  60. return ((int)Hash << 16) | (int)Signature;
  61. }
  62. /**
  63. * Encode this {@link SignatureAndHashAlgorithm} to a {@link Stream}.
  64. *
  65. * @param output the {@link Stream} to encode to.
  66. * @throws IOException
  67. */
  68. public virtual void Encode(Stream output)
  69. {
  70. TlsUtilities.WriteUint8(Hash, output);
  71. TlsUtilities.WriteUint8(Signature, output);
  72. }
  73. /**
  74. * Parse a {@link SignatureAndHashAlgorithm} from a {@link Stream}.
  75. *
  76. * @param input the {@link Stream} to parse from.
  77. * @return a {@link SignatureAndHashAlgorithm} object.
  78. * @throws IOException
  79. */
  80. public static SignatureAndHashAlgorithm Parse(Stream input)
  81. {
  82. byte hash = TlsUtilities.ReadUint8(input);
  83. byte signature = TlsUtilities.ReadUint8(input);
  84. return new SignatureAndHashAlgorithm(hash, signature);
  85. }
  86. }
  87. }
  88. #pragma warning restore
  89. #endif