How can do Http request with openssl sign on golang

Viewed 273

We have a private rsa key and a secret phrase. How can I make a request signed with this?

I know how to make a request signed with a certificate, and it works, for example:

cert, err := tls.LoadX509KeyPair(path+"user.crt", path+"user.key")
if err != nil {
    log.Fatal(err)
}
tlsConfig := &tls.Config{
        Certificates: []tls.Certificate{cert},
    }
t := &http.Transport{
        TLSClientConfig: tlsConfig,
    }
client := &http.Client{
        Transport: t,
    }
.....
.....
resp, err := client.Do(request)

it worked fine…

but how can I do the same thing on Golang? Here is a example done on php:

....
$data = json_encode($data);

    $privateKey = openssl_pkey_get_private(
        file_get_contents(__DIR__ . 'private.pem'),
        file_get_contents(__DIR__ . 'password.txt')
    );
openssl_sign($data, $sign, $privateKey);
    $sign = base64_encode($sign);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'apiurl');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Sign: ' . $sign,
    ]);

How we can get sign on Golang?

0 Answers
Related