protected function sign($privateKey, $parameters)
    {
        if (preg_match('/-----BEGIN (RSA )?PRIVATE KEY-----(.*)-----END (RSA )?PRIVATE KEY-----/si', $privateKey, $matches)) {
            $key = $matches[2];
            $key = preg_replace('/\s*/s', '', $key);
            $key = chunk_split($key, 64, "\n");

            $key = "-----BEGIN PRIVATE KEY-----\n".$key.'-----END PRIVATE KEY-----';

            $digest = $this->sha512Asn1($this->encodeParameters($parameters));

            if (@openssl_private_encrypt($digest, $signature, $key)) {
                return base64_encode($signature);
            }

            throw new \InvalidArgumentException('Unable to sign the request, this has to do with the provided (invalid) private key.');
        }

        throw new \InvalidArgumentException('Invalid private key.');
    }