PHP-cURL:如何使用cURL安全地将数据发送到另一台服务器

我正尝试通过cURL将一些数据从服务器(A)发送到服务器(B),然后根据情况将所述数据放入数据库或从中删除。 问题是我想保护它,并确保不是每个人都可以通过访问服务器(B)将他想要的任何东西放入数据库中。 因此,我对其他数据进行了哈希处理:

<?php
    $url = "https://serverB/test.php";
    $hash = hash('sha512','UPbztBfJEY7FjDjUZ7kd');//Don't mind the sha512 instead of bcrypt,both my servers aren't working with bcrypt.

    $fields = array(
        'ref' => 'toasty','name' => 'toasta'
        'hash'=> $hash
    );

    $fields_string = http_build_query($fields);

    $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_POST,count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
    curl_exec($ch);
?>

然后,我们在另一台服务器上对其进行验证:

<?php
    $hash=(array_key_exists('hash',$_POST))?$_POST['hash']:'';
    if($hash==hash('sha512','UPbztBfJEY7FjDjUZ7kd')){
        //Insert the data into the database
    }
?>

但是它真的安全吗?如果有人能够读完我发送的内容,即使$hash很好地进行了哈希处理,他也可以只输入哈希密码就可以发送他想要的任何内容,因为验证可以进行。

够了吗?我该如何做得更好?

请随时问我我想念的更多信息,谢谢!

jlgrt 回答:PHP-cURL:如何使用cURL安全地将数据发送到另一台服务器

您需要对数据进行哈希处理,以确保在传输过程中未对其进行更改,并且可以使用密钥来确保只有授权方才能生成有效的哈希。因此您的发送代码可能如下所示。

  $yourSecretKey = 'UPbztBfJEY7FjDjUZ7kd';
  $fields = array(
      'ref' => 'toasty','name' => 'toasta'
       );
  $hash = hash('sha512',$yourSecretKey . serialize($fields));

  $fields['hash'] = $hash;

在接收端,您需要从数据中提取散列,使用密钥对其他数据字段进行散列,并对照提取的散列检查生成的散列。

foreach ($_POST as $key => $value) {
    if ($key === 'hash') {     // Checksum value is separate from all other fields and shouldn't be included in the hash
        $checksum = $value;
    } else {
        $input[$key] = $value;
    }
}

$hash = hash('sha512',$yourSecretKey . serialize($input));
if ($hash === $checksum) {
    $valid = true;
} else {
    $valid = false;
}
本文链接:https://www.f2er.com/3160233.html

大家都在问