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.
- Go
- Node.js
- PHP
- Python
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[:]))
}
info
Recommend Node.js version is >= 15.
const crypto = require('crypto');
const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', {
modulusLength: 2048,
publicKeyEncoding: {
type: 'pkcs1',
format: 'der'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'der'
}
});
console.log('The private key is: ', privateKey.toString('hex'));
console.log();
console.log('The public key is: ', publicKey.toString('base64'));
console.log();
console.log('Api Key Base64 is: ', crypto.createHash('sha256').update(publicKey).digest('base64'));
info
Recommend PHP version is 8.
<?php
require __DIR__ . "/vendor/autoload.php";
use phpseclib3\Crypt\RSA;
function pemToDer($pem) {
$lines = explode("\n", trim($pem));
unset($lines[count($lines)-1]);
unset($lines[0]);
$result = implode('', $lines);
$result = base64_decode($result);
return $result;
}
$privateKey = RSA::createKey();
$publicKey = $privateKey->getPublicKey()->toString('PKCS1');
$privateKey = pemToDer($privateKey);
$publicKey = str_replace(["\r", "\n", "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A"],"", preg_replace("/--.*--/","", $publicKey));
echo "The private key is: " . bin2hex($privateKey) . "\n\n";
echo "The public key is: " . $publicKey . "\n\n";
echo "SHA256 hash of your public key in Base64 format is: ". base64_encode(hash("sha256", $publicKey, true)) . "\n";
info
Recommend Python version is >= 3.9.
import base64
from cryptography.hazmat.primitives import serialization, hashes
from cryptography.hazmat.primitives.asymmetric import rsa
private_key: rsa.RSAPrivateKey = rsa.generate_private_key(
public_exponent=65537,
key_size=2048
)
private_key_der: bytes = private_key.private_bytes(
encoding=serialization.Encoding.DER,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption(),
)
public_key_der: bytes = private_key.public_key().public_bytes(
encoding=serialization.Encoding.DER,
format=serialization.PublicFormat.PKCS1,
)
private_key: str = private_key_der.hex()
public_key: str = base64.b64encode(public_key_der).decode()
public_key_hash = hashes.Hash(hashes.SHA256())
public_key_hash.update(public_key_der)
api_key: bytes = public_key_hash.finalize()
api_key_base64: str = base64.b64encode(api_key).decode()
print(
f"""
The private key is: {private_key}
The public key is: {public_key}
SHA256 hash of your public key in Base64 format is: {api_key_base64}
"""
)