Cryptography
hive.crypto is pure, so it can be called from a func as well as a proc.
sha256(input)andsha512(input)hash, andhmacSha256(input, key)authenticates with aSecret<Str>key. Each answers in lowercase hex.encrypt(plaintext, password)seals aSecret<Str>under aSecret<Str>password with AES-256-GCM. The key is stretched with 600,000 rounds of PBKDF2, and every call uses a fresh salt and nonce, so the same text never encrypts the same way twice. The ciphertext is aStr, safe to print and store.decrypt(ciphertext, password)opens it into aSecret<Str>, so the plaintext is never aStr;decryptBypass(ciphertext, password)opens it into ordinary memory, asbypassis tohide, so it has no locked-memory limit to hit.base64Encode(input)andbase64Decode(input)use standard base64 with padding.randomHex(bytes)draws that many random bytes and answers with twice as many hex digits;randomSecret(bytes)puts them straight into aSecret<Str>and answers with aResult<Secret<Str>, SecretError>.
JSON Web Tokens are a codec, jwtCodec(secret) with a Secret<Str>, used in the same two places as JSON's: encode(claims, hive.crypto.jwtCodec(secret)) signs a value of your own type, and Claims.decode(token, hive.crypto.jwtCodec(secret)) checks the signature before reading the claims back. Only HS256 is accepted, which rules out the classic algorithm-confusion attack. A token expires only if its claims type declares exp: Int (or nbf, not before) in Unix seconds, and those are then checked. Use hive.time.now() / 1000, since a timestamp is in milliseconds; encode adds neither field. A failure is a hive.crypto.JwtError whose path is $ for a token that does not verify, or a field's path for claims of the wrong shape. jwtDecode(token) reads the claims as JSON without verifying anything, for inspection only, and jwtHeader(token) reads alg, typ and kid. Tokens Hive signs carry a fixed header, so their kid is empty.
The calls that can fail (decrypt, decryptBypass, base64Decode, jwtDecode, jwtHeader) answer with a Result whose hive.crypto.CryptoError is Malformed, BadSignature (a wrong password, or a message someone changed) or, from decrypt when no locked memory was available, LimitExceeded or Unsupported, each with a message.
proc main(): void {
echo hive.crypto.sha256("hive")
echo hive.crypto.sha512("hive")
echo hive.crypto.hmacSha256("hive", bypass("secret"))
}proc main(): void {
password := bypass("password")
sealed := hive.crypto.encrypt(bypass("a secret"), password)
echo sealed
if hive.crypto.decryptBypass(sealed, password) is Result.Ok(plain) {
echo reveal(plain)
}
}proc main(): void {
encoded := hive.crypto.base64Encode("hive")
if hive.crypto.base64Decode(encoded) is Result.Ok(decoded) {
echo decoded
}
echo hive.crypto.randomHex(8)
if hive.crypto.randomSecret(8) is Result.Ok(key) {
echo len(reveal(key)) // 16
}
}type Claims { user: Str }
proc main(): void {
token := encode(Claims("ada"), hive.crypto.jwtCodec(bypass("secret")))
// Checks the signature and the times, then reads the claims.
if Claims.decode(token, hive.crypto.jwtCodec(bypass("secret"))) is Result.Ok(claims) {
echo claims.user
}
echo hive.crypto.jwtHeader(token)
}Run a program to see what it prints here.