10 defined(
'JPATH_PLATFORM') or die;
47 public function __construct()
49 if (!is_callable(
'mcrypt_encrypt'))
51 throw new RuntimeException(
'The mcrypt extension is not available.');
68 if ($key->type != $this->keyType)
70 throw new InvalidArgumentException(
'Invalid key of type: ' . $key->type .
'. Expected ' . $this->keyType .
'.');
74 $decrypted = trim(mcrypt_decrypt($this->type, $key->private, $data, $this->mode, $key->public));
92 if ($key->type != $this->keyType)
94 throw new InvalidArgumentException(
'Invalid key of type: ' . $key->type .
'. Expected ' . $this->keyType .
'.');
98 $encrypted = mcrypt_encrypt($this->type, $key->private, $data, $this->mode, $key->public);
112 public function generateKey(array $options = array())
118 $key->public = mcrypt_create_iv(mcrypt_get_iv_size($this->type, $this->mode));
121 $salt = (isset($options[
'salt'])) ? $options[
'salt'] : substr(pack(
"h*", md5(mt_rand())), 0, 16);
122 $password = (isset($options[
'password'])) ? $options[
'password'] :
'J00ml4R0ck$!';
125 $key->private = $this->pbkdf2($password, $salt, mcrypt_get_key_size($this->type, $this->mode));
145 public function pbkdf2($p, $s, $kl, $c = 10000, $a =
'sha256')
148 $hl = strlen(hash($a, null,
true));
151 $kb = ceil($kl / $hl);
157 for ($block = 1; $block <= $kb; $block++)
160 $ib = $b = hash_hmac($a, $s . pack(
'N', $block), $p,
true);
163 for ($i = 1; $i < $c; $i++)
165 $ib ^= ($b = hash_hmac($a, $b, $p,
true));
173 return substr($dk, 0, $kl);