function verify(string $key, string $nonce, string $aad, string $ciphertext, string $tag) : bool { $cipher = new Cipher(); $context = $cipher->init($key, $nonce); $cipher->aad($context, $aad); $cipher->verify($context, $ciphertext); try { $cipher->finish($context, $tag); return true; } catch (AuthenticationException $e) { return false; } }
<?php ini_set('display_errors', 1); error_reporting(E_ALL); require_once dirname(__FILE__) . '/../Cipher.php'; Cipher::$defaultOptions['cipher'] = MCRYPT_RIJNDAEL_256; $out = Cipher::init($_REQUEST['options'])->{$_REQUEST['method']}($_REQUEST['subject']); if (!$_REQUEST['options']['base']) { $out = preg_replace_callback('/[\\x00-\\x20\\x7F-\\xFF]/', 'make_printable', $out); } echo $out; function make_printable($match) { $hex = dechex(ord($match[0])); $hex = strtoupper($hex); if (strlen($hex) == 1) { return '\\x0' . $hex; } else { return '\\x' . $hex; } }
<pre><?php ini_set('display_errors', 1); error_reporting(E_ALL); require_once dirname(__FILE__) . '/../Cipher.php'; Cipher::$defaultOptions['base'] = 62; $begin = microtime(true); foreach (range(1000, 1050) as $i) { echo "\n{$i} => " . ($o = Cipher::init()->encrypt($i)) . ' => ' . Cipher::init()->decrypt($o); } $end = round(microtime(true) - $begin, 3); echo "\nCompleted in {$end} seconds";
<pre><?php ini_set('display_errors', 1); error_reporting(E_ALL); require_once dirname(__FILE__) . '/../Cipher.php'; Cipher::$defaultOptions['base'] = 62; $begin = microtime(true); foreach (range(1, 3) as $i) { $password = uniqid(); echo "\n{$i} => " . ($o = Cipher::init()->hashPassword($password)) . ' => matches? ' . (int) Cipher::init()->validatePassword($password, $o); } $end = round(microtime(true) - $begin, 3); echo "\nCompleted in {$end} seconds";