PemWriter.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO.Pem
  7. {
  8. /**
  9. * A generic PEM writer, based on RFC 1421
  10. */
  11. public class PemWriter
  12. : IDisposable
  13. {
  14. private const int LineLength = 64;
  15. private readonly TextWriter writer;
  16. private readonly int nlLength;
  17. private char[] buf = new char[LineLength];
  18. /**
  19. * Base constructor.
  20. *
  21. * @param out output stream to use.
  22. */
  23. public PemWriter(TextWriter writer)
  24. {
  25. this.writer = writer ?? throw new ArgumentNullException(nameof(writer));
  26. this.nlLength = Environment.NewLine.Length;
  27. }
  28. #region IDisposable
  29. public void Dispose()
  30. {
  31. Dispose(true);
  32. GC.SuppressFinalize(this);
  33. }
  34. protected virtual void Dispose(bool disposing)
  35. {
  36. if (disposing)
  37. {
  38. writer.Dispose();
  39. }
  40. }
  41. #endregion
  42. public TextWriter Writer
  43. {
  44. get { return writer; }
  45. }
  46. /**
  47. * Return the number of bytes or characters required to contain the
  48. * passed in object if it is PEM encoded.
  49. *
  50. * @param obj pem object to be output
  51. * @return an estimate of the number of bytes
  52. */
  53. public int GetOutputSize(PemObject obj)
  54. {
  55. // BEGIN and END boundaries.
  56. int size = (2 * (obj.Type.Length + 10 + nlLength)) + 6 + 4;
  57. if (obj.Headers.Count > 0)
  58. {
  59. foreach (PemHeader header in obj.Headers)
  60. {
  61. size += header.Name.Length + ": ".Length + header.Value.Length + nlLength;
  62. }
  63. size += nlLength;
  64. }
  65. // base64 encoding
  66. int dataLen = ((obj.Content.Length + 2) / 3) * 4;
  67. size += dataLen + (((dataLen + LineLength - 1) / LineLength) * nlLength);
  68. return size;
  69. }
  70. public void WriteObject(PemObjectGenerator objGen)
  71. {
  72. PemObject obj = objGen.Generate();
  73. WritePreEncapsulationBoundary(obj.Type);
  74. if (obj.Headers.Count > 0)
  75. {
  76. foreach (PemHeader header in obj.Headers)
  77. {
  78. writer.Write(header.Name);
  79. writer.Write(": ");
  80. writer.WriteLine(header.Value);
  81. }
  82. writer.WriteLine();
  83. }
  84. WriteEncoded(obj.Content);
  85. WritePostEncapsulationBoundary(obj.Type);
  86. }
  87. private void WriteEncoded(byte[] bytes)
  88. {
  89. bytes = Base64.Encode(bytes);
  90. for (int i = 0; i < bytes.Length; i += buf.Length)
  91. {
  92. int index = 0;
  93. while (index != buf.Length)
  94. {
  95. if ((i + index) >= bytes.Length)
  96. break;
  97. buf[index] = (char)bytes[i + index];
  98. index++;
  99. }
  100. writer.WriteLine(buf, 0, index);
  101. }
  102. }
  103. private void WritePreEncapsulationBoundary(string type)
  104. {
  105. writer.WriteLine("-----BEGIN " + type + "-----");
  106. }
  107. private void WritePostEncapsulationBoundary(string type)
  108. {
  109. writer.WriteLine("-----END " + type + "-----");
  110. }
  111. }
  112. }
  113. #pragma warning restore
  114. #endif