123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- /*
- * The RandomLib library for securely generating random numbers and strings in PHP
- *
- * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
- * @copyright 2011 The Authors
- * @license http://www.opensource.org/licenses/mit-license.html MIT License
- * @version Build @@version@@
- */
- /**
- * The Capicom Random Number Source
- *
- * This uses the Windows CapiCom Com object to generate random numbers
- *
- * PHP version 5.3
- *
- * @category PHPCryptLib
- * @package Random
- * @subpackage Source
- *
- * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
- * @author Paragon Initiative Enterprises <security@paragonie.com>
- * @copyright 2011 The Authors
- * @license http://www.opensource.org/licenses/mit-license.html MIT License
- *
- * @version Build @@version@@
- */
- namespace RandomLib\Source;
- use SecurityLib\Strength;
- /**
- * The Capicom Random Number Source
- *
- * This uses the Windows CapiCom Com object to generate random numbers
- *
- * @category PHPCryptLib
- * @package Random
- * @subpackage Source
- *
- * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
- * @author Paragon Initiative Enterprises <security@paragonie.com>
- * @codeCoverageIgnore
- */
- class CAPICOM extends \RandomLib\AbstractSource
- {
- /**
- * Return an instance of Strength indicating the strength of the source
- *
- * @return \SecurityLib\Strength An instance of one of the strength classes
- */
- public static function getStrength()
- {
- return new Strength(Strength::MEDIUM);
- }
- /**
- * If the source is currently available.
- * Reasons might be because the library is not installed
- *
- * @return bool
- */
- public static function isSupported()
- {
- return class_exists('\\COM', false);
- }
- /**
- * Generate a random string of the specified size
- *
- * @param int $size The size of the requested random string
- *
- * @return string A string of the requested size
- */
- public function generate($size)
- {
- if (!\class_exists('COM', false)) {
- /** @var string $result */
- $result = static::emptyValue($size);
- return $result;
- }
- try {
- /** @var \COM $util */
- $util = new \COM('CAPICOM.Utilities.1');
- if (!\method_exists($util, 'GetRandom')) {
- /** @var string $result */
- $result = static::emptyValue($size);
- return $result;
- }
- $data = base64_decode((string) $util->GetRandom($size, 0));
- return (string) str_pad($data, $size, chr(0));
- } catch (\Exception $e) {
- unset($e);
- /** @var string $result */
- $result = static::emptyValue($size);
- return $result;
- }
- }
- }
|