php - How can I create a shared-secret voucher code system between 2 independent servers? -


given workflow:

server a

  1. user authenticates.
  2. user purchases randomly generated unique voucher code using shared secret use application on on server b.

server b

  1. user authenticates.
  2. user inputs voucher code.
  3. server b validates code legitimate using shared secret
  4. 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:

  1. server generates one-use code (in manner want) , calculates hmac. pair of code + hmac given user voucher code.
  2. user presents voucher server b.
  3. 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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -