π 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
βοΈ Setup
Use a 32-byte secret key.
β
Option 1: via .env
SWIFTTOKEN_SECRET=12345678901234567890123456789012
β Option 2: via code
os.Setenv("SWIFTTOKEN_SECRET", "12345678901234567890123456789012")
π§ 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")
}
}
π Benchmarks
go test -bench=. ./benchmarks -benchmem
| 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
- β Star the repo
- π΄ Fork it
-
git checkout -b feat/my-feature -
git commit -m "β¨ add feature" - 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)