Skip to content

Instantly share code, notes, and snippets.

@h3nr1ke
Created December 2, 2017 17:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h3nr1ke/b3d8a2f1ac5cfdde572574a7638be1ee to your computer and use it in GitHub Desktop.
Save h3nr1ke/b3d8a2f1ac5cfdde572574a7638be1ee to your computer and use it in GitHub Desktop.
<?php
/**
* Arquivo de exemplo para descriptografar os dados do visa checkout apos validacao do usuario
* este codigo esta na documentacao disponivel em
* https://developer.visa.com/capabilities/visa_checkout/docs
*
*/
/**
* Exeuta o primeiro nivel de descriptografia
* @param [String] $key key passada pela visa
* @param [String] $wrappedKey key que retornou na consulta
* @param [String] $payload base64 do payload
* @return [Object] o objeto descriptografado
*/
function decryptPayload($key, $wrappedKey, $payload) {
$unwrappedKey = decrypt($key, $wrappedKey);
return decrypt($unwrappedKey, $payload);
}
/**
* [decrypt description]
* @param [String] $key key passada pela visa
* @param [String] $data base64 do payload
* @return [String] key retornada para pegar os dados do cartao
*/
function decrypt($key, $data) {
$decodedData = base64_decode($data);
// TODO: Check that data is at least bigger than HMAC + IV length
$hmac = substr($decodedData, 0, 32);
$iv = substr($decodedData, 32, 16);
$data = substr($decodedData, 48);
if ($hmac != hmac($key, $iv . $data)) {
// TODO: Handle HMAC validation failure
return false;
}
return openssl_decrypt($data, 'aes-256-cbc', hashKey($key), OPENSSL_RAW_DATA, $iv);
}
function hashKey($data) {
$hasher = hash_init('sha256');
hash_update($hasher, $data);
return hash_final($hasher, true);
}
function hmac($key, $data) {
return hash_hmac('sha256', $data, $key, true);
}
/**
* Processo os dados passados para o arquivo
*/
$_key = $_REQUEST['enckey'];
$_data = $_REQUEST['data'];
$_skey = $_REQUEST['skey'];
//define os headers do arquvio para retornarmos um json
header('Pragma: no-cache');
header('Cache-Control: private, no-cache');
header('Content-Disposition: inline; filename="key.json"');
header('X-Content-Type-Options: nosniff');
header('Access-Control-Allow-Origin: *');
header('Vary: Accept');
header('Content-type: application/json');
//se todos os dados estao aqui, continua
if( $_key != "" && $_data != "" && $_skey != "" ){
$ret = decryptPayload($_skey, $_key, $_data);
if( $ret ){
echo $ret;
}
else{
echo json_encode(array("erro"=>true, "data" => null, "msg" => "Erro ao realizar a conversao dos dados"));
}
}
else{
echo json_encode(array("erro"=>true, "data" => null, "msg" => "faltam dados para processar"));
}
exit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment