1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

ext/phar: Improve signature tests

This commit is contained in:
Gina Peter Banyard
2025-10-16 21:02:08 +01:00
parent e5d0e62793
commit 670a8f73cc
3 changed files with 125 additions and 65 deletions

View File

@@ -2,6 +2,7 @@
Phar::setSupportedSignatures() with hash
--EXTENSIONS--
phar
openssl
--SKIPIF--
<?php
$arr = Phar::getSupportedSignatures();
@@ -15,70 +16,89 @@ phar.readonly=0
$fname = __DIR__ . '/' . basename(__FILE__, '.php') . '.phar';
$p = new Phar($fname);
$p['file1.txt'] = 'hi';
echo "Default:\n";
var_dump($p->getSignature());
echo "Set MD5:\n";
$p->setSignatureAlgorithm(Phar::MD5);
var_dump($p->getSignature());
echo "Set SHA1:\n";
$p->setSignatureAlgorithm(Phar::SHA1);
var_dump($p->getSignature());
echo "Set SHA256:\n";
try {
$p->setSignatureAlgorithm(Phar::SHA256);
var_dump($p->getSignature());
} catch (Exception $e) {
echo $e->getMessage();
$p->setSignatureAlgorithm(Phar::SHA256);
var_dump($p->getSignature());
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
echo "Set SHA512:\n";
try {
$p->setSignatureAlgorithm(Phar::SHA512);
var_dump($p->getSignature());
} catch (Exception $e) {
echo $e->getMessage();
$p->setSignatureAlgorithm(Phar::SHA512);
var_dump($p->getSignature());
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
echo "Set OPENSSL:\n";
try {
$config = __DIR__ . '/files/openssl.cnf';
$config_arg = array('config' => $config);
$private = openssl_get_privatekey(file_get_contents(__DIR__ . '/files/private.pem'));
$pkey = '';
openssl_pkey_export($private, $pkey, NULL, $config_arg);
$p->setSignatureAlgorithm(Phar::OPENSSL, $pkey);
var_dump($p->getSignature());
} catch (Exception $e) {
echo $e->getMessage();
$config = __DIR__ . '/files/openssl.cnf';
$config_arg = array('config' => $config);
$private = openssl_get_privatekey(file_get_contents(__DIR__ . '/files/private.pem'));
$pkey = '';
openssl_pkey_export($private, $pkey, NULL, $config_arg);
$p->setSignatureAlgorithm(Phar::OPENSSL, $pkey);
var_dump($p->getSignature());
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
?>
--CLEAN--
<?php
unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.phar');
?>
--EXPECTF--
Default:
array(2) {
["hash"]=>
string(%d) "%s"
["hash_type"]=>
string(7) "SHA-256"
}
Set MD5:
array(2) {
["hash"]=>
string(%d) "%s"
["hash_type"]=>
string(3) "MD5"
}
Set SHA1:
array(2) {
["hash"]=>
string(%d) "%s"
["hash_type"]=>
string(5) "SHA-1"
}
Set SHA256:
array(2) {
["hash"]=>
string(%d) "%s"
["hash_type"]=>
string(7) "SHA-256"
}
Set SHA512:
array(2) {
["hash"]=>
string(%d) "%s"
["hash_type"]=>
string(7) "SHA-512"
}
Set OPENSSL:
array(2) {
["hash"]=>
string(%d) "%s"

View File

@@ -2,6 +2,7 @@
Phar::setSupportedSignatures() with hash, tar-based
--EXTENSIONS--
phar
openssl
--SKIPIF--
<?php
$arr = Phar::getSupportedSignatures();
@@ -15,86 +16,109 @@ phar.readonly=0
$fname = __DIR__ . '/' . basename(__FILE__, '.php') . '.phar.tar';
$p = new Phar($fname);
$p['file1.txt'] = 'hi';
echo "Default:\n";
var_dump($p->getSignature());
echo "Set MD5:\n";
$p->setSignatureAlgorithm(Phar::MD5);
var_dump($p->getSignature());
echo "Set SHA1:\n";
$p->setSignatureAlgorithm(Phar::SHA1);
var_dump($p->getSignature());
echo "Set SHA256:\n";
try {
$p->setSignatureAlgorithm(Phar::SHA256);
var_dump($p->getSignature());
} catch (Exception $e) {
echo $e->getMessage();
$p->setSignatureAlgorithm(Phar::SHA256);
var_dump($p->getSignature());
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
echo "Set SHA512:\n";
try {
$p->setSignatureAlgorithm(Phar::SHA512);
var_dump($p->getSignature());
} catch (Exception $e) {
echo $e->getMessage();
$p->setSignatureAlgorithm(Phar::SHA512);
var_dump($p->getSignature());
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
echo "Set OPENSSL:\n";
try {
$config = __DIR__ . '/../files/openssl.cnf';
$config_arg = array('config' => $config);
$private = openssl_get_privatekey(file_get_contents(dirname(__DIR__) . '/files/private.pem'));
$pkey = '';
openssl_pkey_export($private, $pkey, NULL, $config_arg);
$p->setSignatureAlgorithm(Phar::OPENSSL, $pkey);
var_dump($p->getSignature());
$p->setSignatureAlgorithm(Phar::OPENSSL_SHA512, $pkey);
var_dump($p->getSignature());
$p->setSignatureAlgorithm(Phar::OPENSSL_SHA256, $pkey);
var_dump($p->getSignature());
} catch (Exception $e) {
echo $e->getMessage();
$config = __DIR__ . '/../files/openssl.cnf';
$config_arg = array('config' => $config);
$private = openssl_get_privatekey(file_get_contents(dirname(__DIR__) . '/files/private.pem'));
$pkey = '';
openssl_pkey_export($private, $pkey, NULL, $config_arg);
$p->setSignatureAlgorithm(Phar::OPENSSL, $pkey);
var_dump($p->getSignature());
echo "Set OPENSSL_SHA512:\n";
$p->setSignatureAlgorithm(Phar::OPENSSL_SHA512, $pkey);
var_dump($p->getSignature());
echo "Set OPENSSL_SHA256:\n";
$p->setSignatureAlgorithm(Phar::OPENSSL_SHA256, $pkey);
var_dump($p->getSignature());
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
?>
--CLEAN--
<?php
unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.phar.tar');
?>
--EXPECTF--
Default:
array(2) {
["hash"]=>
string(%d) "%s"
["hash_type"]=>
string(7) "SHA-256"
}
Set MD5:
array(2) {
["hash"]=>
string(%d) "%s"
["hash_type"]=>
string(3) "MD5"
}
Set SHA1:
array(2) {
["hash"]=>
string(%d) "%s"
["hash_type"]=>
string(5) "SHA-1"
}
Set SHA256:
array(2) {
["hash"]=>
string(%d) "%s"
["hash_type"]=>
string(7) "SHA-256"
}
Set SHA512:
array(2) {
["hash"]=>
string(%d) "%s"
["hash_type"]=>
string(7) "SHA-512"
}
Set OPENSSL:
array(2) {
["hash"]=>
string(%d) "%s"
["hash_type"]=>
string(7) "OpenSSL"
}
Set OPENSSL_SHA512:
array(2) {
["hash"]=>
string(%d) "%s"
["hash_type"]=>
string(14) "OpenSSL_SHA512"
}
Set OPENSSL_SHA256:
array(2) {
["hash"]=>
string(%d) "%s"

View File

@@ -20,49 +20,59 @@ $fname5 = __DIR__ . '/' . basename(__FILE__, '.php') . '.5.phar.zip';
$fname6 = __DIR__ . '/' . basename(__FILE__, '.php') . '.6.phar.zip';
$p = new Phar($fname);
$p['file1.txt'] = 'hi';
echo "Default:\n";
var_dump($p->getSignature());
echo "Set MD5:\n";
$p->setSignatureAlgorithm(Phar::MD5);
copy($fname, $fname2);
$p = new Phar($fname2);
var_dump($p->getSignature());
echo "Set SHA1:\n";
$p->setSignatureAlgorithm(Phar::SHA1);
copy($fname2, $fname3);
$p = new Phar($fname3);
var_dump($p->getSignature());
echo "Set SHA256:\n";
try {
$p->setSignatureAlgorithm(Phar::SHA256);
copy($fname3, $fname4);
$p = new Phar($fname4);
var_dump($p->getSignature());
} catch (Exception $e) {
echo $e->getMessage();
$p->setSignatureAlgorithm(Phar::SHA256);
copy($fname3, $fname4);
$p = new Phar($fname4);
var_dump($p->getSignature());
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
try {
$p->setSignatureAlgorithm(Phar::SHA512);
copy($fname4, $fname5);
$p = new Phar($fname5);
var_dump($p->getSignature());
} catch (Exception $e) {
echo $e->getMessage();
}
try {
$config = __DIR__ . '/../files/openssl.cnf';
$config_arg = array('config' => $config);
$keys=openssl_pkey_new($config_arg);
openssl_pkey_export($keys, $privkey, NULL, $config_arg);
$pubkey=openssl_pkey_get_details($keys);
$p->setSignatureAlgorithm(Phar::OPENSSL, $privkey);
copy($fname5, $fname6);
file_put_contents($fname6 . '.pubkey', $pubkey['key']);
$p = new Phar($fname6);
var_dump($p->getSignature());
} catch (Exception $e) {
echo $e->getMessage();
echo "Set SHA512:\n";
try {
$p->setSignatureAlgorithm(Phar::SHA512);
copy($fname4, $fname5);
$p = new Phar($fname5);
var_dump($p->getSignature());
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
echo "Set OPENSSL:\n";
try {
$config = __DIR__ . '/../files/openssl.cnf';
$config_arg = array('config' => $config);
$keys=openssl_pkey_new($config_arg);
openssl_pkey_export($keys, $privkey, NULL, $config_arg);
$pubkey=openssl_pkey_get_details($keys);
$p->setSignatureAlgorithm(Phar::OPENSSL, $privkey);
copy($fname5, $fname6);
file_put_contents($fname6 . '.pubkey', $pubkey['key']);
$p = new Phar($fname6);
var_dump($p->getSignature());
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
?>
--CLEAN--
@@ -76,36 +86,42 @@ unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.6.phar.zip');
unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.6.phar.zip.pubkey');
?>
--EXPECTF--
Default:
array(2) {
["hash"]=>
string(%d) "%s"
["hash_type"]=>
string(7) "SHA-256"
}
Set MD5:
array(2) {
["hash"]=>
string(%d) "%s"
["hash_type"]=>
string(3) "MD5"
}
Set SHA1:
array(2) {
["hash"]=>
string(%d) "%s"
["hash_type"]=>
string(5) "SHA-1"
}
Set SHA256:
array(2) {
["hash"]=>
string(%d) "%s"
["hash_type"]=>
string(7) "SHA-256"
}
Set SHA512:
array(2) {
["hash"]=>
string(%d) "%s"
["hash_type"]=>
string(7) "SHA-512"
}
Set OPENSSL:
array(2) {
["hash"]=>
string(%d) "%s"