6.1. Using signatures
Signatures protect the data exchanged between your site and a survey.
Control signature — is MD5 hash from line composed of listed transferred parameter. «Salt» — secret key set in poll settings at «API» tab and added before hashing for support of confidentiality. Since the key is known only to you and Simpoll — the violator won’t be able to fake the signature, even if he knows its creation algorithm.
For the use of control signatures, the corresponding option in poll parameters on «API» tab should be enabled.
6.2. Signing parameters sent to a survey
The section 5 shows how to pass parameters to a survey in a hidden field. A visitor can see and change this field. A signature prevents that.
The signature is the MD5 hash of the secret key and the values of all parameters, separated by colons:
MD5 ( key:parameter1:parameter2 )
List the values in the same order as in the query string.
Then add the signature to the parameters as a regular parameter named sig.
Example of passing parameters with a signature:
<?php
// Key of the poll control signature
// is set in poll parameters on "API" tab
$survey_secret_key = 'q1w2e3r4';
// Array of parameters to be sent to
// our site once the poll is passed.
// Can be formed dynamically, if necessary.
$params_arr = array(
'user_id' => 123,
'user_login' => 'anonymous'
);
// Create control signature for parameter array
$signature = md5( $survey_secret_key . ':' . implode(':', $params_arr) );
// Add control signature to parameter list
$params_arr['sig'] = $signature;
// Create URL-coded inquiry line
$params_str = http_build_query($params_arr);
?>
<!—Create field with inquiry line -->
<input type="hidden" id="simpoll_params" value="<?php echo $params_str; ?>" />
<!—Connect the poll -->
<script type="text/javascript" src="https://simpoll.com/embed/framejs/aa7ca07e"></script>
If a user changes any parameter, the signature no longer matches: Simpoll will not show the survey and will report an error.
6.3. Signature of the results
Results that Simpoll sends to your site are signed differently.
Because the response contains arrays, the signature is made from the secret key and the serialized response:
MD5 ( key:SERIALIZE( result array ) )
To check it, build the signature from the results you received and compare it with the one Simpoll sent. If they don't match, reject the results.
For example, if your result.php script receives the results by POST:
Example of checking the signature:
<?php
// Key of poll’s control signature
// is set in parameters on "API" tab
$survey_secret_key = 'q1w2e3r4';
// Receive parameters sent from Simpoll through POST
$results = $_POST;
// Save signature received from Simpoll
// and remove it from the list of results
$server_sig = $results['sig'];
unset($results['sig']);
// Create own control signature
// on the basis of key and results received from Simpoll
$signature = md5( $survey_secret_key . ':' . serialize($results) );
// Compare signatures
if ($server_sig == $signature) {
// ... signatures match, response is reliable ...
} else {
// ... signatures mismatch, exit ...
exit;
}
?>
If $_POST (or $_GET) can contain parameters unrelated to Simpoll (for example, ones your CMS adds to every request), remove them before building the signature.
All parameters sent by Simpoll start with sp_, except the ones you passed yourself.