D
D
Deman12018-07-01 03:46:09
PHP
Deman1, 2018-07-01 03:46:09

How to verify the digital signature of the VKontakte application?

Please help to solve the problem, I know that there are already such topics, but I did not find the answer to my question in them.
I need to generate a digital signature, and VK gives an example of a code that does not work Example

$sign = ""; 

foreach ($request->getParams() as $key => $param) { 

    if ($key == 'hash' || $key == 'sign') continue; 

    $sign .=$param; 

} 

$secret = 'SECRET_KEY'; 

$sig = $secret ? hash_hmac('sha256', $sign, $secret) : "";

But unfortunately php doesn't know what getParams() function is and $request null
Error: Fatal error: Uncaught Error: Call to a member function getParams() on null in index.php:4 Stack trace: #0 {main} thrown in index.php on line 4
Having rummaged through the internet, I didn’t find anything normal except for this code
$url = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$parsed_url = parse_url($url, PHP_URL_QUERY);   
parse_str($parsed_url,$request);

$sign = ""; 
foreach ($request as $key => $param) { 

    if ($key == 'hash' || $key == 'sign') continue; 

    $sign .=$param; 

} 
$secret = 'Защищенный_Ключ'; 
$sig = $secret ? hash_hmac('sha256', $sign, $secret) : ""; 
$result = ($sig === $_GET['sign']) ? "Сходятся" : "Не сходятся";

The person who made it did it, but it turned out to be not working again, or he didn’t throw it all off, since $_GET['sign'] has null and is not transmitted to VK, so I decided to check with $sign but there is a code that contains 326 characters, and the key that is generated in $sig is 64 characters long.
Please help me solve this, please give me a working code that will generate the correct code for me to verify the digital signature.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2018-07-01
@sergiks

replace

foreach ($request->getParams() as $key => $param) {

On the example of my code that I have now checked, the signatures match:
$sign = "";
  
  foreach($_REQUEST AS $key => $param) {
  
      if ($key == 'hash' || $key == 'sign') continue;
  
      $sign .=$param;
  
  }
  
  $secret = '3iiyZNDFaXgR6yZMxK'; // Настройки приложения – Защищённый ключ
  
  $sig = $secret ? hash_hmac('sha256', $sign, $secret) : ""; 
  
  if( $sig === $_REQUEST['sign']) {
    echo "Подписи совпали.";
  } else {
    echo "Разные подписи, не хорошо.";
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question