Slide 32 of 37
Cryptography
Hashing
proc main(): void {
echo hive.crypto.sha256("hive")
echo hive.crypto.sha512("hive")
echo hive.crypto.hmacSha256("hive", "secret")
}
Encryption
proc main(): void {
sealed := hive.crypto.encrypt("a secret", "password")
if hive.crypto.decrypt(sealed, "password") is Result.Ok(plain) {
echo 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)
}
JWTs
type Claims { user: Str }
proc main(): void {
token := hive.crypto.jwtSign(Claims("ada"), "secret")
verified := hive.crypto.jwtVerify(token, "secret") with Claims
if verified is Result.Ok(claims) {
echo claims.user
}
echo hive.crypto.jwtHeader(token)
}
hive.crypto is pure, so it works in a func as well as a proc. sha256 and sha512 hash, and hmacSha256 authenticates with a key. encrypt and decrypt seal a string under a password with AES-256-GCM, using a fresh salt and nonce every call so the same text never encrypts alike twice; base64Encode and base64Decode move between text and base64, and randomHex draws bytes at random. jwtSign, jwtVerify and jwtDecode cover HS256 JSON Web Tokens end to end — jwtVerify checks the signature and expiry and accepts only HS256, closing off the classic algorithm-confusion attack outright, while jwtDecode reads the claims without verifying anything, and jwtHeader reads just alg/typ/kid.