@ 2017-12-14T14:27:53Z <?php
$password = 'My Plaintext Password';
$plaintext = 'This is my message';
$secretBox = new Secretbox();
$ciphertext = $secretBox->encrypt($plaintext, $password);
var_dump($ciphertext);
$decryptedPlaintext = $secretBox->decrypt($ciphertext, $password);
var_dump($decryptedPlaintext);
assertThat($decryptedPlaintext === $plaintext, 'Can decrypt');
assertThat(
$secretBox->encrypt($plaintext, $password) !== $secretBox->encrypt($plaintext, $password),
'Should not result in same ciphertext'
);
try {
$failed = false;
// modify ciphertext
$ciphertext[65] = 'f';
$secretBox->decrypt($ciphertext, $password);
} catch (\Exception $e) {
$failed = true;
} finally {
assertThat($failed, 'Tempered chiphertext should result in exception');
}
class Secretbox
{
public function encrypt(string $plaintext, string $password): string
{
// create a random salt for key derivation
$salt = random_bytes(SODIUM_CRYPTO_PWHASH_SALTBYTES);
$key = $this->deriveKeyFromUserPassword($password, $salt);
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
$ciphertext = sodium_crypto_secretbox($plaintext, $nonce, $key);
sodium_memzero($password);
sodium_memzero($key);
return $salt.$nonce.$ciphertext;
}
public function decrypt(string $ciphertext, string $password): string
{
$salt = substr($ciphertext, 0, SODIUM_CRYPTO_PWHASH_SALTBYTES);
$nonce = substr($ciphertext, SODIUM_CRYPTO_PWHASH_SALTBYTES, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
$ciphertext = substr($ciphertext, SODIUM_CRYPTO_PWHASH_SALTBYTES + SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
$key = $this->deriveKeyFromUserPassword($password, $salt);
$plaintext = sodium_crypto_secretbox_open($ciphertext, $nonce, $key);
sodium_memzero($password);
sodium_memzero($key);
sodium_memzero($nonce);
if ($plaintext === false) {
throw new \InvalidArgumentException('Bad ciphertext');
}
return $plaintext;
}
private function deriveKeyFromUserPassword(string $password, string $salt): string
{
$key = sodium_crypto_pwhash(
SODIUM_CRYPTO_SECRETBOX_KEYBYTES,
$password,
$salt,
SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE,
SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE
);
sodium_memzero($password);
return $key;
}
}
function assertThat(bool $expressionResult, string $message) {
if (!$expressionResult) {
throw new \RuntimeException($message);
}
}
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.083 0.553 46.82 7.3.12 0.106 0.650 47.17 7.3.11 0.090 0.691 47.01 7.3.10 0.076 0.537 46.92 7.3.9 0.076 0.550 47.10 7.3.8 0.073 0.577 46.86 7.3.7 0.073 0.553 47.00 7.3.6 0.066 0.607 46.64 7.3.5 0.083 0.518 46.79 7.3.4 0.040 0.504 46.98 7.3.3 0.066 0.541 46.94 7.3.2 0.100 0.506 48.62 7.3.1 0.066 0.531 48.59 7.3.0 0.080 0.451 48.69 7.2.25 0.076 0.641 47.12 7.2.24 0.093 0.693 47.11 7.2.23 0.063 0.537 47.05 7.2.22 0.066 0.521 47.16 7.2.21 0.060 0.510 47.09 7.2.20 0.093 0.619 47.26 7.2.19 0.073 0.513 47.00 7.2.18 0.060 0.497 47.09 7.2.17 0.070 0.575 47.10 7.2.0 0.134 0.802 84.09 7.1.33 0.000 0.015 15.82 7.1.32 0.000 0.011 15.80 7.1.31 0.003 0.010 15.70 7.1.30 0.003 0.008 15.79 7.1.29 0.008 0.003 15.44 7.1.28 0.005 0.007 15.89 7.1.27 0.003 0.006 15.68 7.1.26 0.008 0.004 15.81 7.1.12 0.006 0.008 18.69 7.1.11 0.006 0.007 18.24 7.1.10 0.009 0.006 17.88 7.1.9 0.006 0.006 18.11 7.1.8 0.006 0.009 18.34 7.1.7 0.006 0.009 17.02 7.1.6 0.013 0.013 35.18 7.1.5 0.010 0.016 34.94 7.1.4 0.013 0.013 34.39 7.1.3 0.013 0.013 34.42 7.1.2 0.017 0.007 34.63 7.1.1 0.006 0.006 16.45 7.1.0 0.006 0.009 16.67
preferences:dark mode live preview ace vim emacs key bindings
36.74 ms | 403 KiB | 5 Q