How to write this code in php? Telegram webapp bot documentation

47 Views Asked by At

I've got this instrunctions from Telegram documenation enter image description here

but how to write it in PHP?

I tried this one:

if (pack("H*",hash_hmac('sha256',$string, $secret_key)) == $info['hash']) {

but unsuccesfully.. any ideas how to apply this code in php?

2

There are 2 best solutions below

0
mae On

Does this accurately reflect your intent?

if(hach_hmac("sha256", $data_check_string, $secret_key) === $info['hash']){
  // data is from Telegram
}

If the third parameter of the hash_hmac function is not provided or is set to false, it will return a lowercase hexadecimal value. More information can be found at https://www.php.net/manual/ja/function.hash-hmac.php

0
Sdafs Fasafs On

I figured it out..

secret_key = HMAC_SHA256(<bot_token>, "WebAppData")

php equal is:

$secret_key = hash_hmac('sha256',$bot_token, 'WebAppData', true);

and

hex(HMAC_SHA256(data_check_string, secret_key))

it is just:

hash_hmac('sha256',$dataCheckString, $secret_key);