79177084

Date: 2024-11-11 09:34:22
Score: 1.5
Natty:
Report link

i cannot add a comment, @zdm ‘s answer is not work for me, i changed "secretKey", now its working.

function auth(apiToken: string, telegramInitData: typeof mockData) {
  const initData = new URLSearchParams(telegramInitData);
  initData.sort();
  const hash = initData.get("hash");
  initData.delete("hash");
  const dataToCheck = [...initData.entries()].map(([key, value]) => key + "=" + value).join("\n");
  // change to createHash
  const secretKey = crypto.createHash("sha256").update(apiToken).digest();
  const _hash = crypto.createHmac("sha256", secretKey).update(dataToCheck).digest("hex");
  return hash === _hash;
}
Reasons:
  • Blacklisted phrase (0.5): i cannot
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @zdm
  • Low reputation (1):
Posted by: Noah