/** * @requires extension libsodium */ public function testStringCompare() { $this->assertTrue(Helpers::stringCompare('test', 'test')); $this->assertFalse(Helpers::stringCompare('test', 'nottest')); }
/** * @param string $message * @param string $signature * @param string $signer_public * @return bool * @throws SignatureException * @throws InvalidTypeException */ public static function verify($message, $signature, $signer_public) { # Test to make sure all the required variables are strings. Helpers::isString($message, 'PublicKeyEncryption', 'verify'); Helpers::isString($signature, 'PublicKeyEncryption', 'verify'); Helpers::isString($signer_public, 'PublicKeyEncryption', 'verify'); # Decode the signature from hex $signature = base64_decode(Helpers::hex2bin($signature)); # Decode the signer's public key from hex $signer_public = Helpers::hex2bin($signer_public); if (\Sodium\crypto_sign_verify_detached($signature, $message, $signer_public)) { return true; } else { throw new SignatureException('Signature for message invalid.'); } }
/** * @depends testNonceSize */ public function testNonceFastForward() { $this->assertTrue(Helpers::nonceCheck(Helpers::hex2bin($this->nonceData['new']), Helpers::hex2bin($this->nonceData['old'])) === 'MSG_FAST_FORWARD'); }
/** * Returns a random integer to the client. * * @param int $range Upper limit of random numbers to return to the client. * @return int * @throws Exceptions\InvalidTypeException * @throws Exceptions\OutOfRangeException */ static function integer($range = Constants::RANGE) { # Test the length for validity. Helpers::rangeCheck($range, Constants::RANGE_MAX, Constants::RANGE_MIN, 'Entropy', 'integer'); return \Sodium\randombytes_uniform($range) + 1; }