CAPICOM.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /*
  3. * The RandomLib library for securely generating random numbers and strings in PHP
  4. *
  5. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  6. * @copyright 2011 The Authors
  7. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  8. * @version Build @@version@@
  9. */
  10. /**
  11. * The Capicom Random Number Source
  12. *
  13. * This uses the Windows CapiCom Com object to generate random numbers
  14. *
  15. * PHP version 5.3
  16. *
  17. * @category PHPCryptLib
  18. * @package Random
  19. * @subpackage Source
  20. *
  21. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  22. * @author Paragon Initiative Enterprises <security@paragonie.com>
  23. * @copyright 2011 The Authors
  24. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  25. *
  26. * @version Build @@version@@
  27. */
  28. namespace RandomLib\Source;
  29. use SecurityLib\Strength;
  30. /**
  31. * The Capicom Random Number Source
  32. *
  33. * This uses the Windows CapiCom Com object to generate random numbers
  34. *
  35. * @category PHPCryptLib
  36. * @package Random
  37. * @subpackage Source
  38. *
  39. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  40. * @author Paragon Initiative Enterprises <security@paragonie.com>
  41. * @codeCoverageIgnore
  42. */
  43. class CAPICOM extends \RandomLib\AbstractSource
  44. {
  45. /**
  46. * Return an instance of Strength indicating the strength of the source
  47. *
  48. * @return \SecurityLib\Strength An instance of one of the strength classes
  49. */
  50. public static function getStrength()
  51. {
  52. return new Strength(Strength::MEDIUM);
  53. }
  54. /**
  55. * If the source is currently available.
  56. * Reasons might be because the library is not installed
  57. *
  58. * @return bool
  59. */
  60. public static function isSupported()
  61. {
  62. return class_exists('\\COM', false);
  63. }
  64. /**
  65. * Generate a random string of the specified size
  66. *
  67. * @param int $size The size of the requested random string
  68. *
  69. * @return string A string of the requested size
  70. */
  71. public function generate($size)
  72. {
  73. if (!\class_exists('COM', false)) {
  74. /** @var string $result */
  75. $result = static::emptyValue($size);
  76. return $result;
  77. }
  78. try {
  79. /** @var \COM $util */
  80. $util = new \COM('CAPICOM.Utilities.1');
  81. if (!\method_exists($util, 'GetRandom')) {
  82. /** @var string $result */
  83. $result = static::emptyValue($size);
  84. return $result;
  85. }
  86. $data = base64_decode((string) $util->GetRandom($size, 0));
  87. return (string) str_pad($data, $size, chr(0));
  88. } catch (\Exception $e) {
  89. unset($e);
  90. /** @var string $result */
  91. $result = static::emptyValue($size);
  92. return $result;
  93. }
  94. }
  95. }