S2k.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO;
  7. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Bcpg
  8. {
  9. /// <remarks>The string to key specifier class.</remarks>
  10. public class S2k
  11. : BcpgObject
  12. {
  13. private const int ExpBias = 6;
  14. public const int Simple = 0;
  15. public const int Salted = 1;
  16. public const int SaltedAndIterated = 3;
  17. public const int GnuDummyS2K = 101;
  18. public const int GnuProtectionModeNoPrivateKey = 1;
  19. public const int GnuProtectionModeDivertToCard = 2;
  20. internal int type;
  21. internal HashAlgorithmTag algorithm;
  22. internal byte[] iv;
  23. internal int itCount = -1;
  24. internal int protectionMode = -1;
  25. internal S2k(
  26. Stream inStr)
  27. {
  28. type = inStr.ReadByte();
  29. algorithm = (HashAlgorithmTag) inStr.ReadByte();
  30. //
  31. // if this happens we have a dummy-S2k packet.
  32. //
  33. if (type != GnuDummyS2K)
  34. {
  35. if (type != 0)
  36. {
  37. iv = new byte[8];
  38. if (Streams.ReadFully(inStr, iv, 0, iv.Length) < iv.Length)
  39. throw new EndOfStreamException();
  40. if (type == 3)
  41. {
  42. itCount = inStr.ReadByte();
  43. }
  44. }
  45. }
  46. else
  47. {
  48. inStr.ReadByte(); // G
  49. inStr.ReadByte(); // N
  50. inStr.ReadByte(); // U
  51. protectionMode = inStr.ReadByte(); // protection mode
  52. }
  53. }
  54. public S2k(
  55. HashAlgorithmTag algorithm)
  56. {
  57. this.type = 0;
  58. this.algorithm = algorithm;
  59. }
  60. public S2k(
  61. HashAlgorithmTag algorithm,
  62. byte[] iv)
  63. {
  64. this.type = 1;
  65. this.algorithm = algorithm;
  66. this.iv = iv;
  67. }
  68. public S2k(
  69. HashAlgorithmTag algorithm,
  70. byte[] iv,
  71. int itCount)
  72. {
  73. this.type = 3;
  74. this.algorithm = algorithm;
  75. this.iv = iv;
  76. this.itCount = itCount;
  77. }
  78. public virtual int Type
  79. {
  80. get { return type; }
  81. }
  82. /// <summary>The hash algorithm.</summary>
  83. public virtual HashAlgorithmTag HashAlgorithm
  84. {
  85. get { return algorithm; }
  86. }
  87. /// <summary>The IV for the key generation algorithm.</summary>
  88. public virtual byte[] GetIV()
  89. {
  90. return Arrays.Clone(iv);
  91. }
  92. /// <summary>The iteration count</summary>
  93. public virtual long IterationCount
  94. {
  95. get { return (16 + (itCount & 15)) << ((itCount >> 4) + ExpBias); }
  96. }
  97. /// <summary>The protection mode - only if GnuDummyS2K</summary>
  98. public virtual int ProtectionMode
  99. {
  100. get { return protectionMode; }
  101. }
  102. public override void Encode(
  103. BcpgOutputStream bcpgOut)
  104. {
  105. bcpgOut.WriteByte((byte) type);
  106. bcpgOut.WriteByte((byte) algorithm);
  107. if (type != GnuDummyS2K)
  108. {
  109. if (type != 0)
  110. {
  111. bcpgOut.Write(iv);
  112. }
  113. if (type == 3)
  114. {
  115. bcpgOut.WriteByte((byte) itCount);
  116. }
  117. }
  118. else
  119. {
  120. bcpgOut.WriteByte((byte) 'G');
  121. bcpgOut.WriteByte((byte) 'N');
  122. bcpgOut.WriteByte((byte) 'U');
  123. bcpgOut.WriteByte((byte) protectionMode);
  124. }
  125. }
  126. }
  127. }
  128. #pragma warning restore
  129. #endif