@ 2016-02-21T00:04:38Z <?php
if (PHP_VERSION_ID < 70000) {
die('no');
}
/**
* Use HKDF to derive multiple keys from one.
* http://tools.ietf.org/html/rfc5869
*
* @param string $hash Hash Function
* @param string $ikm Initial Keying Material
* @param int $length How many bytes?
* @param string $info What sort of key are we deriving?
* @param string $salt
* @return string
* @throws Exception
*/
function hash_hkdf(string $algo, string $ikm, int $length, string $info = '', $salt = null)
{
$digest_length = mb_strlen(hash_hmac($algo, '', '', true), '8bit');
// Sanity-check the desired output length.
if (empty($length) || !is_int($length) ||
$length < 0 || $length > 255 * $digest_length) {
throw new Exception("Bad output length requested of HKDF.");
}
// "if [salt] not provided, is set to a string of HashLen zeroes."
if ($salt === null) {
$salt = str_repeat("\x00", $digest_length);
}
// HKDF-Extract:
// PRK = HMAC-Hash(salt, IKM)
// The salt is the HMAC key.
$prk = hash_hmac($algo, $ikm, $salt, true);
// HKDF-Expand:
// This check is useless, but it serves as a reminder to the spec.
if (mb_strlen($prk, '8bit') < $digest_length) {
throw new Exception('HKDF-Expand failed');
}
// T(0) = ''
$t = '';
$last_block = '';
for ($block_index = 1; mb_strlen($t, '8bit') < $length; ++$block_index) {
// T(i) = HMAC-Hash(PRK, T(i-1) | info | 0x??)
$last_block = hash_hmac(
$algo,
$last_block . $info . chr($block_index),
$prk,
true
);
// T = T(1) | T(2) | T(3) | ... | T(N)
$t .= $last_block;
}
// ORM = first L octets of T
$orm = mb_substr($t, 0, $length, '8bit');
if ($orm === false) {
throw new Exception('Unexpected error');
}
return $orm;
}
/**
* Encrypt a message with AES-256-CTR + HMAC-SHA256
* @param string $message
* @param string $key
* @return string
*/
function aes256ctr_hmacsha256_encrypt(string $message, string $key)
{
$iv = random_bytes(16);
$salt = random_bytes(16);
$eKey = hash_hkdf('sha256', $key, 32, 'Encryption Key', $salt);
$aKey = hash_hkdf('sha256', $key, 32, 'Authentication Key', $salt);
$ciphertext = $iv . $salt . openssl_encrypt(
$message,
'aes-256-ctr',
$eKey,
OPENSSL_RAW_DATA,
$iv
);
$mac = hash_hmac('sha256', $ciphertext, $aKey, true);
return base64_encode($mac . $ciphertext);
}
/**
* Decrypt a message with AES-256-CTR + HMAC-SHA256
* @param string $message
* @param string $key
* @return string
* @throws Exception
*/
function aes256ctr_hmacsha256_decrypt(string $ciphertext, string $key)
{
$decode = base64_decode($ciphertext);
if ($decode === false) {
throw new Exception("Encoding error");
}
$mac = mb_substr($decode, 0, 32, '8bit');
$iv = mb_substr($decode, 32, 16, '8bit');
$salt = mb_substr($decode, 48, 16, '8bit');
$ciphertext = mb_substr($decode, 64, null, '8bit');
$aKey = hash_hkdf('sha256', $key, 32, 'Authentication Key', $salt);
$calcMac = hash_hmac('sha256', $iv . $salt . $ciphertext, $aKey, true);
if (!hash_equals($calcMac, $mac)) {
throw new Exception("Invalid message");
}
$eKey = hash_hkdf('sha256', $key, 32, 'Encryption Key', $salt);
return openssl_decrypt(
$ciphertext,
'aes-256-ctr',
$eKey,
OPENSSL_RAW_DATA,
$iv
);
}
$key = random_bytes(32);
$message = "This code rocks";
$ciphertext = aes256ctr_hmacsha256_encrypt($message, $key);
var_dump($ciphertext);
$plaintext = aes256ctr_hmacsha256_decrypt($ciphertext, $key);
var_dump($plaintext);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 7.4.0 0.007 0.008 14.85 7.3.12 0.008 0.008 14.77 7.3.11 0.007 0.011 14.74 7.3.10 0.010 0.005 14.72 7.3.9 0.008 0.005 14.84 7.3.8 0.007 0.009 14.77 7.3.7 0.008 0.008 14.63 7.3.6 0.005 0.008 14.70 7.3.5 0.004 0.007 14.77 7.3.4 0.004 0.007 14.66 7.3.3 0.003 0.008 14.72 7.3.2 0.009 0.003 16.60 7.3.1 0.007 0.004 16.50 7.3.0 0.003 0.008 16.39 7.2.25 0.011 0.007 15.01 7.2.24 0.007 0.011 14.83 7.2.23 0.012 0.003 14.86 7.2.22 0.006 0.009 14.97 7.2.21 0.007 0.008 14.66 7.2.20 0.003 0.013 14.82 7.2.19 0.004 0.011 14.65 7.2.18 0.006 0.006 14.88 7.2.17 0.010 0.007 14.88 7.1.33 0.010 0.003 15.57 7.1.32 0.002 0.007 15.59 7.1.31 0.004 0.011 15.57 7.1.30 0.007 0.007 15.50 7.1.29 0.007 0.004 15.35 7.1.28 0.008 0.005 15.49 7.1.27 0.008 0.006 15.58 7.1.26 0.005 0.005 15.70 7.1.7 0.000 0.010 16.98 7.1.6 0.010 0.014 19.19 7.1.5 0.010 0.013 16.84 7.1.0 0.003 0.073 22.50 7.0.20 0.000 0.008 16.44 7.0.10 0.007 0.073 20.04 7.0.9 0.020 0.070 20.01 7.0.8 0.007 0.067 20.07 7.0.7 0.010 0.047 20.02 7.0.6 0.013 0.043 20.05 7.0.5 0.017 0.050 20.48 7.0.4 0.007 0.087 20.14 7.0.3 0.017 0.077 20.20 7.0.2 0.007 0.050 20.19 7.0.1 0.003 0.060 20.06 7.0.0 0.003 0.087 20.13 5.6.28 0.010 0.067 20.79 5.6.25 0.007 0.030 20.57 5.6.24 0.007 0.050 20.64 5.6.23 0.003 0.047 20.63 5.6.22 0.010 0.047 20.65 5.6.21 0.010 0.073 20.67 5.6.20 0.013 0.043 21.04 5.6.19 0.010 0.073 21.09 5.6.18 0.000 0.090 21.12 5.6.17 0.007 0.090 21.09 5.6.16 0.010 0.057 20.93 5.6.15 0.010 0.073 21.08 5.6.14 0.007 0.077 21.06 5.6.13 0.007 0.050 21.04 5.6.12 0.007 0.070 20.96 5.6.11 0.000 0.047 21.13 5.6.10 0.007 0.040 20.95 5.6.9 0.020 0.060 21.12 5.6.8 0.020 0.067 20.37 5.6.7 0.007 0.070 20.47 5.6.6 0.003 0.050 20.38 5.6.5 0.010 0.073 20.56 5.6.4 0.013 0.040 20.45 5.6.3 0.007 0.037 20.38 5.6.2 0.013 0.040 20.24 5.6.1 0.007 0.050 20.38 5.6.0 0.007 0.037 20.36 5.5.38 0.003 0.050 20.41 5.5.37 0.003 0.047 20.39 5.5.36 0.010 0.060 20.39 5.5.35 0.007 0.053 20.48 5.5.34 0.017 0.040 20.92 5.5.33 0.007 0.067 20.87 5.5.32 0.007 0.080 20.65 5.5.31 0.010 0.067 20.89 5.5.30 0.010 0.077 20.64 5.5.29 0.007 0.060 20.92 5.5.28 0.003 0.067 20.90 5.5.27 0.013 0.067 20.87 5.5.26 0.010 0.037 20.91 5.5.25 0.003 0.070 20.71 5.5.24 0.007 0.080 20.14 5.5.23 0.007 0.077 20.18 5.5.22 0.003 0.040 20.02 5.5.21 0.013 0.040 20.28 5.5.20 0.017 0.060 20.24 5.5.19 0.010 0.047 20.23 5.5.18 0.013 0.070 20.09 5.5.16 0.007 0.070 19.95 5.5.15 0.010 0.070 20.13 5.5.14 0.003 0.080 20.21 5.5.13 0.000 0.043 20.15 5.5.12 0.007 0.040 20.15 5.5.11 0.007 0.037 20.25 5.5.10 0.007 0.043 20.14 5.5.9 0.007 0.037 20.05 5.5.8 0.003 0.037 20.06 5.5.7 0.010 0.067 20.09 5.5.6 0.007 0.033 20.09 5.5.5 0.003 0.037 20.16 5.5.4 0.017 0.060 20.05 5.5.3 0.010 0.070 20.14 5.5.2 0.000 0.050 20.11 5.5.1 0.010 0.067 20.05 5.5.0 0.007 0.033 20.04
preferences:dark mode live preview
32.49 ms | 403 KiB | 5 Q