-
-
Save brpaz/5bab0c51c2a628eae380 to your computer and use it in GitHub Desktop.
#php #hmac #crypt #base64
This file contains hidden or 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 hidden or 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