ClpCryptoApiRandomGenerator.pas 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. { *********************************************************************************** }
  2. { * CryptoLib Library * }
  3. { * Copyright (c) 2018 - 20XX Ugochukwu Mmaduekwe * }
  4. { * Github Repository <https://github.com/Xor-el> * }
  5. { * Distributed under the MIT software license, see the accompanying file LICENSE * }
  6. { * or visit http://www.opensource.org/licenses/mit-license.php. * }
  7. { * Acknowledgements: * }
  8. { * * }
  9. { * Thanks to Sphere 10 Software (http://www.sphere10.com/) for sponsoring * }
  10. { * development of this library * }
  11. { * ******************************************************************************* * }
  12. (* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
  13. unit ClpCryptoApiRandomGenerator;
  14. {$I CryptoLib.inc}
  15. interface
  16. uses
  17. ClpIRandomNumberGenerator,
  18. ClpRandomNumberGenerator,
  19. ClpICryptoApiRandomGenerator,
  20. ClpIRandomGenerator,
  21. ClpCryptoLibTypes;
  22. resourcestring
  23. SNegativeOffset = 'Start Offset Cannot be Negative, "Start"';
  24. SArrayTooSmall = 'Byte Array Too Small For Requested Offset and Length';
  25. type
  26. /// <summary>
  27. /// Uses TRandomNumberGenerator.Create() to Get randomness generator
  28. /// </summary>
  29. TCryptoApiRandomGenerator = class(TInterfacedObject,
  30. ICryptoApiRandomGenerator, IRandomGenerator)
  31. strict private
  32. var
  33. FrndProv: IRandomNumberGenerator;
  34. public
  35. /// <summary>
  36. /// Uses TRandomNumberGenerator.CreateRNG() to Get randomness generator
  37. /// </summary>
  38. constructor Create(); overload;
  39. constructor Create(const rng: IRandomNumberGenerator); overload;
  40. /// <summary>Add more seed material to the generator.</summary>
  41. /// <param name="seed">A byte array to be mixed into the generator's state.</param>
  42. procedure AddSeedMaterial(const seed: TCryptoLibByteArray);
  43. overload; virtual;
  44. /// <summary>Add more seed material to the generator.</summary>
  45. /// <param name="seed">A long value to be mixed into the generator's state.</param>
  46. procedure AddSeedMaterial(seed: Int64); overload; virtual;
  47. /// <summary>Fill byte array with random values.</summary>
  48. /// <param name="bytes">Array to be filled.</param>
  49. procedure NextBytes(const bytes: TCryptoLibByteArray); overload; virtual;
  50. /// <summary>Fill byte array with random values.</summary>
  51. /// <param name="bytes">Array to receive bytes.</param>
  52. /// <param name="start">Index to start filling at.</param>
  53. /// <param name="len">Length of segment to fill.</param>
  54. procedure NextBytes(const bytes: TCryptoLibByteArray; start, len: Int32);
  55. overload; virtual;
  56. end;
  57. implementation
  58. { TCryptoApiRandomGenerator }
  59. procedure TCryptoApiRandomGenerator.AddSeedMaterial(seed: Int64);
  60. begin
  61. // We don't care about the seed
  62. end;
  63. procedure TCryptoApiRandomGenerator.AddSeedMaterial
  64. (const seed: TCryptoLibByteArray);
  65. begin
  66. // We don't care about the seed
  67. end;
  68. constructor TCryptoApiRandomGenerator.Create(const rng: IRandomNumberGenerator);
  69. begin
  70. Inherited Create();
  71. FrndProv := rng;
  72. end;
  73. constructor TCryptoApiRandomGenerator.Create;
  74. begin
  75. Create(TRandomNumberGenerator.CreateRNG());
  76. end;
  77. procedure TCryptoApiRandomGenerator.NextBytes(const bytes: TCryptoLibByteArray);
  78. begin
  79. FrndProv.GetBytes(bytes);
  80. end;
  81. procedure TCryptoApiRandomGenerator.NextBytes(const bytes: TCryptoLibByteArray;
  82. start, len: Int32);
  83. var
  84. tmpBuf: TCryptoLibByteArray;
  85. begin
  86. if (start < 0) then
  87. begin
  88. raise EArgumentCryptoLibException.CreateRes(@SNegativeOffset);
  89. end;
  90. if (System.Length(bytes) < (start + len)) then
  91. begin
  92. raise EArgumentCryptoLibException.CreateRes(@SArrayTooSmall);
  93. end;
  94. if ((System.Length(bytes) = len) and (start = 0)) then
  95. begin
  96. NextBytes(bytes);
  97. end
  98. else
  99. begin
  100. System.SetLength(tmpBuf, len);
  101. NextBytes(tmpBuf);
  102. System.Move(tmpBuf[0], bytes[start], len * System.SizeOf(Byte));
  103. end;
  104. end;
  105. end.