php - How can I create a shared-secret voucher code system between 2 independent servers? -
given workflow:
server a
- user authenticates.
- user purchases randomly generated unique voucher code using shared secret use application on on server b.
server b
- user authenticates.
- user inputs voucher code.
- server b validates code legitimate using shared secret
- server b grants access application.
i need way in php implement functions generatevouchercode
, validatevouchercode
shown below:
server a
$voucher = generatevouchercode("somesharedsecret");
server b
$isvalid = validatevouchercode($userinputtedcode, "somesharedsecret"); if($isvalid) { // allow access application }
validating legitimacy through shared secret hmacs for. can generate hmac in php through hash_hmac
. workflow be:
- server generates one-use code (in manner want) , calculates hmac. pair of code + hmac given user voucher code.
- user presents voucher server b.
- server b isolates one-use code voucher , independently calculates hmac using shared secret. if calculated hmac matches 1 in voucher voucher genuine.
example voucher generation:
$secret = '$uper$ecret$tring'; $code = 'a pet unicorn'; $voucher = $code.'/'.hash_hmac('sha512', $code, $secret); echo 'your voucher '.$voucher';
example voucher verification:
$secret = '$uper$ecret$tring'; list ($code, $hmac) = explode('/', $voucher); $verify_hmac = hash_hmac('sha512', $code, $secret); if ($hmac === $verify_hmac) { echo 'your voucher can redeemed '.$code'; } else { echo 'invalid voucher, sorry'; }
Comments
Post a Comment