pssht  latest
SSH server library written in PHP
Base.php
1 <?php
2 
3 /*
4 * This file is part of pssht.
5 *
6 * (c) François Poirotte <clicky@erebot.net>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 
13 
17 
21 abstract class Base implements
25 {
27  protected $mcrypt;
28 
29  public function __construct($iv, $key)
30  {
31  $this->mcrypt = mcrypt_module_open(
32  constant(static::getAlgorithm()),
33  '',
34  static::getMode(),
35  ''
36  );
37  mcrypt_generic_init($this->mcrypt, $key, $iv);
38  }
39 
40  final public function __destruct()
41  {
42  mcrypt_generic_deinit($this->mcrypt);
43  mcrypt_module_close($this->mcrypt);
44  }
45 
46  final public static function isAvailable()
47  {
48  if (!extension_loaded('mcrypt')) {
49  return false;
50  }
51 
52  if (!defined(static::getAlgorithm())) {
53  return false;
54  }
55  $res = @mcrypt_module_open(
56  constant(static::getAlgorithm()),
57  '',
58  static::getMode(),
59  ''
60  );
61  if ($res !== false) {
62  mcrypt_module_close($res);
63  }
64  return (bool) $res;
65  }
66 
67  final public static function getIVSize()
68  {
69  return mcrypt_get_iv_size(
70  constant(static::getAlgorithm()),
71  static::getMode()
72  );
73  }
74 
75  final public static function getBlockSize()
76  {
77  return mcrypt_get_block_size(
78  constant(static::getAlgorithm()),
79  static::getMode()
80  );
81  }
82 
83  final public function encrypt($seqno, $data)
84  {
85  return mcrypt_generic($this->mcrypt, $data);
86  }
87 
88  final public function decrypt($seqno, $data)
89  {
90  return mdecrypt_generic($this->mcrypt, $data);
91  }
92 
93  public static function getAlgorithm()
94  {
95  $algo = strtoupper(substr(strrchr(get_called_class(), '\\'), 1));
96  return 'MCRYPT_' . $algo;
97  }
98 
99  public static function getMode()
100  {
101  $cls = explode('\\', get_called_class());
102  $mode = strtolower($cls[count($cls) - 2]);
103  return $mode;
104  }
105 
106  public static function getName()
107  {
108  $algo = strtolower(substr(strrchr(get_called_class(), '\\'), 1));
109  return $algo . '-' . static::getMode();
110  }
111 }
static getName()
Return the name of the algorithm.
Definition: Base.php:106
decrypt($seqno, $data)
Definition: Base.php:88
$mcrypt
mcrypt handle for the cipher.
Definition: Base.php:27
encrypt($seqno, $data)
Definition: Base.php:83