Skip to content

Commit

Permalink
Add support for specifying Hash algorithm to use
Browse files Browse the repository at this point in the history
for phar generation (SHA-1, SHA-256, SHA-512)
  • Loading branch information
theseer committed Nov 10, 2015
1 parent 2696aac commit 2e27e20
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ private function runSaver($code) {
if ($aliasName = $this->config->getPharAliasName()) {
$pharBuilder->setAliasName($aliasName);
}
if ($this->config->hasPharHashAlgorithm()) {
$pharBuilder->setSignatureType($this->config->getPharHashAlgorithm());
}
$pharBuilder->build($output, $code);
$this->logger->log("\nphar archive '{$output}' generated.\n\n");
return CLI::RC_OK;
Expand Down
12 changes: 12 additions & 0 deletions src/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ private function configure(\ezcConsoleInput $input) {
$config->setVariable('PHAR',
$input->getOption('alias')->value ? $input->getOption('alias')->value : basename($output)
);
if ($hashAlgorithm = $input->getOption('hash')->value) {
$config->setPharHashAlgorithm($hashAlgorithm);
}
}

if ($input->getOption('cache')->value) {
Expand Down Expand Up @@ -281,6 +284,7 @@ protected function showUsage() {
-o, --output Output file for generated code (default: STDOUT)
-p, --phar Create a phar archive (requires -o )
--hash Force given hash algorithm (SHA-1, SHA-256 or SHA-512) (requires -p, conflicts with --key)
--bzip2 Compress phar archive using bzip2 (requires -p) (bzip2 required)
--gz Compress phar archive using gzip (requires -p) (gzip required)
--key OpenSSL key file to use for signing phar archive (requires -p) (openssl required)
Expand Down Expand Up @@ -390,6 +394,14 @@ protected function setupInput() {
array( new \ezcConsoleOptionRule( $input->getOption( 'p' ) ) )
));

$this->outputOption = $input->registerOption( new \ezcConsoleOption(
'', 'hash', \ezcConsoleInput::TYPE_STRING, NULL, FALSE,
'Force given hash algorithm (SHA-1, SHA-256 or SHA-512) (requires -p)',
NULL,
array( new \ezcConsoleOptionRule( $input->getOption( 'p' ) ) ),
array( new \ezcConsoleOptionRule( $input->getOption( 'key' ) ) )
));

$input->registerOption( new \ezcConsoleOption(
'i', 'include', \ezcConsoleInput::TYPE_STRING, '*.php', TRUE,
'File pattern to include (default: *.php)'
Expand Down
24 changes: 24 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Config {
private $pharKey;
private $pharAll = false;
private $pharAliasName = '';
private $pharHashAlgorithm;
private $followSymlinks = false;
private $cacheFilename;
private $prepend = false;
Expand Down Expand Up @@ -244,6 +245,29 @@ public function getPharAliasName() {
return $this->pharAliasName;
}

public function hasPharHashAlgorithm() {
return $this->pharHashAlgorithm !== null;
}

/**
* @return string
*/
public function getPharHashAlgorithm() {
return $this->pharHashAlgorithm;
}

/**
* @param string $pharHashAlgorithm
*/
public function setPharHashAlgorithm($pharHashAlgorithm) {
if (!in_array($pharHashAlgorithm, array('SHA-512','SHA-256','SHA-1'))) {
throw new \InvalidArgumentException(
sprintf('Algorithm %s not supported', $pharHashAlgorithm)
);
}
$this->pharHashAlgorithm = $pharHashAlgorithm;
}

public function setPhp($php) {
$this->php = $php;
}
Expand Down
26 changes: 20 additions & 6 deletions src/PharBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,16 @@ class PharBuilder {
private $key;
private $basedir;
private $aliasName;
private $signatureType;

private $directories = array();

private $supportedSignatureTypes = array(
'SHA-512' => \Phar::SHA512,
'SHA-256' => \Phar::SHA256,
'SHA-1' => \Phar::SHA1
);

public function __construct(DirectoryScanner $scanner, $basedir) {
$this->scanner = $scanner;
$this->basedir = $basedir;
Expand All @@ -58,6 +65,15 @@ public function setCompressionMode($mode) {
$this->compression = $mode;
}

public function setSignatureType($type) {
if (!in_array($type, array_keys($this->supportedSignatureTypes))) {
throw new \InvalidArgumentException(
sprintf('Signature type "%s" not known or not supported by this PHP installation.', $type)
);
}
$this->signatureType = $type;
}

public function setSignatureKey($key) {
$this->key = $key;
}
Expand Down Expand Up @@ -99,13 +115,11 @@ public function build($filename, $stub) {
}

private function selectSignatureType(\Phar $phar) {
$map = array(
'SHA-512' => \Phar::SHA512,
'SHA-256' => \Phar::SHA256,
'SHA-1' => \Phar::SHA1
);
if ($this->signatureType !== NULL) {
return $this->supportedSignatureTypes[$this->signatureType];
}
$supported = $phar->getSupportedSignatures();
foreach($map as $candidate => $type) {
foreach($this->supportedSignatureTypes as $candidate => $type) {
if (in_array($candidate, $supported)) {
return $type;
}
Expand Down

0 comments on commit 2e27e20

Please sign in to comment.