Validation through a shared secret - these are HMACs . You can create HMAC in PHP via hash_hmac . Your workflow will be as follows:
- Server A generates a one-time code (in any way) and calculates its HMAC. The + HMAC code pair is provided to the user as a voucher code.
- The user submits a voucher to server B.
- Server B isolates the one-time code from the voucher and independently calculates its HMAC using a shared secret. If the calculated HMAC matches that of the voucher, then the voucher is genuine.
Example of receiving a voucher:
$secret = '$uper$ecret$tring'; $code = 'a pet unicorn'; $voucher = $code.'/'.hash_hmac('sha512', $code, $secret); echo 'Your voucher is '.$voucher';
Example of checking a voucher:
$secret = '$uper$ecret$tring'; list ($code, $hmac) = explode('/', $voucher); $verify_hmac = hash_hmac('sha512', $code, $secret); if ($hmac === $verify_hmac) { echo 'Your voucher can be redeemed for '.$code'; } else { echo 'Invalid voucher, sorry'; }
source share