Skip to main content

Generate a key pair


Generating a new key pair is a crucial step for Exchange API integration.

warning

Keep your private key in a safe place. Strictly do not share it with anyone, even with us.
For registration, we require only your public key and API key in Base64.


Code samples

The following code samples illustrate how to generate new keys.

info

Recommend Go version is 1.21.1.

package main

import (
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"fmt"
)

func main() {
// Generate RSA key pair
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
panic(err)
}

// Get DER formatted private key using PKCS8
privateDER, err := x509.MarshalPKCS8PrivateKey(privateKey)
if err != nil {
panic(err)
}
fmt.Println("The private key is:")
fmt.Println(hex.EncodeToString(privateDER))
fmt.Println()

// Get DER formatted public key using PKCS1
publicDER := x509.MarshalPKCS1PublicKey(&privateKey.PublicKey)
fmt.Println("The public key is:")
fmt.Println(base64.StdEncoding.EncodeToString(publicDER))
fmt.Println()

// Create SHA-256 hash of the public key and encode in Base64
hash := sha256.Sum256(publicDER)
fmt.Println("Api Key Base64 is:")
fmt.Println(base64.StdEncoding.EncodeToString(hash[:]))
}