VMPCRandomGenerator.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  5. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Prng
  6. {
  7. public sealed class VmpcRandomGenerator
  8. : IRandomGenerator
  9. {
  10. /// <remarks>
  11. /// Permutation generated by code:
  12. /// <code>
  13. /// // First 1850 fractional digit of Pi number.
  14. /// byte[] key = new BigInteger("14159265358979323846...5068006422512520511").ToByteArray();
  15. /// s = 0;
  16. /// P = new byte[256];
  17. /// for (int i = 0; i &lt; 256; i++)
  18. /// {
  19. /// P[i] = (byte) i;
  20. /// }
  21. /// for (int m = 0; m &lt; 768; m++)
  22. /// {
  23. /// s = P[(s + P[m &amp; 0xff] + key[m % key.length]) &amp; 0xff];
  24. /// byte temp = P[m &amp; 0xff];
  25. /// P[m &amp; 0xff] = P[s &amp; 0xff];
  26. /// P[s &amp; 0xff] = temp;
  27. /// } </code>
  28. /// </remarks>
  29. private readonly byte[] P =
  30. {
  31. 0xbb, 0x2c, 0x62, 0x7f, 0xb5, 0xaa, 0xd4, 0x0d, 0x81, 0xfe, 0xb2, 0x82, 0xcb, 0xa0, 0xa1, 0x08,
  32. 0x18, 0x71, 0x56, 0xe8, 0x49, 0x02, 0x10, 0xc4, 0xde, 0x35, 0xa5, 0xec, 0x80, 0x12, 0xb8, 0x69,
  33. 0xda, 0x2f, 0x75, 0xcc, 0xa2, 0x09, 0x36, 0x03, 0x61, 0x2d, 0xfd, 0xe0, 0xdd, 0x05, 0x43, 0x90,
  34. 0xad, 0xc8, 0xe1, 0xaf, 0x57, 0x9b, 0x4c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xdc, 0x67, 0x48, 0xba, 0x2e, 0xe6, 0xa4, 0xab, 0x7c, 0x94,
  46. 0x00, 0x21, 0xef, 0xea, 0xbe, 0xca, 0x72, 0x4f, 0x52, 0x98, 0x3f, 0xc2, 0x14, 0x7b, 0x3b, 0x54,
  47. };
  48. /// <remarks>Value generated in the same way as <c>P</c>.</remarks>
  49. private byte s = 0xbe;
  50. private byte n = 0;
  51. public VmpcRandomGenerator()
  52. {
  53. }
  54. public void AddSeedMaterial(byte[] seed)
  55. {
  56. for (int m = 0; m < seed.Length; m++)
  57. {
  58. byte pn = P[n];
  59. s = P[(s + pn + seed[m]) & 0xff];
  60. P[n] = P[s];
  61. P[s] = pn;
  62. n = (byte)(n + 1);
  63. }
  64. }
  65. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  66. public void AddSeedMaterial(ReadOnlySpan<byte> seed)
  67. {
  68. for (int m = 0; m < seed.Length; m++)
  69. {
  70. byte pn = P[n];
  71. s = P[(s + pn + seed[m]) & 0xff];
  72. P[n] = P[s];
  73. P[s] = pn;
  74. n = (byte)(n + 1);
  75. }
  76. }
  77. #endif
  78. public void AddSeedMaterial(long seed)
  79. {
  80. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  81. Span<byte> bytes = stackalloc byte[8];
  82. Pack.UInt64_To_BE((ulong)seed, bytes);
  83. AddSeedMaterial(bytes);
  84. #else
  85. AddSeedMaterial(Pack.UInt64_To_BE((ulong)seed));
  86. #endif
  87. }
  88. public void NextBytes(byte[] bytes)
  89. {
  90. NextBytes(bytes, 0, bytes.Length);
  91. }
  92. public void NextBytes(byte[] bytes, int start, int len)
  93. {
  94. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  95. NextBytes(bytes.AsSpan(start, len));
  96. #else
  97. lock (P)
  98. {
  99. int end = start + len;
  100. for (int i = start; i != end; i++)
  101. {
  102. byte pn = P[n];
  103. s = P[(s + pn) & 0xFF];
  104. byte ps = P[s];
  105. bytes[i] = P[(P[ps] + 1) & 0xFF];
  106. P[s] = pn;
  107. P[n] = ps;
  108. n = (byte)(n + 1);
  109. }
  110. }
  111. #endif
  112. }
  113. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  114. public void NextBytes(Span<byte> bytes)
  115. {
  116. lock (P)
  117. {
  118. for (int i = 0; i < bytes.Length; ++i)
  119. {
  120. byte pn = P[n];
  121. s = P[(s + pn) & 0xFF];
  122. byte ps = P[s];
  123. bytes[i] = P[(P[ps] + 1) & 0xFF];
  124. P[s] = pn;
  125. P[n] = ps;
  126. n = (byte)(n + 1);
  127. }
  128. }
  129. }
  130. #endif
  131. }
  132. }
  133. #pragma warning restore
  134. #endif