Files
miles c250653961 Add comprehensive test suite for application functionality
- Implement tests for HTTP API endpoints, ensuring correct responses and security headers.
- Create configuration tests to validate loading of defaults and environment overrides.
- Add tests for FEDL service to verify data handling and snapshot functionality.
- Introduce GitHub service tests to check API data normalization and caching behavior.
- Develop monitor service tests for network checks and uptime calculations.
- Implement persistence tests to ensure database migrations are idempotent and data persists across restarts.
- Add vault service tests for session management and brute-force protection mechanisms.
- Include helper functions for test context creation and JSON response handling.
2026-11-08 16:24:06 -06:00

49 lines
1.4 KiB
JavaScript

import crypto from 'node:crypto';
export function sha256Hex(value) {
return crypto.createHash('sha256').update(value).digest('hex');
}
export function hmacHex(secret, value) {
return crypto.createHmac('sha256', secret).update(value).digest('hex');
}
export function randomHex(bytes = 32) {
return crypto.randomBytes(bytes).toString('hex');
}
export function safeEqualHex(a, b) {
if (typeof a !== 'string' || typeof b !== 'string') return false;
if (a.length !== b.length || a.length % 2 !== 0) return false;
try {
return crypto.timingSafeEqual(Buffer.from(a, 'hex'), Buffer.from(b, 'hex'));
} catch {
return false;
}
}
export function signValue(secret, value) {
return hmacHex(secret, value);
}
export function verifySignature(secret, value, signature) {
return safeEqualHex(signValue(secret, value), signature);
}
export function dayKey(date = new Date()) {
return date.toISOString().slice(0, 10);
}
/**
* Daily rotating key. Hashing an IP with this key gives a stable
* anonymous identifier for the day, but cannot be reversed.
*/
export function dailyKey(secret, date = new Date(), scope = 'analytics') {
return hmacHex(secret, `${scope}:${dayKey(date)}`);
}
export function hashIp(secret, ip, { date = new Date(), scope = 'analytics' } = {}) {
const key = dailyKey(secret, date, scope);
return hmacHex(key, String(ip || 'unknown')).slice(0, 32);
}