UNPKG

novagate

Version:

Novagate is a flexible and fast API key middleware system for Node.js apps. It provides authentication, usage limits, burst protection, suspension, logging, and analytics.

31 lines (21 loc) 687 B
import { createHash, timingSafeEqual } from 'node:crypto'; import { validateApiKey } from './validate.js'; // hash the api key then store in the db function hashApiKey(key) { return createHash("sha256").update(key).digest("hex"); } function compareHashedApiKey(rawKey, storedHash) { let isValidkey = validateApiKey(rawKey); if(!isValidkey){ return; } const hashed = hashApiKey(rawKey); const buffA = Buffer.from(hashed); const buffB = Buffer.from(storedHash); if (buffA.length !== buffB.length) return false; return timingSafeEqual(buffA, buffB); } export { hashApiKey, compareHashedApiKey }