Repository navigation
Expand file tree
/
Copy pathbytecode-keygen
More file actions
executable file
·116 lines (106 loc) · 3.15 KB
/
Copy pathbytecode-keygen
File metadata and controls
executable file
·116 lines (106 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env php
<?php
declare(strict_types=1);
// --- Rust-first dispatch -------------------------------------------------
// If a compiled Rust `bytecode-keygen` is available, hand off to it (it is a
// byte-compatible reimplementation). Set BYTECODE_NO_RUST=1 to force PHP.
(static function (): void {
if (getenv('BYTECODE_NO_RUST') || PHP_SAPI !== 'cli' || !function_exists('proc_open')) {
return;
}
$root = getenv('BYTECODE_ROOT') ?: dirname(__DIR__, 2);
$candidates = [];
if ($explicit = getenv('BYTECODE_KEYGEN_RS')) {
$candidates[] = $explicit;
}
$candidates[] = $root . '/rust/target/release/bytecode-keygen';
$candidates[] = __DIR__ . '/bytecode-keygen-rs';
$bin = null;
foreach ($candidates as $cand) {
if ($cand !== '' && is_file($cand) && is_executable($cand)) {
$bin = $cand;
break;
}
}
if ($bin === null) {
return;
}
// Repo root so --vendor-secret resolves build/vendor-secret.key the same way.
if (getenv('BYTECODE_ROOT') === false) {
putenv('BYTECODE_ROOT=' . dirname(__DIR__, 2));
}
global $argv;
$proc = @proc_open(
array_merge([$bin], array_slice($argv, 1)),
[0 => STDIN, 1 => STDOUT, 2 => STDERR],
$pipes
);
if (!is_resource($proc)) {
return; // could not launch -> fall back to PHP
}
exit(proc_close($proc));
})();
// --- end Rust-first dispatch ---------------------------------------------
function usage(): void
{
fwrite(STDERR, <<<'USAGE'
Usage:
bytecode-keygen
bytecode-keygen --out <path> [--force]
bytecode-keygen --vendor-secret [--force]
Prints a 32-byte key as 64 hex characters, or writes one to a key file.
--vendor-secret writes build/vendor-secret.key under the repository root.
USAGE);
}
function fail(string $message, int $code = 1): never
{
fwrite(STDERR, $message . PHP_EOL);
exit($code);
}
$args = array_slice($argv, 1);
if (in_array($args[0] ?? '', ['-h', '--help'], true)) {
usage();
exit(0);
}
$out = null;
$force = false;
while ($args) {
$arg = array_shift($args);
if ($arg === '--force') {
$force = true;
continue;
}
if ($arg === '--vendor-secret') {
$out = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'build' . DIRECTORY_SEPARATOR . 'vendor-secret.key';
continue;
}
if ($arg === '--out') {
if (!$args) {
fail('--out requires a path', 2);
}
$out = array_shift($args);
continue;
}
if (str_starts_with($arg, '--out=')) {
$out = substr($arg, strlen('--out='));
continue;
}
usage();
exit(2);
}
$key = bin2hex(random_bytes(32));
if ($out === null || $out === '') {
echo $key, PHP_EOL;
exit(0);
}
if (is_file($out) && !$force) {
fail("key file already exists: {$out} (pass --force to regenerate)", 2);
}
if (!is_dir(dirname($out)) && !@mkdir(dirname($out), 0770, true) && !is_dir(dirname($out))) {
fail("cannot create key directory: " . dirname($out));
}
if (file_put_contents($out, $key . PHP_EOL) === false) {
fail("cannot write key file: {$out}");
}
@chmod($out, 0600);
echo $out, PHP_EOL;