3 * The Capicom Random Number Source
5 * This uses the Windows CapiCom Com object to generate random numbers
9 * @category PHPCryptLib
12 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
13 * @copyright 2011 The Authors
14 * @license http://www.opensource.org/licenses/mit-license.html MIT License
15 * @version Build @@version@@
18 namespace CryptLib\Random\Source
;
20 use CryptLib\Core\Strength
;
23 * The Capicom Random Number Source
25 * This uses the Windows CapiCom Com object to generate random numbers
27 * @category PHPCryptLib
30 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
33 class CAPICOM
implements \CryptLib\Random\Source
{
36 * Return an instance of Strength indicating the strength of the source
38 * @return Strength An instance of one of the strength classes
40 public static function getStrength() {
41 return new Strength(Strength
::MEDIUM
);
45 * Generate a random string of the specified size
47 * @param int $size The size of the requested random string
49 * @return string A string of the requested size
51 public function generate($size) {
52 if (!class_exists('\\COM', false)) {
53 return str_repeat(chr(0), $size);
56 $util = new \
COM('CAPICOM.Utilities.1');
57 $data = base64_decode($util->GetRandom($size, 0));
58 return str_pad($data, $size, chr(0));
59 } catch (\Exception
$e) {
61 return str_repeat(chr(0), $size);