Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php web服务中的校验和实现_Php_Json_Web Services_Checksum - Fatal编程技术网

php web服务中的校验和实现

php web服务中的校验和实现,php,json,web-services,checksum,Php,Json,Web Services,Checksum,我有php web服务来获取JSON格式的大量数据。目前我正在使用发送数据和接收数据的计数来比较成功案例。最近我听到一种叫做校验和的方法。在这种情况下如何实现校验和?您可以使用已知密钥对有效负载进行签名,创建的令牌将使用HTTP头传递 例如: <?php // key which will sign the data $key = hash('sha256', 'Unique user data or Some secret'); // your data $array = [

我有php web服务来获取JSON格式的大量数据。目前我正在使用发送数据和接收数据的计数来比较成功案例。最近我听到一种叫做校验和的方法。在这种情况下如何实现校验和?

您可以使用已知密钥对有效负载进行签名,创建的令牌将使用HTTP头传递

例如:

<?php
// key which will sign the data 
$key = hash('sha256', 'Unique user data or Some secret');

// your data
$array = [
    'foobar' => 'baz'    
];

// encode the payload
$json = json_encode($array);

// sign it with key
$token = hash_hmac('sha256', $json, $key);

// set response header
header('X-Checksum: '.$token);

echo $json;

在线查看:

目前我正在使用发送数据和接收数据的计数来比较成功案例。这意味着什么?从服务器发送的数据计数将与应用程序中接收的json数据计数进行比较。但两者都需要数据库查询。服务器到服务器?2种方案。一个是从一台服务器到另一台服务器获取数据,另一个是从服务器到移动应用程序使用已知的密钥来分配有效负载,就像工作原理一样。谢谢@Lawrence Cherone
// faked: this would be populated by the request/response
$_POST['json'] = $json; 
$_SERVER['X-Checksum'] = $token; 

// verify the data matches token by signing the data with the key
$check = hash_hmac('sha256', $_POST['json'], $key);
if (hash_equals($token, $check)) {
    echo 'Verified';
} else {
    echo 'Tampered';
}

// example tampered data
$_POST['json'] = 'tampered'.$json; 

$check = hash_hmac('sha256', $_POST['json'], $key);
if (hash_equals($token, $check)) {
    echo 'Verified';
} else {
    echo 'Tampered';
}