SignatureAndHashAlgorithm.cs 2.7 KB

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