import * as crypto from 'crypto';

/**
 * [BIZ_INT_12] 签名校验工具类
 * 用于 Webhook 推送的 HMAC-SHA256 签名生成
 */
export class SignatureUtils {
  /**
   * 生成 HMAC-SHA256 签名
   */
  static generateSignature(payload: string, secret: string): string {
    return crypto
      .createHmac('sha256', secret)
      .update(payload)
      .digest('hex');
  }

  /**
   * 验证签名
   */
  static verifySignature(payload: string, signature: string, secret: string): boolean {
    const expectedSignature = this.generateSignature(payload, secret);
    return crypto.timingSafeEqual(
      Buffer.from(signature),
      Buffer.from(expectedSignature)
    );
  }
}
