Skip to content

Commit

Permalink
test decrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Feb 10, 2024
1 parent 2794d4e commit a98b7b1
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
15 changes: 0 additions & 15 deletions .phpstorm.meta.php

This file was deleted.

6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
"require": {
"omnipay/common": "^3.0",
"symfony/http-client": "^6.4",
"phpseclib/phpseclib": "^3.0"
"phpseclib/phpseclib": "^3.0",
"http-interop/http-factory-guzzle": "^1.2"
},
"require-dev": {
"roave/security-advisories": "dev-latest",
"omnipay/tests": "^3.0|^4.0",
"squizlabs/php_codesniffer": "^3",
"http-interop/http-factory-guzzle": "^1.2"
"squizlabs/php_codesniffer": "^3"
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 7 additions & 0 deletions src/Encryptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,11 @@ private static function getKeySize($key)
'Key of size '.$length.' not supported by this algorithm. Only keys of sizes 128, 192 or 256 supported'
);
}

public function decrypt($plainText)
{
return json_decode($this->cipher->decrypt(
base64_decode($plainText)
), true);
}
}
5 changes: 5 additions & 0 deletions src/Message/XWallet/PurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class PurchaseRequest extends AbstractRequest

/**
* 特店訂單編號
*
* @param string $value
*/
public function setFirmOrderNo($value)
Expand All @@ -29,6 +30,7 @@ public function getFirmOrderNo()

/**
* 支付方式 1:信用卡2、虛擬
*
* @param int $value
*/
public function setPayType($value)
Expand All @@ -46,6 +48,7 @@ public function getPayType()

/**
* 金額
*
* @param int $value
*/
public function setPrice($value)
Expand All @@ -55,6 +58,7 @@ public function setPrice($value)

/**
* @return string
*
* @throws InvalidRequestException
*/
public function getPrice()
Expand All @@ -64,6 +68,7 @@ public function getPrice()

/**
* 消費者手機
*
* @param string $value
*/
public function setMobile($value)
Expand Down
27 changes: 27 additions & 0 deletions tests/EncryptorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class EncryptorTest extends TestCase
{
private $key = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

private $iv = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

public function testEncrypt()
Expand All @@ -26,6 +27,20 @@ public function testEncrypt()
self::assertEquals($this->opensslEncrypt($data), $encryptor->encrypt($data));
}

public function testDecrypt()
{
$data = 'u4OlyM8T0QFQPQkL8XhzfzbRQgoPeJauC/hntXPi7Mt2koLqMbjzRbOCOWOwkIgDYTyGgtCrigF0yJ9vP0eKYPjwQUspyhVKdUK6+RVTBE4aBX998OOJ+zwxO0c8f5ZJ';

$encryptor = new Encryptor($this->key, $this->iv);

self::assertEquals([
'FirmOrderNo' => 'test202308301722001',
'o_PayNo' => '460199********8103',
'o_PriceReal' => 100,
], $encryptor->decrypt($data));
self::assertEquals($this->opensslDecrypt($data), $encryptor->decrypt($data));
}

private function opensslEncrypt($data)
{
$data = json_encode($data);
Expand All @@ -34,4 +49,16 @@ private function opensslEncrypt($data)

return base64_encode($hash);
}

/**
* @return mixed
*/
private function opensslDecrypt(string $data)
{
$HashIv = substr($this->iv, 0, 16);
$str = base64_decode($data);
$str = openssl_decrypt($str, 'aes-256-cbc', $this->key, OPENSSL_RAW_DATA, $HashIv);

return json_decode($str, true);
}
}

0 comments on commit a98b7b1

Please sign in to comment.