Files
wehub-resource-sync c48612c494
CI / E2E Tests (push) Has been cancelled
CI / Lint, Typecheck & Unit Tests (push) Has been cancelled
Docs Build / Build docs site (push) Has been cancelled
Publish @openmaic packages / Build, validate & publish (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:03:23 +08:00

26 lines
944 B
TypeScript

import { createHmac, timingSafeEqual } from 'crypto';
/** Create an HMAC-signed token: `timestamp.signature` */
export function createAccessToken(accessCode: string): string {
const timestamp = Date.now().toString();
const signature = createHmac('sha256', accessCode).update(timestamp).digest('hex');
return `${timestamp}.${signature}`;
}
/** Verify an HMAC-signed token against the access code */
export function verifyAccessToken(token: string, accessCode: string): boolean {
const dotIndex = token.indexOf('.');
if (dotIndex === -1) return false;
const timestamp = token.substring(0, dotIndex);
const signature = token.substring(dotIndex + 1);
const expected = createHmac('sha256', accessCode).update(timestamp).digest('hex');
const sigBuf = Buffer.from(signature, 'hex');
const expBuf = Buffer.from(expected, 'hex');
if (sigBuf.length !== expBuf.length) return false;
return timingSafeEqual(sigBuf, expBuf);
}