Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/files/management/PhabricatorFilesManagementGenerateKeyWorkflow.php
13419 views
1
<?php
2
3
final class PhabricatorFilesManagementGenerateKeyWorkflow
4
extends PhabricatorFilesManagementWorkflow {
5
6
protected function didConstruct() {
7
$this
8
->setName('generate-key')
9
->setSynopsis(
10
pht('Generate an encryption key.'))
11
->setArguments(
12
array(
13
array(
14
'name' => 'type',
15
'param' => 'keytype',
16
'help' => pht('Select the type of key to generate.'),
17
),
18
));
19
}
20
21
public function execute(PhutilArgumentParser $args) {
22
$type = $args->getArg('type');
23
if (!strlen($type)) {
24
throw new PhutilArgumentUsageException(
25
pht(
26
'Specify the type of key to generate with --type.'));
27
}
28
29
$format = PhabricatorFileStorageFormat::getFormat($type);
30
if (!$format) {
31
throw new PhutilArgumentUsageException(
32
pht(
33
'No key type "%s" exists.',
34
$type));
35
}
36
37
if (!$format->canGenerateNewKeyMaterial()) {
38
throw new PhutilArgumentUsageException(
39
pht(
40
'Storage format "%s" can not generate keys.',
41
$format->getStorageFormatName()));
42
}
43
44
$material = $format->generateNewKeyMaterial();
45
46
$structure = array(
47
'name' => 'generated-key-'.Filesystem::readRandomCharacters(12),
48
'type' => $type,
49
'material.base64' => $material,
50
);
51
52
$json = id(new PhutilJSON())->encodeFormatted($structure);
53
54
echo tsprintf(
55
"%s: %s\n\n%B\n",
56
pht('Key Material'),
57
$format->getStorageFormatName(),
58
$json);
59
60
return 0;
61
}
62
63
}
64
65