Skip to main content
Cloacina Documentation
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

API Keys

cloacina::security::api_keys Rust

API key generation and hashing utilities.

Functions

cloacina::security::api_keys::generate_api_key

pub

fn generate_api_key () -> (String , String)

Generates a new API key, returning (plaintext, hash).

The plaintext has the form clk_ followed by 32 random bytes encoded as base64url (no padding). The hash is the lowercase hex SHA-256 digest of the full plaintext string.

Source
pub fn generate_api_key() -> (String, String) {
    let mut rng = rand::thread_rng();
    let mut bytes = [0u8; 32];
    rng.fill(&mut bytes);

    let plaintext = format!("clk_{}", URL_SAFE_NO_PAD.encode(bytes));
    let hash = hash_api_key(&plaintext);
    (plaintext, hash)
}

cloacina::security::api_keys::hash_api_key

pub

fn hash_api_key (key : & str) -> String

Returns the lowercase hex SHA-256 hash of an API key string.

Source
pub fn hash_api_key(key: &str) -> String {
    let mut hasher = Sha256::new();
    hasher.update(key.as_bytes());
    format!("{:x}", hasher.finalize())
}