5 * Use this factory to instantiate random number generators, sources and mixers.
9 * @category PHPCryptLib
11 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
12 * @copyright 2011 The Authors
13 * @license http://www.opensource.org/licenses/mit-license.html MIT License
14 * @version Build @@version@@
17 namespace CryptLib\Random
;
19 use CryptLib\Core\Strength
;
24 * Use this factory to instantiate random number generators, sources and mixers.
26 * @category PHPCryptLib
28 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
30 class Factory
extends \CryptLib\Core\AbstractFactory
{
33 * @var array A list of available random number mixing strategies
35 protected $mixers = array();
38 * @var array A list of available random number sources
40 protected $sources = array();
43 * Build a new instance of the factory, loading core mixers and sources
47 public function __construct() {
53 * Get a generator for the requested strength
55 * @param Strength $strength The requested strength of the random number
57 * @return Generator The instantiated generator
58 * @throws RuntimeException If an appropriate mixing strategy isn't found
60 public function getGenerator(\CryptLib\Core\Strength
$strength) {
61 $sources = $this->getSources();
62 $newSources = array();
63 foreach ($sources as $source) {
64 if ($strength->compare($source::getStrength()) <= 0) {
65 $newSources[] = new $source;
68 $mixer = $this->findMixer($strength);
69 return new Generator($newSources, $mixer);
73 * Get a high strength random number generator
75 * High Strength keys should ONLY be used for generating extremely strong
76 * cryptographic keys. Generating them is very resource intensive and may
77 * take several minutes or more depending on the requested size.
79 * @return Generator The instantiated generator
81 public function getHighStrengthGenerator() {
82 return $this->getGenerator(new Strength(Strength
::HIGH
));
86 * Get a low strength random number generator
88 * Low Strength should be used anywhere that random strings are needed in a
89 * non-cryptographical setting. They are not strong enough to be used as
90 * keys or salts. They are however useful for one-time use tokens.
92 * @return Generator The instantiated generator
94 public function getLowStrengthGenerator() {
95 return $this->getGenerator(new Strength(Strength
::LOW
));
99 * Get a medium strength random number generator
101 * Medium Strength should be used for most needs of a cryptographic nature.
102 * They are strong enough to be used as keys and salts. However, they do
103 * take some time and resources to generate, so they should not be over-used
105 * @return Generator The instantiated generator
107 public function getMediumStrengthGenerator() {
108 return $this->getGenerator(new Strength(Strength
::MEDIUM
));
112 * Get all loaded mixing strategies
114 * @return array An array of mixers
116 public function getMixers() {
117 return $this->mixers
;
121 * Get all loaded random number sources
123 * @return array An array of sources
125 public function getSources() {
126 return $this->sources
;
130 * Register a mixing strategy for this factory instance
132 * @param string $name The name of the stategy
133 * @param string $class The class name of the implementation
135 * @return Factory $this The current factory instance
137 public function registerMixer($name, $class) {
140 __NAMESPACE__
. '\\Mixer',
148 * Register a random number source for this factory instance
150 * Note that this class must implement the Source interface
152 * @param string $name The name of the stategy
153 * @param string $class The class name of the implementation
155 * @return Factory $this The current factory instance
157 public function registerSource($name, $class) {
160 __NAMESPACE__
. '\\Source',
168 * Find a mixer based upon the requested strength
170 * @param Strength $strength The strength mixer to find
172 * @return Mixer The found mixer
173 * @throws RuntimeException if a valid mixer cannot be found
175 protected function findMixer(\CryptLib\Core\Strength
$strength) {
178 foreach ($this->getMixers() as $mixer) {
179 if ($strength->compare($mixer::getStrength()) == 0) {
180 $newMixer = new $mixer;
181 } elseif ($strength->compare($mixer::getStrength()) == 1) {
182 $fallback = new $mixer;
185 if (is_null($newMixer)) {
186 if (is_null($fallback)) {
187 throw new \
RuntimeException('Could not find mixer');
195 * Load all core mixing strategies
199 protected function loadMixers() {
202 __NAMESPACE__
. '\\Mixer\\',
203 array($this, 'registerMixer')
208 * Load all core random number sources
212 protected function loadSources() {
215 __NAMESPACE__
. '\\Source\\',
216 array($this, 'registerSource')