protected function signWithPrivateKey($keyPath, $source)
    {
        $file = @fopen($keyPath, 'r');
        if ($file === false) {
            throw new Exception('Could not open private key file. ');
        }

        $privateKey = fread($file, 8192);
        fclose($file);
        $signature = null;

        $key = openssl_pkey_get_private($privateKey);
        $isSignatureCorrect = openssl_sign($source, $signature, $key);
        openssl_free_key($key);

        if ($isSignatureCorrect === false) {
            throw new Exception('Could not sign with private key. ');
        }

        return $signature;
    }