This repository has been archived by the owner on Jul 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from 1415003719/new-api-key
add as-api-key and mark as complete api
- Loading branch information
Showing
6 changed files
with
289 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace AfterShip; | ||
|
||
use phpseclib3\Crypt\RSA; | ||
|
||
/** | ||
* Class Encryption | ||
* @package AfterShip | ||
*/ | ||
class Encryption | ||
{ | ||
const ENCRYPTION_RSA = 'RSA'; | ||
|
||
const ENCRYPTION_AES = 'AES'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $apiSecret = ''; | ||
/** | ||
* @var string | ||
*/ | ||
private $encryptionMethod = ''; | ||
/** | ||
* @var string | ||
*/ | ||
private $encryptionPassword = ''; | ||
/** | ||
* Request constructor. | ||
* | ||
* @param $apiSecret the private key | ||
* @param $encryptionMethod 'RSA' or 'AES' | ||
* @param $encryptionPassword the private key password | ||
*/ | ||
function __construct($apiSecret, $encryptionMethod, $encryptionPassword) | ||
{ | ||
$this->apiSecret = $apiSecret; | ||
$this->encryptionMethod = $encryptionMethod; | ||
$this->encryptionPassword = $encryptionPassword; | ||
} | ||
|
||
/** | ||
* Calculate the digest of data with RSA_SIGN_PSS_2048_SHA256 algorithm. | ||
* https://www.aftership.com/docs/tracking/quickstart/authentication#3-rsa | ||
* @param $data The data to be encrypted. | ||
* @return string the encrypted result with base64 encode | ||
* @throws AfterShipException | ||
*/ | ||
function rsaPSSSha256Encrypt($data) { | ||
$key = RSA::loadPrivateKey($this->apiSecret, $this->encryptionPassword) | ||
-> withHash("sha256") | ||
-> withPadding(RSA::SIGNATURE_PSS) | ||
-> sign($data); | ||
return base64_encode($key); | ||
} | ||
|
||
/** | ||
* Calculate the hash of data with hmac-sha256 algorithm. | ||
* https://www.aftership.com/docs/tracking/quickstart/authentication#2-aes | ||
* @param $data The data to be encrypted. | ||
* @return string the encrypted result with base64 encode | ||
* @throws AfterShipException | ||
*/ | ||
function hmacSha256Encrypt($data) { | ||
return base64_encode(hash_hmac('sha256', $data, $this->apiSecret, true)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters