minimumKeyLength) { throw new InvalidArgumentException(sprintf( 'The key is %d bytes long; at least %d bytes are required', $keyLength, $this->minimumKeyLength )); } } public function isValid(Key $key): bool { try { $this->check($key); } catch (Throwable) { return false; } return true; } /** * @throws InvalidArgumentException when the key does not satisfy the constraints */ private static function keyValue(Key $key): string { if ($key->type() !== Key::TYPE_OCT && $key->type() !== Key::TYPE_NAME_OCT) { throw new InvalidArgumentException('Invalid key. Must be of type symmetric'); } if (! $key->has(SymmetricKey::DATA_K)) { throw new InvalidArgumentException('Invalid key. The value of the key is missing'); } $k = $key->get(SymmetricKey::DATA_K); if (! is_string($k)) { throw new InvalidArgumentException( 'Invalid key. The value of the key must be a byte string (CBOR objects shall be normalized first)' ); } if ($k === '') { throw new InvalidArgumentException('Invalid key. The value of the key is empty'); } return $k; } }