Пример #1
0
 /**
  * Authenticate a string
  * 
  * @param string $message
  * @param AuthenticationKey $secretKey
  * @param boolean $raw
  * @throws CryptoException\InvalidKey
  * @return string
  */
 public static function authenticate($message, Contract\KeyInterface $secretKey, $raw = false)
 {
     if (!$secretKey instanceof AuthenticationKey) {
         throw new CryptoException\InvalidKey('Expected an instnace of AuthenticationKey');
     }
     if ($secretKey->isAsymmetricKey()) {
         throw new CryptoException\InvalidKey('Expected a symmetric key, not an asymmetric key');
     }
     if (!$secretKey->isSigningKey()) {
         throw new CryptoException\InvalidKey('Authentication key expected');
     }
     $config = SymmetricConfig::getConfig(Halite::HALITE_VERSION, 'auth');
     $mac = self::calculateMAC($message, $secretKey->get(), $config);
     if ($raw) {
         return $mac;
     }
     return \Sodium\bin2hex($mac);
 }