public static function fromString(string $str): self
    {
        if (!preg_match(self::PEM_REGEX, $str, $match)) {
            throw new \UnexpectedValueException("Not a PEM formatted string.");
        }
        $payload = preg_replace('/\s+/', "", $match[2]);
        $data = base64_decode($payload, true);
        if (false === $data) {
            throw new \UnexpectedValueException("Failed to decode PEM data.");
        }
        return new self($match[1], $data);
    }