Skip to content

secure and chance

Kova has two kinds of chance, on purpose:

  • random is fast and can be repeated: with the same KOVA_SEED, a program draws the same numbers, which makes games, simulations and tests reproducible. It must never protect anything: its numbers can be guessed.
  • secure asks the system for chance that cannot be guessed, for passwords, keys and tokens.

No import needed.

FunctionGives
random(a..b) -> Inta whole number from a up to, not including, b; random(a..=b) includes b.
l.shuffled, l.chosena list in random order, one element drawn (List).
fn main {
let die: Int := random(1..=6)
print(die >= 1 && die <= 6)
}
true

KOVA_SEED=42 kova run game.kova draws the same numbers at every run, built or not.

import secure
FunctionGives
secure.random(a..b) -> Intlike random, from the system’s own chance.
secure.token(bytes: Int) -> Stringthat many bytes of chance, in hexadecimal (token(16) is 32 characters): a session key, a reset link.
secure.uuid -> Stringa random UUID (version 4), "3f2b…-…", 36 characters.

A password is never stored, nor its SHA-256: a stolen list of fingerprints is broken in hours. Store what secure.password_hash gives, and check a password against it:

import secure
fn main {
let stored: String := secure.password_hash("correct horse")
print(secure.password_matches("correct horse") hash(stored))
print(secure.password_matches("wrong horse") hash(stored))
}
true
false
FunctionGives
secure.password_hash(password) -> Stringan Argon2id hash to keep, with its own random salt.
secure.password_hash(password) memory(mb) passes(n)the same with more cost (8 to 1024 MB, 1 to 16 passes).
secure.password_matches(password) hash(stored) -> Boolwhether password is the one stored was made from.

Argon2id is slow and memory-hungry on purpose (19 MB and 2 passes by default), so each guess costs an attacker the same. The text keeps its settings, so they can be raised later without making the stored hashes invalid.

import secure
fn main {
let key: String := secure.key
let code: String := secure.encrypt("meet at noon") key(key)
print(secure.decrypt(code) key(key))
print(secure.decrypt(code) key(secure.key))
}
"meet at noon"
none
FunctionGives
secure.key -> Stringa new random key, as text, to keep secret.
secure.encrypt(text) key(k) -> Stringa code only that key opens (XChaCha20-Poly1305).
secure.decrypt(code) key(k) -> String?the text back; none if the key is another, or if the code was changed.

The same text encrypted twice gives two different codes, and a code that was changed, even by one character, is refused rather than giving a wrong text.

s.sha256, files.sha256(path) and s.hmac_sha256(key) are in String and files.

Every algorithm here (SHA-256, HMAC, BLAKE2b, Argon2id, XChaCha20-Poly1305) is written in Kova’s runtime, checked against the examples of their standards and against reference libraries, and works the same on every system.