Skip to content

Commit

Permalink
bump to v0.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNorthMemory committed Nov 3, 2021
1 parent c262e1f commit 29218ef
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 63 deletions.
16 changes: 11 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
# 变更历史

## 0.3.0 - 2021-10-17
## [0.3.2](../../compare/v0.3.1...v0.3.2) - 2021-11-03

[变更细节](../../compare/v0.2.0...v0.3.0)
- 新增`phpstan/phpstan:^1.0`支持;
- 优化代码,消除函数内部不安全的`Unsafe call to private|protected method|property ... through static::`调用隐患;
- 优化`Makefile`生成大数逻辑,贴近真实序列号情况;

## [0.3.1](../../compare/v0.3.0...v0.3.1) - 2021-10-17

- 调整`composer.json`,去除`version`字典;

## [0.3.0](../../compare/v0.2.0...v0.3.0) - 2021-10-17

- 新增`Guzzle6`+`PHP7.1``Guzzle7`+`PHP8.1`支持;
- 调整`\EasyAlipay\Crypto\Rsa::from`方法,增加第二入参`$type(private|public)`,显示声明第一入参类型;
- 调整`\EasyAlipay\Crypto\Rsa::fromPkcs1`方法的第二入参为`$type(private|public)`,兼容布尔量声明方式;

## 0.2.0 - 2021-08-20

[变更细节](../../compare/v0.1.0...v0.2.0)
## [0.2.0](../../compare/v0.1.0...v0.2.0) - 2021-08-20

- 新增`\EasyAlipay\Helpers`类,以支持`公钥证书模式`使用;
- 新增`\EasyAlipay\Crypto\Rsa::pkcs1ToSpki`转换函数,以支持加载`PKCS#1`格式的`RSA公钥`
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"require-dev": {
"phpunit/phpunit": "^7.5 || ^8.5.16 || ^9.3.5",
"phpstan/phpstan": "^0.12.89"
"phpstan/phpstan": "^0.12.89 || ^1.0"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
parameters:
checkExplicitMixed: false
level: max
paths:
- src
Expand Down
6 changes: 3 additions & 3 deletions src/Crypto/AesCbc.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private static function detector(string $cipherkey, ?string $iv = null): array
/** @var string $key */
$key = base64_decode($cipherkey);

return [sprintf(static::MODE_CBC, strlen($key) * 8), $key, OPENSSL_RAW_DATA, $iv ?? str_repeat(static::CHR_NUL, static::BLOCK_SIZE)];
return [sprintf(self::MODE_CBC, strlen($key) * 8), $key, OPENSSL_RAW_DATA, $iv ?? str_repeat(self::CHR_NUL, static::BLOCK_SIZE)];
}

/**
Expand All @@ -56,7 +56,7 @@ private static function detector(string $cipherkey, ?string $iv = null): array
*/
public static function encrypt(string $plaintext, string $cipherkey, ?string $iv = null): string
{
$ciphertext = openssl_encrypt($plaintext, ...static::detector($cipherkey, $iv));
$ciphertext = openssl_encrypt($plaintext, ...self::detector($cipherkey, $iv));

if (false === $ciphertext) {
throw new UnexpectedValueException("Encrypting the {$plaintext} failed, please checking the {$cipherkey} and {$iv} whether or nor correct.");
Expand All @@ -77,7 +77,7 @@ public static function encrypt(string $plaintext, string $cipherkey, ?string $iv
*/
public static function decrypt(string $ciphertext, string $cipherkey, ?string $iv = null): string
{
$plaintext = openssl_decrypt(base64_decode($ciphertext), ...static::detector($cipherkey, $iv));
$plaintext = openssl_decrypt(base64_decode($ciphertext), ...self::detector($cipherkey, $iv));

if (false === $plaintext) {
throw new UnexpectedValueException("Decrypting the {$ciphertext} failed, please checking the {$cipherkey} and {$iv} whether or nor correct.");
Expand Down
20 changes: 10 additions & 10 deletions src/Crypto/Rsa.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public static function pkcs1ToSpki(string $thing): string
*/
public static function fromPkcs8(string $thing)
{
$pkey = openssl_pkey_get_private(static::parse(sprintf('private.pkcs8://%s', $thing)));
$pkey = openssl_pkey_get_private(self::parse(sprintf('private.pkcs8://%s', $thing)));

if (false === $pkey) {
throw new UnexpectedValueException(sprintf('Cannot load the PKCS#8 privateKey(%s).', $thing));
Expand All @@ -139,8 +139,8 @@ public static function fromPkcs8(string $thing)
public static function fromPkcs1(string $thing, $type = self::KEY_TYPE_PRIVATE)
{
$pkey = ($isPublic = is_bool($type) ? $type : $type === static::KEY_TYPE_PUBLIC)
? openssl_pkey_get_public(static::parse(sprintf('public.pkcs1://%s', $thing), $type))
: openssl_pkey_get_private(static::parse(sprintf('private.pkcs1://%s', $thing)));
? openssl_pkey_get_public(self::parse(sprintf('public.pkcs1://%s', $thing), $type))
: openssl_pkey_get_private(self::parse(sprintf('private.pkcs1://%s', $thing)));

if (false === $pkey) {
throw new UnexpectedValueException(sprintf('Cannot load the PKCS#1 %s(%s).', $isPublic ? 'publicKey' : 'privateKey', $thing));
Expand All @@ -158,7 +158,7 @@ public static function fromPkcs1(string $thing, $type = self::KEY_TYPE_PRIVATE)
*/
public static function fromSpki(string $thing)
{
$pkey = openssl_pkey_get_public(static::parse(sprintf('public.spki://%s', $thing), static::KEY_TYPE_PUBLIC));
$pkey = openssl_pkey_get_public(self::parse(sprintf('public.spki://%s', $thing), static::KEY_TYPE_PUBLIC));

if (false === $pkey) {
throw new UnexpectedValueException(sprintf('Cannot load the SPKI publicKey(%s).', $thing));
Expand Down Expand Up @@ -188,8 +188,8 @@ public static function fromSpki(string $thing)
public static function from($thing, $type = self::KEY_TYPE_PRIVATE)
{
$pkey = ($isPublic = is_bool($type) ? $type : $type === static::KEY_TYPE_PUBLIC)
? openssl_pkey_get_public(static::parse($thing, $type))
: openssl_pkey_get_private(static::parse($thing));
? openssl_pkey_get_public(self::parse($thing, $type))
: openssl_pkey_get_private(self::parse($thing));

if (false === $pkey) {
throw new UnexpectedValueException(sprintf(
Expand Down Expand Up @@ -242,18 +242,19 @@ private static function parse($thing, $type = self::KEY_TYPE_PRIVATE)
{
$src = $thing;

if (is_resource($src) || is_object($src) || is_array($src) || is_int(strpos($src, self::LOCAL_FILE_PROTOCOL))) {
if (is_resource($src) || is_object($src) || is_array($src) || (is_string($src) && is_int(strpos($src, self::LOCAL_FILE_PROTOCOL)))) {
return $src;
}

/** @var string $src */
if (is_int(strpos($src, '://'))) {
$protocol = parse_url($src, PHP_URL_SCHEME);
[$format, $kind, $offset] = static::RULES[$protocol] ?? [null, null, null];
[$format, $kind, $offset] = self::RULES[$protocol] ?? [null, null, null];
if ($format && $kind && $offset) {
$src = substr($src, $offset);
if ('public.pkcs1' === $protocol) {
$src = static::pkcs1ToSpki($src);
[$format, $kind] = static::RULES['public.spki'];
[$format, $kind] = self::RULES['public.spki'];
}
return sprintf($format, $kind, wordwrap($src, 64, self::CHR_LF, true));
}
Expand All @@ -268,7 +269,6 @@ private static function parse($thing, $type = self::KEY_TYPE_PRIVATE)
return self::parse(sprintf('%s://%s', $protocol, str_replace([self::CHR_CR, self::CHR_LF], '', $base64)), $type);
}
}
return $src;
}

return $src;
Expand Down
17 changes: 9 additions & 8 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ class Helpers
/**
* MD5 hash function
*
* @param string[] $things - To caculating things
* @param string $things - To caculating things
*
* @return string - The digest string
*/
public static function md5(...$things): string
public static function md5(string ...$things): string
{
$ctx = hash_init(static::ALGO_MD5);
$ctx = hash_init(self::ALGO_MD5);

array_walk($things, static function(string $thing) use ($ctx): void { hash_update($ctx, $thing); });

Expand All @@ -58,20 +58,21 @@ public static function md5(...$things): string
* @param string $thing - The certificatie(s) file path string or `data://text/plain;utf-8,...` (RFC2397) string
* @param string $pattern - The signatureAlgorithm matching pattern, default is `null` means for all
*
* @return array<?array{pem:string,attr:array<mixed>}> - The X509 Certificate instance list.
* @return array<array{'pem':string,'attr':array<mixed>}> - The X509 Certificate instance list.
*/
public static function load(string $thing, ?string $pattern = null): array
{
preg_match_all(static::X509_CERT_FORMAT, file_get_contents($thing) ?: '', $matches);
preg_match_all(self::X509_CERT_FORMAT, file_get_contents($thing) ?: '', $matches);

$certs = $matches['cert'] ?? [];

array_walk($certs, static function(string &$cert) use ($pattern): void {
$attr = openssl_x509_parse($cert, true);
[static::X509_ASN1_CERT_SIGNATURE_LONG_NAME => $algo] = $attr ?: [static::X509_ASN1_CERT_SIGNATURE_LONG_NAME => null];
$cert = $pattern && $algo && false === stripos($algo, $pattern) ? null : [static::CERT_PEM => $cert, static::CERT_ATTR => $attr];
[self::X509_ASN1_CERT_SIGNATURE_LONG_NAME => $algo] = $attr ?: [self::X509_ASN1_CERT_SIGNATURE_LONG_NAME => null];
$cert = $pattern && $algo && false === stripos($algo, $pattern) ? null : [self::CERT_PEM => $cert, self::CERT_ATTR => $attr];
});

/** @var array<array{'pem':string,'attr':array<mixed>}> $certs */
return array_filter($certs);
}

Expand Down Expand Up @@ -107,7 +108,7 @@ public static function sn(string $thing, ?string $pattern = null): string
* @var array<string,string> $issuer
* @var string $serial
*/
[static::X509_ASN1_CERT_ISSUER => $issuer, static::X509_ASN1_CERT_SERIAL => $serial] = $cert[static::CERT_ATTR];
[self::X509_ASN1_CERT_ISSUER => $issuer, self::X509_ASN1_CERT_SERIAL => $serial] = $cert[self::CERT_ATTR];

$carry[] = static::md5(static::fold($issuer), $serial);

Expand Down
4 changes: 2 additions & 2 deletions tests/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public function testConstractor(): void
public function configurationDataProvider(): array
{

$privateKey = openssl_pkey_get_private('file://' . sprintf(static::FIXTURES, 'pkcs8', 'key'));
$publicKey = openssl_pkey_get_public('file://' . sprintf(static::FIXTURES, 'spki', 'pem'));
$privateKey = openssl_pkey_get_private('file://' . sprintf(self::FIXTURES, 'pkcs8', 'key'));
$publicKey = openssl_pkey_get_public('file://' . sprintf(self::FIXTURES, 'spki', 'pem'));

if (false === $privateKey || false === $publicKey) {
throw new \Exception('Loading the pkey failed.');
Expand Down
54 changes: 27 additions & 27 deletions tests/ClientDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ public function testGetHandlerStack(array $config): void
*/
private function mockConfiguration(): array
{
$privateKey = openssl_pkey_get_private('file://' . sprintf(static::FIXTURES, 'pkcs8', 'key'));
$publicKey = openssl_pkey_get_public('file://' . sprintf(static::FIXTURES, 'spki', 'pem'));
$privateKey = openssl_pkey_get_private('file://' . sprintf(self::FIXTURES, 'pkcs8', 'key'));
$publicKey = openssl_pkey_get_public('file://' . sprintf(self::FIXTURES, 'spki', 'pem'));

if (false === $privateKey || false === $publicKey) {
throw new \Exception('Loading the pkey failed.');
Expand All @@ -172,35 +172,35 @@ public function withMockHandlerProvider(): array
[$privateKey, $publicKey] = $this->mockConfiguration();
/** @var array<string,string> $available */
$available = [
static::CONTENT_LANGUAGE => 'zh-CN',
static::CONTENT_LENGTH => '150',
static::CONTENT_TYPE => 'text/html;charset=GBK',
self::CONTENT_LANGUAGE => 'zh-CN',
self::CONTENT_LENGTH => '150',
self::CONTENT_TYPE => 'text/html;charset=GBK',
];
/** @var array<string,string> $unavailable */
$unavailable = [static::CONTENT_LENGTH => '0'];
$unavailable = [self::CONTENT_LENGTH => '0'];

return [
'HTTP 200, `GET` with `UTF-8` GOT `GBK` content in incompleted format' => [
$privateKey, $publicKey, 'GET', '',
static::pickResponse(200, $available,
self::pickResponse(200, $available,
$body = '<!DOCTYPE html><html>head></head><style></style><div id="Header"><script></script></div><div id="Info"></div><div id="Foot"></div><!--footer ending-->'),
$length = '150',
],
'HTTP 200, `POST` with `UTF-8` GOT `GBK` content in incompleted format' => [
$privateKey, $publicKey, 'POST', '',
static::pickResponse(200, $available, $body), $length,
self::pickResponse(200, $available, $body), $length,
],
'HTTP 200 `PATCH` with `UTF-8` GOT nothing' => [
$privateKey, $publicKey, 'PATCH', '',
static::pickResponse(200, $unavailable), $length = '0',
self::pickResponse(200, $unavailable), $length = '0',
],
'HTTP 200 `PUT` with `UTF-8` GOT nothing' => [
$privateKey, $publicKey, 'PUT', '',
static::pickResponse(200, $unavailable), $length,
self::pickResponse(200, $unavailable), $length,
],
'HTTP 200 `DELETE` with `UTF-8` GOT nothing' => [
$privateKey, $publicKey, 'DELETE', '',
static::pickResponse(200, $unavailable), $length,
self::pickResponse(200, $unavailable), $length,
],
];
}
Expand All @@ -227,14 +227,14 @@ public function testRequestsWithMockHandler($privateKey, $publicKey, string $met
$this->mock->append($response);

$res = $instance->request($method, $uri);
self::assertTrue($res->hasHeader(static::CONTENT_LENGTH));
self::assertTrue($res->hasHeader(self::CONTENT_LENGTH));
/** @var string $length */
[$length] = $res->getHeader(static::CONTENT_LENGTH);
[$length] = $res->getHeader(self::CONTENT_LENGTH);
self::assertIsString($length);
self::assertEquals($length, $contentLength);
if ($length !== '0') {
self::assertTrue($res->hasHeader(static::CONTENT_TYPE));
self::assertTrue($res->hasHeader(static::CONTENT_LANGUAGE));
self::assertTrue($res->hasHeader(self::CONTENT_TYPE));
self::assertTrue($res->hasHeader(self::CONTENT_LANGUAGE));
}
}

Expand Down Expand Up @@ -266,14 +266,14 @@ public function testAsyncRequestsWithMockHandler($privateKey, $publicKey, string
static::assertInstanceOf(\GuzzleHttp\Psr7\Request::class, $req);
static::assertEquals($method, $req->getMethod());
static::assertNotEquals($uri, $req->getRequestTarget());
static::assertTrue($res->hasHeader(static::CONTENT_LENGTH));
static::assertTrue($res->hasHeader(self::CONTENT_LENGTH));
/** @var string $length */
[$length] = $res->getHeader(static::CONTENT_LENGTH);
[$length] = $res->getHeader(self::CONTENT_LENGTH);
static::assertIsString($length);
static::assertEquals($length, $contentLength);
if ($length !== '0') {
static::assertTrue($res->hasHeader(static::CONTENT_TYPE));
static::assertTrue($res->hasHeader(static::CONTENT_LANGUAGE));
static::assertTrue($res->hasHeader(self::CONTENT_TYPE));
static::assertTrue($res->hasHeader(self::CONTENT_LANGUAGE));
}
})->wait();
}
Expand Down Expand Up @@ -309,7 +309,7 @@ private static function verification(RequestInterface $request, string $appId, s
}

/**
* @return array<string,array{mixed,mixed,string,string,string,string,array<string,mixed>,callable<ResponseInterface>}>
* @return array<string,array{mixed,mixed,string,string,string,string,array<string,mixed>,callable}>
*/
public function normalRequestsDataProvider(): array
{
Expand All @@ -320,7 +320,7 @@ public function normalRequestsDataProvider(): array
$privateKey, $publicKey, $appId = '2014072300007148', 'POST', '', $entryMethod = 'easy.alipay.ping',
$content = ['app_id' => $appId, 'user_id' => 'abcd1234'],
static function(RequestInterface $request) use ($privateKey, $publicKey, $appId, $entryMethod, $content): ResponseInterface {
static::verification($request, $appId, $entryMethod, $content, $publicKey);
self::verification($request, $appId, $entryMethod, $content, $publicKey);

$body = sprintf(
'{"%s%s":%s,"sign":"%s"}',
Expand All @@ -329,9 +329,9 @@ static function(RequestInterface $request) use ($privateKey, $publicKey, $appId,
Rsa::sign($payload, $privateKey)
);
/** @var array<string,mixed> $headers */
$headers = [static::CONTENT_LENGTH => strlen($body), static::CONTENT_TYPE => 'text/html;charset=utf-8'];
$headers = [self::CONTENT_LENGTH => strlen($body), self::CONTENT_TYPE => 'text/html;charset=utf-8'];

return static::pickResponse(200, $headers, $body);
return self::pickResponse(200, $headers, $body);
},
],
];
Expand Down Expand Up @@ -365,9 +365,9 @@ public function testRequest($privateKey, $publicKey, string $appId, string $meth

array_map(static function($key) use($res): void {
static::assertTrue($res->hasHeader($key));
}, [static::X_ALIPAY_RESPONDER, static::X_ALIPAY_VERIFIED, static::X_ALIPAY_SIGNATURE]);
}, [self::X_ALIPAY_RESPONDER, self::X_ALIPAY_VERIFIED, self::X_ALIPAY_SIGNATURE]);

self::assertEquals('ok', $res->getHeaderLine(static::X_ALIPAY_VERIFIED));
self::assertEquals('ok', $res->getHeaderLine(self::X_ALIPAY_VERIFIED));
self::assertEquals($content, json_decode((string)$res->getBody(), true));
}

Expand Down Expand Up @@ -400,8 +400,8 @@ public function testRequestAsync($privateKey, $publicKey, string $appId, string
->then(static function(ResponseInterface $res) use($content) {
array_map(static function($key) use($res): void {
static::assertTrue($res->hasHeader($key));
}, [static::X_ALIPAY_RESPONDER, static::X_ALIPAY_VERIFIED, static::X_ALIPAY_SIGNATURE]);
static::assertEquals('ok', $res->getHeaderLine(static::X_ALIPAY_VERIFIED));
}, [self::X_ALIPAY_RESPONDER, self::X_ALIPAY_VERIFIED, self::X_ALIPAY_SIGNATURE]);
static::assertEquals('ok', $res->getHeaderLine(self::X_ALIPAY_VERIFIED));
static::assertEquals($content, json_decode((string)$res->getBody(), true));
})->wait();
}
Expand Down
Loading

0 comments on commit 29218ef

Please sign in to comment.