protected function getKeyResource($key, $password = null)
    {
        if (is_resource($key)) {
            return $key;
        }

        $resource = openssl_pkey_get_public($key) ?: openssl_pkey_get_private($key, $password);
        if ($resource === false) {
            throw new RuntimeException('Could not read key resource: ' . openssl_error_string());
        }
        return $resource;
    }