arcbjorn/paseto-zig
Zig implementation of PASETO (Platform-Agnostic SEcurity TOkens)
A secure, type-safe implementation of PASETO (Platform-Agnostic Security Tokens) v4 and PASERK (Platform-Agnostic Serialized Keys) for Zig.
const std = @import("std");
const paseto = @import("paseto");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Generate keys
var local_key = paseto.LocalKey.generate();
defer local_key.deinit();
// Create token with secure defaults (1-hour expiration)
var builder = paseto.createLocalBuilder(allocator);
defer builder.deinit();
_ = try builder.withDefaults();
_ = try builder.setIssuer("myapp");
_ = try builder.setSubject("user123");
const token = try builder.buildLocal(&local_key);
defer allocator.free(token);
// Verify token
var parser = paseto.createParser(allocator);
var claims = try parser.parseLocal(token, &local_key, null, null);
defer claims.deinit(allocator);
std.debug.print("Issuer: {s}\n", .{claims.issuer.?});
}
Add to your build.zig
:
const paseto = b.dependency("paseto", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("paseto", paseto.module("paseto"));
Symmetric encryption for trusted environments:
Digital signatures for distributed systems:
Complete key management with serialization formats:
// Serialize keys
const local_paserk = try paseto.paserk.serializeLocalKey(allocator, &key);
const public_paserk = try paseto.paserk.serializePublicKey(allocator, &keypair.public);
// Key identifiers
const lid = paseto.LocalKeyId.fromLocalKey(key.bytes());
const lid_paserk = try lid.serialize(allocator);
// Password-based key wrapping
const wrapped = try paseto.paserk.password.wrapLocalKeyWithPassword(
allocator, &key, "password", .{}
);
createLocalBuilder()
/ createPublicBuilder()
- Token creationcreateParser()
- Token verificationLocalKey.generate()
/ KeyPair.generate()
- Key generationwithDefaults()
- Apply secure defaultssetIssuer()
, setSubject()
, setAudience()
- Standard claimssetExpiration()
, setIssuedAt()
- Time-based claimssetClaim()
, setClaimNumber()
- Custom claimssetFooter()
, setImplicit()
- Additional authenticated dataparseLocal()
/ parsePublic()
- Token verificationsetValidateTime()
- Enable/disable time validationsetLeeway()
- Clock skew tolerancezig build test # Run test suite
zig build example # Run example program
MIT License - see LICENSE file for details.
Issues and pull requests welcome. Please ensure all tests pass and follow existing code style.