/** * Returns a new set of keys for message encryption and signing. * * @param string $seed The seed to use to create repeatable keys. * @param string $hashKey The key to hash the key with. * @return array */ public static function generateKeys($seed = null, $hashKey = '') { # The keys are being generated from a seed. if ($seed !== null) { # Generate some repeatable hashes to create keys against for recovery $encrHash = Hash::hash($seed, $hashKey, Constants::BOX_SEEDBYTES); $signHash = Hash::hash($seed, $hashKey, Constants::SIGN_SEEDBYTES); # Build recoverable pre-seeded key pairs. $seeds = ['encr' => \Sodium\crypto_box_keypair($encrHash), 'sign' => \Sodium\crypto_sign_keypair($signHash)]; } else { # Build un-recoverable key pairs. $seeds = ['encr' => \Sodium\crypto_box_keypair(), 'sign' => \Sodium\crypto_sign_keypair()]; } # Return the two generated key pairs to the client. return ['encr' => ['pri' => Helpers::bin2hex(\Sodium\crypto_box_secretkey($seeds['encr'])), 'pub' => Helpers::bin2hex(\Sodium\crypto_box_publickey($seeds['encr']))], 'sign' => ['pri' => Helpers::bin2hex(\Sodium\crypto_sign_secretkey($seeds['sign'])), 'pub' => Helpers::bin2hex(\Sodium\crypto_sign_publickey($seeds['sign']))]]; }
/** * @depends testGenericHash */ public function testHashLength() { $this->assertTrue(strlen(Hash::hash('test', '', Constants::GENERICHASH_BYTES_MAX)) === Constants::GENERICHASH_BYTES_MAX); $this->assertTrue(strlen(Hash::hash('test', '', Constants::GENERICHASH_BYTES_MIN)) === Constants::GENERICHASH_BYTES_MIN); }