UNPKG

nodejs-cryptomus

Version:

A comprehensive Node.js client for the Cryptomus API

32 lines (31 loc) 982 B
import * as crypto from 'crypto'; // Add explicit type import for Node.js Buffer import { Buffer } from 'buffer'; /** * Generate signature for API requests * * @param payload - Request payload * @param apiKey - API key * @returns Generated signature */ export function generateSignature(payload, apiKey) { const payloadString = JSON.stringify(payload); const sign = crypto.createHash('md5') .update(Buffer.from(payloadString).toString('base64') + apiKey) .digest('hex'); return sign; } /** * Verify webhook signature * * @param payload - Webhook payload * @param signature - Signature from webhook headers * @param apiKey - API key * @returns Whether the signature is valid */ export function verifyWebhookSignature(payload, signature, apiKey) { const calculatedSignature = crypto.createHash('md5') .update(Buffer.from(payload).toString('base64') + apiKey) .digest('hex'); return calculatedSignature === signature; }