public function sign($signWith, $key)
{
$hashType = $this->getHeader()->getAlgorithm();
$hash = '\\Psecio\\Jwt\\HashMethod\\' . $hashType;
if (class_exists($hash) === false) {
throw new \InvalidArgumentException('Invalid hash type: ' . $hashType);
}
$hash = new $hash();
if ($hash->getKeyType() === 'HMAC') {
$signature = hash_hmac($hash->getAlgorithm(), $signWith, $key, true);
} else {
if ($hash->isValidKey($key) === false) {
throw new \Psecio\Jwt\Exception\InvalidKeyException('Invalid key provided');
}
openssl_sign($signWith, $signature, $key, $hash->getAlgorithm());
}
if ($signature === false) {
throw new \Psecio\Jwt\Exception\SignatureErrorException('Error signing with provided key');
}
return $signature;
}