-
-
Save vzool/efd71ce730f54d590d34fd1701b7da12 to your computer and use it in GitHub Desktop.
Adding content verification using hmac in php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function get_private_key_for_public_key($public_key) { | |
// extract private key from database or cache store | |
return 'private_key_user_id_9999'; | |
} | |
// Data submitted | |
$data = $_GET['data']; | |
$data = json_decode(stripslashes($data), TRUE); | |
// User hit the end point API with $data, $signature and $public_key | |
$message = $data['data']; | |
$received_signature = $data['sig']; | |
$private_key = get_private_key_for_public_key($data['pubKey']); | |
$computed_signature = base64_encode(hash_hmac('sha1', $message, $private_key, TRUE)); | |
if($computed_signature == $received_signature) { | |
echo "Content Signature Verified"; | |
}else { | |
echo "Invalid Content Verification Signature"; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
const $SUBMIT_URL = "http://mysite.com/hmac-receiver.php?data="; | |
//User public & private keys | |
$private_key = ""; | |
$public_key = ""; | |
//Data to be submitted | |
$data = ""; | |
//Generate content verification signature | |
$sig = base64_encode(hash_hmac('sha1', $data, $private_key, TRUE)); | |
//Prepare json data to be submitted | |
$json_data = json_encode(array('data'=>$data, 'sig'=>$sig, 'pubKey'=>$public_key)); | |
//Submit to api | |
submit_to_api($SUBMIT_URL.urlencode($json_data)); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment