foodshare-crypto
Cryptographic utilities for webhook verification and HMAC signature generation.
Features
- HMAC-SHA256/SHA1 - Generate signatures for webhook verification
- Constant-Time Comparison - Secure signature verification resistant to timing attacks
- Provider Support - Works with Stripe, GitHub, Meta/Facebook webhooks
- WASM Support - Compile to WebAssembly for browser/Deno usage
Installation
Add to your Cargo.toml:
[]
= "1.3"
Feature Flags
| Feature | Default | Description |
|---|---|---|
wasm |
No | Enable WebAssembly bindings |
Usage
Generate HMAC Signature
use ;
// HMAC-SHA256 (Stripe, Meta)
let signature = hmac_sha256;
// HMAC-SHA1 (GitHub)
let signature = hmac_sha1;
Verify Webhook Signature
use ;
Constant-Time Comparison
Always use constant-time comparison for security-sensitive operations:
use constant_time_compare;
// Safe: takes the same time regardless of where strings differ
let is_valid = constant_time_compare;
Provider Examples
Stripe
use ;
GitHub
use ;
Meta/Facebook
use ;
Security Considerations
- Always use constant-time comparison - Never use
==for signature comparison - Keep secrets secure - Never log or expose webhook secrets
- Validate timestamps - Check webhook timestamp to prevent replay attacks
- Use HTTPS - Always receive webhooks over TLS
WASM Usage
For browser/Deno usage, see @foodshare/crypto-wasm.
import init, { hmac_sha256_hex, verify_webhook_sha256 } from '@foodshare/crypto-wasm';
await init();
const signature = hmac_sha256_hex('secret', 'payload');
const isValid = verify_webhook_sha256('secret', 'payload', signature);
License
MIT License - see LICENSE for details.