5 * Use this factory to instantiate ciphers and modes based upon their names. You
6 * can register new ciphers and modes by simply calling the appropriate methods.
10 * @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\Cipher
;
21 * Some used classes, aliased appropriately
23 use CryptLib\Cipher\Block\Cipher
as Cipher
;
24 use CryptLib\Cipher\Block\Mode
as Mode
;
25 use CryptLib\Cipher\Block\Cipher\MCrypt
;
31 * Use this factory to instantiate ciphers and modes based upon their names. You
32 * can register new ciphers and modes by simply calling the appropriate methods.
34 * @category PHPCryptLib
36 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
38 class Factory
extends \CryptLib\Core\AbstractFactory
{
41 * @var array A list of available cipher implementations by name of cipher
43 protected $ciphers = array();
46 * @var array A list of available mode implementations by name of mode
48 protected $modes = array();
51 * Instantiate the factory
53 * This automatically loads and registers the default cipher and mode
58 public function __construct() {
64 * Get an instance of a cipher by name
66 * Note that this will return the passed argument if it is an instance of
67 * the Block cipher interface.
69 * @param string|Block $cipher The cipher name or instance to load
71 * @return Cipher The loaded block cipher
72 * @throws RuntimeException if the cipher is not supported
74 public function getBlockCipher($cipher) {
75 if (is_object($cipher) && $cipher instanceof Cipher
) {
78 $cipher = strtolower($cipher);
79 if (in_array($cipher, MCrypt
::getSupportedCiphers())) {
80 //Use the built in MCrypt library if it's available
81 return new MCrypt($cipher);
82 } elseif (isset($this->ciphers
[$cipher])) {
83 $class = $this->ciphers
[$cipher];
84 return new $class($cipher);
86 $message = sprintf('Unsupported Cipher %s', $cipher);
87 throw new \
RuntimeException($message);
91 * Get an instance of a mode by name
93 * Note that this will return the passed argument if it is an instance of
96 * @param string|Mode $mode The mode name or instance to load
98 * @return Mode The loaded mode instance
99 * @throws RuntimeException if the mode is not supported
101 public function getMode(
103 \CryptLib\Cipher\Block\Cipher
$cipher,
105 array $options = array()
107 if (is_object($mode) && $mode instanceof Mode
) {
110 $mode = strtolower($mode);
111 if (isset($this->modes
[$mode])) {
112 $class = $this->modes
[$mode];
113 return new $class($cipher, $initv, $options);
115 $message = sprintf('Unsupported Mode %s', $mode);
116 throw new \
RuntimeException($message);
120 * Register a new cipher implementation class for this factory
122 * This will iterate over each supported cipher for the class and load the
123 * cipher into the list of supported ciphers
125 * @param string $name The name of the cipher (ignored)
126 * @param string $class The full class name of the cipher implementation
128 * @return Factory $this The current factory instance
129 * @throws InvalidArgumentException If the class is not a block cipher
131 public function registerCipher($name, $class) {
132 $refl = new \
ReflectionClass($class);
133 $interface = '\\'. __NAMESPACE__
. '\\Block\\Cipher';
134 if (!$refl->implementsInterface($interface)) {
135 $message = sprintf('Class must implement %s', $interface);
136 throw new \
InvalidArgumentException($message);
138 foreach ($class::getSupportedCiphers() as $cipher){
139 $this->ciphers
[$cipher] = $class;
145 * Register a new mode implementation class for this factory
147 * @param string $name The name of the mode (ignored)
148 * @param string $class The full class name of the mode implementation
150 * @return Factory $this The current factory instance
151 * @throws InvalidArgumentException If the class is not a valid mode
153 public function registerMode($name, $class) {
154 $refl = new \
ReflectionClass($class);
155 $interface = '\\'. __NAMESPACE__
. '\\Block\\Mode';
156 if (!$refl->implementsInterface($interface)) {
157 throw new \
InvalidArgumentException('Class must implement Mode');
159 $this->modes
[strtolower($name)] = $class;
164 * Load all core cipher implementations
168 protected function loadCiphers() {
170 __DIR__
. '/Block/Cipher',
171 __NAMESPACE__
. '\\Block\\Cipher\\',
172 array($this, 'registerCipher')
177 * Load all core mode implementations
181 protected function loadModes() {
183 __DIR__
. '/Block/Mode',
184 __NAMESPACE__
. '\\Block\\Mode\\',
185 array($this, 'registerMode')