wgc
Version:
The official CLI tool to manage the GraphQL Federation Platform Cosmo
17 lines • 927 B
JavaScript
import crypto from 'node:crypto';
const algorithm = { name: 'HMAC', hash: 'SHA-256' };
const getCryptoKey = async (secret) => {
const secretBuf = typeof secret === 'string' ? new TextEncoder().encode(secret) : secret;
// @ts-ignore
return await crypto.subtle.importKey('raw', secretBuf, algorithm, false, ['sign', 'verify']);
};
export const safeCompare = (a, b) => {
return a.length === b.length && crypto.timingSafeEqual(new TextEncoder().encode(a), new TextEncoder().encode(b));
};
export const makeSignature = async (value, secret) => {
const key = await getCryptoKey(secret);
const signature = await crypto.subtle.sign(algorithm.name, key, new TextEncoder().encode(value));
// the returned base64 encoded signature will always be 44 characters long and end with one or two equal signs
return btoa(String.fromCodePoint(...new Uint8Array(signature)));
};
//# sourceMappingURL=signature.js.map