The standard librarySlide 71 of 86
71cryptoThe standard library

Cryptography

hive.crypto is pure, so it can be called from a func as well as a proc.

  • sha256(input) and sha512(input) hash, and hmacSha256(input, key) authenticates with a Secret<Str> key. Each answers in lowercase hex.
  • encrypt(plaintext, password) seals a Secret<Str> under a Secret<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 a Str, safe to print and store. decrypt(ciphertext, password) opens it into a Secret<Str>, so the plaintext is never a Str; decryptBypass(ciphertext, password) opens it into ordinary memory, as bypass is to hide, so it has no locked-memory limit to hit.
  • base64Encode(input) and base64Decode(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 a Secret<Str> and answers with a Result<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.

Hashing
proc main(): void {
	echo hive.crypto.sha256("hive")
	echo hive.crypto.sha512("hive")
	echo hive.crypto.hmacSha256("hive", bypass("secret"))
}
Encryption
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)
	}
}
Encoding and random bytes
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
	}
}
JWTs
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)
}