pw-punch
Version:
๐ Ultra-lightweight password hashing & token signing with WebCrypto. Zero dependencies. Edge-native. Built for Cloudflare, Deno, Bun, and Vercel.
157 lines (103 loc) โข 3.1 kB
Markdown
# ๐ฅ pw-punch
> ๐ **Ultra-lightweight** password hashing & JWT-style token signing with pure **WebCrypto**.
> Built for **Edge**, **Serverless**, and modern runtimes like **Cloudflare**, **Deno**, **Vercel**, **Bun** โ _no Node.js required_.
> **Zero dependencies. Zero bloat. Just crypto.**
## โก Why pw-punch?
- โ
**0 dependencies** โ no install bloat
- โ
**0 Node.js required** โ pure WebCrypto API
- โ
**0 config** โ import and go
- โ
**~4KB gzipped** โ tiny footprint
- โ
**Crypto only** โ no extra fluff
## ๐ Features
- ๐ Password hashing with PBKDF2 + random salt
- โ๏ธ HMAC-SHA256 / SHA512 token signing (JWT-style)
- ๐ต๏ธ Token verification with standard claim checks (`exp`, `nbf`, `iat`, `iss`, `aud`, `sub`)
- ๐ Supports key rotation (`kid` support)
- ๐งช Constant-time comparison utilities
- ๐งฉ WebCrypto only โ works on:
- โ
Cloudflare Workers
- โ
Deno Deploy
- โ
Bun
- โ
Modern Browsers
- โ
Node 18+ (WebCrypto)
- ๐ก Fully tree-shakable
## ๐ฆ Install
```bash
npm install pw-punch
```
## ๐ง API Usage
### ๐ Hash a password
```ts
import { hashPassword } from 'pw-punch'
const hashed = await hashPassword('hunter2')
// "base64salt:base64hash"
```
### โ
Verify a password
```ts
import { verifyPassword } from 'pw-punch'
const isValid = await verifyPassword('hunter2', hashed)
// true or false
```
### โ๏ธ Sign a token
```ts
import { signToken } from 'pw-punch'
const token = await signToken({ sub: 'user' }, 'secret')
```
### ๐ต๏ธ Verify a token
```ts
import { verifyToken } from 'pw-punch'
const payload = await verifyToken(token, 'secret')
// returns payload or null
```
### ๐ Decode token (without verifying)
```ts
import { decodeToken } from 'pw-punch'
const { header, payload, signature } = decodeToken(token)
```
## ๐ Full Example
```ts
const token = await signToken(
{ sub: 'user' },
'my-secret',
256,
'key-1'
)
const payload = await verifyToken(token, { 'key-1': 'my-secret' })
```
## ๐งช Tests & Demo
- โ
All core features tested using [`bun test`](https://bun.sh/docs/test)
- โ
Additional **interactive demo** available:
```bash
npm run demo
```
Select and run hashing/token functions in CLI with colored output.
Great for dev previewing & inspection.
## ๐ฆ Built With
- ๐ 100% WebCrypto (FIPS-compliant)
- โก Bun for test/dev (optional)
- ๐ TypeScript + `tsc` build
- ๐ฌ No dependencies at all
## โ ๏ธ Disclaimer
This is **not a full JWT spec implementation**.
- Only `HMAC` is supported (no RSA/EC)
- You must check claims like `aud`, `iss` yourself, or provide a `customValidate()` hook
- No support for JWE/JWS standards
## ๐ฎ Roadmap
- [x] Interactive CLI demo
- [x] JWT claim validation hook
- [x] Shorter token support (manual control)
This is the way.
## ๐ License
MIT
<!-- keywords: jwt, token, hmac, pbkdf2, crypto, webcrypto, edge, serverless, cloudflare, bun, vercel, deno, browser, password, hashing, lightweight, 0dep, zero-dependency -->