Telegram WebApp hash verification mismatch despite correct data and bot token in Node.js

Issue:

I’m trying to verify the integrity of the hash provided by the Telegram WebApp authentication flow using Node.js, but the calculated hash never matches the provided hash. I’m using the correct bot token, and the parameters (userId, username, authDate) seem to be correct and properly formatted. Despite this, the hash mismatch persists.


Telegram Parameters:

Here are the values I’m receiving from Telegram’s WebApp (logged from my frontend):

  • User ID: 1234567890
  • Username: random_user123
  • Auth Date: 1727134170
  • Provided Hash: 55f0a395c4851512b31a797e33494801f4ff3f31ad99da3d92f13dfcccf678f0

Telegram Bot Token:

I’m using a bot token generated in the testnet environment, defined in

TELEGRAM_BOT_TOKEN

My Approach:

Following Telegram’s official documentation, I’m constructing the data_check_string and calculating the HMAC-SHA256 hash as follows:


Node.js Code:

const crypto = require('crypto');

function verifyTelegramHash({ userId, username, authDate, hash }) {
  if (!userId || !username || !authDate || !hash) {
    throw new Error('Missing required parameters');
  }

  // Log parameters for debugging
  console.log('Verifying Telegram hash with:', { userId, username, authDate, hash });

  // Create the secret key using SHA-256 with the Telegram bot token
  const secretKey = crypto.createHash('sha256')
    .update('process.env.TELEGRAM_BOT_TOKEN', 'utf8')
    .digest();

  // Construct the dataCheckString in alphabetical order of the keys
  const dataCheckString = `auth_date=${authDate}&id=${userId}&username=${username}`;

  console.log('Data Check String:', dataCheckString);

  // Calculate the hash using HMAC-SHA256
  const calculatedHash = crypto
    .createHmac('sha256', secretKey)
    .update(dataCheckString)
    .digest('hex');

  console.log('Calculated Hash:', calculatedHash);
  console.log('Provided Hash:', hash);

  // Return whether the hashes match
  return calculatedHash === hash;
}

// Example usage:
const isValid = verifyTelegramHash({
  userId: '1234567890',
  username: 'random_user123',
  authDate: '1727134170',
  hash: '55f0a395c4851512b31a797e33494801f4ff3f31ad99da3d92f13dfcccf678f0'
});

console.log('Is the hash valid?', isValid);

Output:

Here’s the output I’m seeing:

Verifying Telegram hash with: {
  userId: '1234567890',
  username: 'random_user123',
  authDate: '1727134170',
  hash: '55f0a395c4851512b31a797e33494801f4ff3f31ad99da3d92f13dfcccf678f0'
}
Data Check String: auth_date=1727134170&id=1234567890&username=random_user123
Calculated Hash: a97dc385905f5e09258ec19276751ab7fff55bc6bfe5e48a74151a502ac73b09
Provided Hash: 55f0a395c4851512b31a797e33494801f4ff3f31ad99da3d92f13dfcccf678f0
Is the hash valid? false

What I’ve Tried:

  • Environment variables: I’ve hardcoded the bot token in the code to eliminate issues with environment variables.
  • String formatting: I’ve ensured that the dataCheckString is correctly ordered and formatted (alphabetically by keys and no extra spaces).
  • Bot token: Double-checked that the bot token is correct (it’s the same one used in my Telegram bot, currently in test mode. I also tried the main one).
  • Checked Telegram docs: I followed Telegram’s documentation to calculate the hash using HMAC-SHA256 and verify the signature.

Expected Behavior:

The calculated hash should match the hash provided by Telegram, but this is not happening.


Question:

What could be causing the mismatch between the calculated and provided hash? Is there something I’m missing in the way Telegram WebApp hashes should be calculated?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Telegram WebApp hash verification mismatch despite correct data and bot token in Node.js

Issue:

I’m trying to verify the integrity of the hash provided by the Telegram WebApp authentication flow using Node.js, but the calculated hash never matches the provided hash. I’m using the correct bot token, and the parameters (userId, username, authDate) seem to be correct and properly formatted. Despite this, the hash mismatch persists.


Telegram Parameters:

Here are the values I’m receiving from Telegram’s WebApp (logged from my frontend):

  • User ID: 1234567890
  • Username: random_user123
  • Auth Date: 1727134170
  • Provided Hash: 55f0a395c4851512b31a797e33494801f4ff3f31ad99da3d92f13dfcccf678f0

Telegram Bot Token:

I’m using a bot token generated in the testnet environment, defined in

TELEGRAM_BOT_TOKEN

My Approach:

Following Telegram’s official documentation, I’m constructing the data_check_string and calculating the HMAC-SHA256 hash as follows:


Node.js Code:

const crypto = require('crypto');

function verifyTelegramHash({ userId, username, authDate, hash }) {
  if (!userId || !username || !authDate || !hash) {
    throw new Error('Missing required parameters');
  }

  // Log parameters for debugging
  console.log('Verifying Telegram hash with:', { userId, username, authDate, hash });

  // Create the secret key using SHA-256 with the Telegram bot token
  const secretKey = crypto.createHash('sha256')
    .update('process.env.TELEGRAM_BOT_TOKEN', 'utf8')
    .digest();

  // Construct the dataCheckString in alphabetical order of the keys
  const dataCheckString = `auth_date=${authDate}&id=${userId}&username=${username}`;

  console.log('Data Check String:', dataCheckString);

  // Calculate the hash using HMAC-SHA256
  const calculatedHash = crypto
    .createHmac('sha256', secretKey)
    .update(dataCheckString)
    .digest('hex');

  console.log('Calculated Hash:', calculatedHash);
  console.log('Provided Hash:', hash);

  // Return whether the hashes match
  return calculatedHash === hash;
}

// Example usage:
const isValid = verifyTelegramHash({
  userId: '1234567890',
  username: 'random_user123',
  authDate: '1727134170',
  hash: '55f0a395c4851512b31a797e33494801f4ff3f31ad99da3d92f13dfcccf678f0'
});

console.log('Is the hash valid?', isValid);

Output:

Here’s the output I’m seeing:

Verifying Telegram hash with: {
  userId: '1234567890',
  username: 'random_user123',
  authDate: '1727134170',
  hash: '55f0a395c4851512b31a797e33494801f4ff3f31ad99da3d92f13dfcccf678f0'
}
Data Check String: auth_date=1727134170&id=1234567890&username=random_user123
Calculated Hash: a97dc385905f5e09258ec19276751ab7fff55bc6bfe5e48a74151a502ac73b09
Provided Hash: 55f0a395c4851512b31a797e33494801f4ff3f31ad99da3d92f13dfcccf678f0
Is the hash valid? false

What I’ve Tried:

  • Environment variables: I’ve hardcoded the bot token in the code to eliminate issues with environment variables.
  • String formatting: I’ve ensured that the dataCheckString is correctly ordered and formatted (alphabetically by keys and no extra spaces).
  • Bot token: Double-checked that the bot token is correct (it’s the same one used in my Telegram bot, currently in test mode. I also tried the main one).
  • Checked Telegram docs: I followed Telegram’s documentation to calculate the hash using HMAC-SHA256 and verify the signature.

Expected Behavior:

The calculated hash should match the hash provided by Telegram, but this is not happening.


Question:

What could be causing the mismatch between the calculated and provided hash? Is there something I’m missing in the way Telegram WebApp hashes should be calculated?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật