A MoonBit library for JSON Web Token (JWT) creation and verification. Pure MoonBit SHA256 + HMAC implementation, no FFI.
import {
"123123213weqw/moonbit_jwt" @jwt,
}
fn main {
// Build claims
let claims = @jwt.ClaimsBuilder::new()
|> @jwt.ClaimsBuilder::issuer("my-app")
|> @jwt.ClaimsBuilder::subject("user123")
|> @jwt.ClaimsBuilder::expires_at(2000000000)
|> @jwt.ClaimsBuilder::to_json_string()
// Sign with HS256
let token = @jwt.sign_hs256(@jwt.JwtHeader::hs256(), claims, "secret".to_bytes())
// Validate with full options
let opts = @jwt.ValidationOptions::new(1000000000)
|> @jwt.ValidationOptions::with_issuer("my-app")
|> @jwt.ValidationOptions::with_leeway(30)
match @jwt.validate(token, "secret".to_bytes(), opts) {
Ok(_) => println("Valid!")
Err(_) => println("Invalid!")
}
}| Module | Lines | Description |
|---|---|---|
| sha256.mbt | 170 | SHA-256 (FIPS 180-4), streaming + one-shot |
| sha512.mbt | 241 | SHA-512 + SHA-384 (truncated SHA-512) |
| hmac.mbt | 30 | HMAC-SHA256 (RFC 2104) |
| hmac_full.mbt | 61 | HMAC-SHA384 + HMAC-SHA512 |
| base64url.mbt | 128 | Base64 URL-safe encode/decode (RFC 4648) |
| jwt.mbt | 143 | JWT types, sign HS256, verify, decode_claims |
| jwt_full.mbt | 118 | HS384/HS512 sign + verify_with_alg |
| claims.mbt | 187 | ClaimsBuilder for structured claim construction |
| validator.mbt | 191 | Full validation (exp/nbf/iss/aud/sub + leeway) |
| decoder.mbt | 144 | Full token decode with header/claims extraction |
| Total | ~1900 |
| Algorithm | Hash | Status |
|---|---|---|
| HS256 | HMAC-SHA256 | ✅ |
| HS384 | HMAC-SHA384 | ✅ |
| HS512 | HMAC-SHA512 | ✅ |
moon fmt --check
moon check --deny-warn
moon test --deny-warn
moon test --target all
moon infopub(all) struct ValidationOptions {
expected_issuer : String?
expected_audience : String?
expected_subject : String?
now_time : Int
leeway : Int
}fn base64url_encode(data : Bytes) -> Stringfn base64url_encode_str(s : String) -> Stringfn hmac_sha256(key : Bytes, message : Bytes) -> Bytesfn hmac_sha384(key : Bytes, message : Bytes) -> Bytesfn hmac_sha512(key : Bytes, message : Bytes) -> Bytesfn is_expired(token : String, now : Int, leeway : Int) -> Boolfn sha256_hex(data : String) -> Stringfn sign_with_alg(alg : Algorithm, header_json : String, claims_json : String, secret : Bytes) -> StringA MoonBit library for JSON Web Token (JWT) creation and verification. Pure MoonBit SHA256 + HMAC implementation, no FFI.