stanford-crypto-patterns
暂无简介
- License 许可
- Apache-2.0
- 最近更新
- 2026-11-11
- Stars
- ★ 13
- Forks
- 1
03 SKILL.md 正文
/stanford-crypto-patterns
When to Use
- Environments where timing-attack resistance is critical (secret comparison)
- AES-CCM authenticated encryption (alternative to GCM with simpler nonce handling)
- SJCL PRNG for cryptographic random number generation
- Academic/compliance contexts requiring vetted, peer-reviewed crypto library
Do NOT use for
- New Node.js code — prefer built-in
cryptomodule (WebCrypto API) for standard cases - Performance-critical bulk encryption (pure JS SJCL is slower than native C bindings)
Constant-time comparison (timing-attack safe)
import sjcl from 'sjcl'
// WRONG: regular string comparison leaks timing info
// if (secretA === secretB) ... ← fails early on first mismatch
// RIGHT: constant-time comparison via XOR of bitArrays
function constantTimeEqual(a: string, b: string): boolean {
if (a.length !== b.length) return false
const aWords = sjcl.codec.utf8String.toBits(a)
const bWords = sjcl.codec.utf8String.toBits(b)
let diff = 0
for (let i = 0; i < aWords.length; i++) {
diff |= aWords[i] ^ bWords[i]
}
return diff === 0
}
AES-CCM authenticated encryption
import sjcl from 'sjcl'
function encryptSJCL(plaintext: string, passphrase: string): string {
// sjcl.encrypt handles: PBKDF2 key derivation + AES-CCM + random IV + MAC
const encrypted = sjcl.encrypt(passphrase, plaintext, {
ks: 256, // AES key size: 128 | 192 | 256
ts: 128, // authentication tag size: 64 | 96 | 128 bits
iter: 100_000, // PBKDF2 iterations
mode: 'ccm', // ccm | gcm | ocb2
})
return encrypted // JSON string with iv, salt, ct, adata, mac
}
function decryptSJCL(encrypted: string, passphrase: string): string {
try {
return sjcl.decrypt(passphrase, encrypted)
} catch (e) {
throw new Error('[sjcl] decryption failed — wrong passphrase or tampered data')
}
}
Cryptographic random bytes
// SJCL PRNG (seeded from system entropy via window.crypto or a custom entropy function)
function secureRandom(bytes: number): string {
const wordCount = Math.ceil(bytes / 4)
const words = sjcl.random.randomWords(wordCount, 10) // paranoia level 10
return sjcl.codec.hex.fromBits(words).slice(0, bytes * 2)
}
// Session nonce
const nonce = secureRandom(16) // 32-char hex = 128-bit nonce
PBKDF2 key derivation
function deriveKeySJCL(passphrase: string, saltHex: string, iterations = 100_000): string {
const salt = sjcl.codec.hex.toBits(saltHex)
const key = sjcl.misc.pbkdf2(passphrase, salt, iterations, 256)
return sjcl.codec.hex.fromBits(key)
}
SHA-256 / HMAC
const hash = sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash('data'))
const key = sjcl.codec.utf8String.toBits(process.env.HMAC_KEY!)
const mac = sjcl.codec.hex.fromBits(new sjcl.misc.hmac(key).encrypt('message'))
Anti-Fake-Pass Checklist
❌ sjcl.random.randomWords without seeding → throws on missing entropy in Node.js (use node-crypto seed)
❌ sjcl.encrypt paranoia < 6 → low entropy tolerance warning; use 8-10 for production
❌ Comparing MAC tags with === → timing attack; use constantTimeEqual above
❌ AES-CCM nonce reused → catastrophic, breaks authentication (SJCL uses random IV by default)
❌ sjcl in browser vs Node.js → window.crypto auto-seed in browser, must manually add entropy in Node
❌ sjcl output is JSON object — stringify before storing, parse before decrypting
04 同仓库更多 Skills
openai--sentry--sentry
Use when the user asks to inspect Sentry issues or events, summarize recent production errors, or pull basic Sentry health data via the Sentry API; perform read-only queries with the bundled script and require `SENTRY_AUTH_TOKEN`.
ito-basket-compare
Compare Itô prediction-market baskets against a user's knowledge base, portfolio notes, financial context, watchlist, or research thesis. Use for read-only basket comparison and gap analysis without investment advice or live trading.
implementing-network-traffic-baselining
Build network traffic baselines from NetFlow/IPFIX data using Python pandas for statistical analysis, z-score anomaly detection, and hourly/daily traffic pattern profiling
detecting-evasion-techniques-in-endpoint-logs
Detects defense evasion techniques used by adversaries in endpoint logs including log tampering, timestomping, process injection, and security tool disabling. Use when investigating suspicious endpoint behavior, building detection rules for evasion tactics, or conducting threat hunting for stealthy adversary activity. Activates for requests involving evasion detection, defense evasion analysis, log tampering detection, or MITRE ATT&CK TA0005.
implementing-microsegmentation-with-guardicore
Implementing microsegmentation using Akamai Guardicore Segmentation to map application dependencies, create granular network policies, visualize east-west traffic flows, and enforce least-privilege communication between workloads across data centers and cloud.
ramdisk-tmpfs-patterns
暂无简介