From 4b7300de8d54930e6d22859487d67fbb034e8ca5 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Mon, 13 Aug 2018 19:56:20 +0100 Subject: [PATCH] Use openssl_digest() instead of hash() in network hot code openssl_digest() is TWICE as fast as hash() on my machine for the same data and same algorithm. I can only guess that OpenSSL is more optimized than PHP ext/standard :) --- src/pocketmine/network/mcpe/NetworkCipher.php | 2 +- src/pocketmine/network/mcpe/ProcessLoginTask.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pocketmine/network/mcpe/NetworkCipher.php b/src/pocketmine/network/mcpe/NetworkCipher.php index eb4c5f1aa..7e3bd0721 100644 --- a/src/pocketmine/network/mcpe/NetworkCipher.php +++ b/src/pocketmine/network/mcpe/NetworkCipher.php @@ -78,6 +78,6 @@ class NetworkCipher{ } private function calculateChecksum(int $counter, string $payload) : string{ - return substr(hash(self::CHECKSUM_ALGO, Binary::writeLLong($counter) . $payload . $this->key, true), 0, 8); + return substr(openssl_digest(Binary::writeLLong($counter) . $payload . $this->key, self::CHECKSUM_ALGO, true), 0, 8); } } diff --git a/src/pocketmine/network/mcpe/ProcessLoginTask.php b/src/pocketmine/network/mcpe/ProcessLoginTask.php index 29d5823fa..0ca99783f 100644 --- a/src/pocketmine/network/mcpe/ProcessLoginTask.php +++ b/src/pocketmine/network/mcpe/ProcessLoginTask.php @@ -101,7 +101,7 @@ class ProcessLoginTask extends AsyncTask{ $salt = random_bytes(16); $sharedSecret = $serverPriv->createExchange($clientPub)->calculateSharedKey(); - $this->aesKey = hash('sha256', $salt . hex2bin(str_pad(gmp_strval($sharedSecret, 16), 96, "0", STR_PAD_LEFT)), true); + $this->aesKey = openssl_digest($salt . hex2bin(str_pad(gmp_strval($sharedSecret, 16), 96, "0", STR_PAD_LEFT)), 'sha256', true); $this->handshakeJwt = $this->generateServerHandshakeJwt($serverPriv, $salt); }