Generate a random API key in Rust

Published: Sep 28, 2024

I'm slowly going to migrate my saas product commentingsystem.com from Next.js to Rust using the Rust web framework Rocket.

One of the task I needed to clone in Rust was generating a random public api key. From the Rust Cookbook I found this resource, in the section titled Create random passwords from a set of alphanumeric characters.

use rand::{thread_rng, Rng};
use rand::distributions::Alphanumeric;

fn main() {
    let rand_string: String = thread_rng()
        .sample_iter(&Alphanumeric)
        .take(30)
        .map(char::from)
        .collect();

    println!("{}", rand_string);
}

Here's how I'm using it in my Rocket web application.

use rand::{thread_rng, Rng};
use rand::distributions::Alphanumeric;

#[post("/")]
async fn register_domain() -> Template {
    let public_api_key = generate_a_random_public_api_key();

    Template::render("dashboard/register-domain", context! { public_api_key })
}

fn generate_a_random_public_api_key() -> String {
    let rand_string: String = thread_rng()
        .sample_iter(&Alphanumeric)
        .take(30)
        .map(char::from)
        .collect();

    rand_string
}

Comments • 0