Generate the private and public keys pair
First of all, you need to generate the private and public keys pair. The following code samples illustrate how to generate new keys.
- Node.js
- Python
- PHP
- Go
note
Node.js >= 15 is required.
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'));
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}
"""
)
note
PHP 8 is required.
<?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";
note
This code sample was created using Go 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[:]))
}
warning
Keep your private key in a safe place and don't share it with anyone, not even us.
You need to share with us only your:
- Public key;
- API key Base64.