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 :)
This commit is contained in:
Dylan K. Taylor 2018-08-13 19:56:20 +01:00
parent 59a51a6c75
commit 4b7300de8d
2 changed files with 2 additions and 2 deletions

View File

@ -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);
}
}

View File

@ -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);
}