DerGeneralString.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Text;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1
  7. {
  8. public class DerGeneralString
  9. : DerStringBase
  10. {
  11. private readonly string str;
  12. public static DerGeneralString GetInstance(
  13. object obj)
  14. {
  15. if (obj == null || obj is DerGeneralString)
  16. {
  17. return (DerGeneralString) obj;
  18. }
  19. throw new ArgumentException("illegal object in GetInstance: "
  20. + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  21. }
  22. public static DerGeneralString GetInstance(
  23. Asn1TaggedObject obj,
  24. bool isExplicit)
  25. {
  26. Asn1Object o = obj.GetObject();
  27. if (isExplicit || o is DerGeneralString)
  28. {
  29. return GetInstance(o);
  30. }
  31. return new DerGeneralString(((Asn1OctetString)o).GetOctets());
  32. }
  33. public DerGeneralString(
  34. byte[] str)
  35. : this(Strings.FromAsciiByteArray(str))
  36. {
  37. }
  38. public DerGeneralString(
  39. string str)
  40. {
  41. if (str == null)
  42. throw new ArgumentNullException("str");
  43. this.str = str;
  44. }
  45. public override string GetString()
  46. {
  47. return str;
  48. }
  49. public byte[] GetOctets()
  50. {
  51. return Strings.ToAsciiByteArray(str);
  52. }
  53. internal override int EncodedLength(bool withID)
  54. {
  55. return Asn1OutputStream.GetLengthOfEncodingDL(withID, str.Length);
  56. }
  57. internal override void Encode(Asn1OutputStream asn1Out, bool withID)
  58. {
  59. asn1Out.WriteEncodingDL(withID, Asn1Tags.GeneralString, GetOctets());
  60. }
  61. protected override bool Asn1Equals(
  62. Asn1Object asn1Object)
  63. {
  64. DerGeneralString other = asn1Object as DerGeneralString;
  65. if (other == null)
  66. return false;
  67. return this.str.Equals(other.str);
  68. }
  69. }
  70. }
  71. #pragma warning restore
  72. #endif