Path: blob/master/src/applications/files/management/PhabricatorFilesManagementGenerateKeyWorkflow.php
13419 views
<?php12final class PhabricatorFilesManagementGenerateKeyWorkflow3extends PhabricatorFilesManagementWorkflow {45protected function didConstruct() {6$this7->setName('generate-key')8->setSynopsis(9pht('Generate an encryption key.'))10->setArguments(11array(12array(13'name' => 'type',14'param' => 'keytype',15'help' => pht('Select the type of key to generate.'),16),17));18}1920public function execute(PhutilArgumentParser $args) {21$type = $args->getArg('type');22if (!strlen($type)) {23throw new PhutilArgumentUsageException(24pht(25'Specify the type of key to generate with --type.'));26}2728$format = PhabricatorFileStorageFormat::getFormat($type);29if (!$format) {30throw new PhutilArgumentUsageException(31pht(32'No key type "%s" exists.',33$type));34}3536if (!$format->canGenerateNewKeyMaterial()) {37throw new PhutilArgumentUsageException(38pht(39'Storage format "%s" can not generate keys.',40$format->getStorageFormatName()));41}4243$material = $format->generateNewKeyMaterial();4445$structure = array(46'name' => 'generated-key-'.Filesystem::readRandomCharacters(12),47'type' => $type,48'material.base64' => $material,49);5051$json = id(new PhutilJSON())->encodeFormatted($structure);5253echo tsprintf(54"%s: %s\n\n%B\n",55pht('Key Material'),56$format->getStorageFormatName(),57$json);5859return 0;60}6162}636465