Generating Encryption Key

You need a base64-encoded 256-bit encryption key for Unibrain

Below are examples of how to generate a base64-encoded 256-bit encryption key in a few different programming languages.

Python

import base64
import os

key = base64.b64encode(os.urandom(32)).decode('utf-8')
print(key)

JavaScript (Node.js)

const crypto = require('crypto');

const key = crypto.randomBytes(32).toString('base64');
console.log(key);

Java

import java.security.SecureRandom;
import java.util.Base64;

SecureRandom random = new SecureRandom();
byte[] key = new byte[32];
random.nextBytes(key);
String base64Key = Base64.getEncoder().encodeToString(key);
System.out.println(base64Key);

Go

package main

import (
	"crypto/rand"
	"encoding/base64"
	"fmt"
)

func main() {
	key := make([]byte, 32)
	_, err := rand.Read(key)
	if err != nil {
		panic(err)
	}
	base64Key := base64.StdEncoding.EncodeToString(key)
	fmt.Println(base64Key)
}

Ruby

require 'securerandom'

key = SecureRandom.random_bytes(32)
base64_key = [key].pack('m0')
puts base64_key

Each example generates a random 256-bit key, encoded in Base64, suitable for use in both storing context and searching context endpoints.

Last updated