ParagonIE\Halite\Symmetric\Crypto::verify PHP Method

verify() public static method

Verify the authenticity of a message, given a shared MAC key
public static verify ( string $message, AuthenticationKey $secretKey, string $mac, mixed $encoding = Halite::ENCODE_BASE64URLSAFE, SymmetricConfig $config = null ) : boolean
$message string
$secretKey AuthenticationKey
$mac string
$encoding mixed
$config SymmetricConfig
return boolean
    public static function verify(string $message, AuthenticationKey $secretKey, string $mac, $encoding = Halite::ENCODE_BASE64URLSAFE, SymmetricConfig $config = null) : bool
    {
        $decoder = Halite::chooseEncoder($encoding, true);
        if ($decoder) {
            // We were given hex data:
            $mac = $decoder($mac);
        }
        if ($config === null) {
            // Default to the current version
            $config = SymmetricConfig::getConfig(Halite::HALITE_VERSION, 'auth');
        }
        return self::verifyMAC($mac, $message, $secretKey->getRawKeyMaterial(), $config);
    }

Usage Example

Example #1
0
 /**
  * @covers Symmetric::authenticate()
  * @covers Symmetric::verify()
  */
 public function testAuthenticateFail()
 {
     $key = new AuthenticationKey(new HiddenString(\str_repeat('A', 32), true));
     $message = 'test message';
     $mac = Symmetric::authenticate($message, $key, true);
     // Test invalid message
     $this->assertFalse(Symmetric::verify('othermessage', $key, $mac, true));
     $r = \Sodium\randombytes_uniform(\mb_strlen($mac, '8bit'));
     $_mac = $mac;
     $_mac[$r] = \chr(\ord($_mac[$r]) ^ 1 << \Sodium\randombytes_uniform(8));
     // Test invalid signature
     $this->assertFalse(Symmetric::verify($message, $key, $_mac, true));
 }