DEV Community

Aditya Sharma
Aditya Sharma

Posted on

Forget JWTs β€” Meet SwiftToken: Fast, Secure Tokens in Go

πŸ” SwiftToken β€” Fast, Secure Token Auth for Go

Hey Gophers! πŸ‘‹

I just released SwiftToken β€” a blazing-fast πŸ”₯ and secure πŸ” token library for Go, built as an encrypted, lightweight alternative to JWT.


πŸš€ Why SwiftToken?

JWTs are powerful, but they’re bulky and slow.

SwiftToken is:

  • ⚑ Up to 70x faster than standard JWT
  • πŸ”’ Encrypted using ChaCha20-Poly1305
  • πŸ“¦ Ultra-compact via MessagePack
  • πŸ” Supports sliding tokens with refresh logic
  • πŸ§ͺ Fully tested with edge cases
  • πŸ“Š Benchmarked against JWT

πŸ“¦ Installation

go get github.com/Aditya251610/swifttoken@v1.0.0
Enter fullscreen mode Exit fullscreen mode

βš™οΈ Setup

Use a 32-byte secret key.

βœ… Option 1: via .env

SWIFTTOKEN_SECRET=12345678901234567890123456789012
Enter fullscreen mode Exit fullscreen mode

βœ… Option 2: via code

os.Setenv("SWIFTTOKEN_SECRET", "12345678901234567890123456789012")
Enter fullscreen mode Exit fullscreen mode

πŸ”§ Usage

Generate and Verify Token

package main

import (
  "fmt"
  "os"
  "time"

  "github.com/Aditya251610/swifttoken/token"
  "github.com/Aditya251610/swifttoken/types"
)

func main() {
  os.Setenv("SWIFTTOKEN_SECRET", "12345678901234567890123456789012")

  payload := types.Payload{
    Sub:         "user-123",
    IssuedAt:    time.Now().Unix(),
    ExpiresAt:   time.Now().Add(30 * time.Minute).Unix(),
    SessionID:   "sess-001",
    Permissions: []string{"read", "write"},
    Sliding:     true,
    Nonce:       "random-nonce",
  }

  tokenBytes, err := token.GenerateToken(payload)
  if err != nil {
    panic("Token generation failed: " + err.Error())
  }

  fmt.Println("πŸ” Token:", tokenBytes)

  decoded, shouldRefresh, err := token.VerifyToken(tokenBytes)
  if err != nil {
    fmt.Println("❌ Invalid token:", err)
    return
  }

  fmt.Printf("βœ… Token valid for: %s\n", decoded.Sub)
  if shouldRefresh {
    fmt.Println("♻️ Token should be refreshed")
  }
}
Enter fullscreen mode Exit fullscreen mode

πŸ“Š Benchmarks

go test -bench=. ./benchmarks -benchmem
Enter fullscreen mode Exit fullscreen mode
Function SwiftToken JWT (standard)
Generate Token ~669 ns/op ~4331 ns/op
Verify Token ~82 ns/op ~5834 ns/op
Allocations 6 49–70
Payload Size Small (binary) Large (Base64)

πŸ› οΈ Roadmap

  • [ ] CLI tool
  • [ ] Token revocation
  • [ ] Introspection endpoint
  • [ ] Plug-in storage backends
  • [ ] SDK for frontend/mobile

🀝 Contributing

  1. ⭐ Star the repo
  2. 🍴 Fork it
  3. git checkout -b feat/my-feature
  4. git commit -m "✨ add feature"
  5. Open a PR βœ…

πŸ“„ License

MIT β€” by Aditya Sharma


πŸ”— GitHub: github.com/Aditya251610/swifttoken

SwiftToken β€” built for developers who care about performance, security, and simplicity.

Top comments (0)