-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.php
More file actions
100 lines (87 loc) · 2.53 KB
/
hash.php
File metadata and controls
100 lines (87 loc) · 2.53 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
<?php
/**
* Hash Visualization CLI (SVG generator)
*
* Based on ideas from Perrig & Song "Hash Visualization".
*
* Usage:
* php hash.php "your input string" [--outfile=out.svg]
* php hash.php -o out.svg "your input string"
*
* - Deterministic: same input => same image
* - Output: SVG with circular form (transparent background),
* overlapping basic shapes (triangle, square, circle) in a 20‑color palette.
* - No external libraries required.
*/
require __DIR__ . '/vendor/autoload.php';
use ANSCD\ShapeHash\Generator;
// Ensure CLI
if (php_sapi_name() !== 'cli') {
fwrite(STDERR, "This script must be run from the command line." . PHP_EOL);
exit(1);
}
// -----------------------------
// Option parsing
// -----------------------------
$argvCopy = $argv;
array_shift($argvCopy); // remove script name
$inputString = null;
$outFile = 'out.svg';
for ($i = 0; $i < count($argvCopy); $i++) {
$arg = $argvCopy[$i];
if ($arg === '-h' || $arg === '--help') {
usage();
exit(0);
}
if (strncmp($arg, '--outfile=', 10) === 0) {
$outFile = basename(substr($arg, 10));
continue;
}
if ($arg === '-o' || $arg === '--outfile') {
$i++;
if ($i >= count($argvCopy)) {
fwrite(STDERR, "Missing value after $arg." . PHP_EOL);
usage(1);
}
$outFile = basename($argvCopy[$i]);
continue;
}
// The first non-option is the input string
if ($inputString === null) {
$inputString = $arg;
continue;
}
// If there's more than one positional argument, join them with space (allows unquoted multi-word input)
$inputString .= ' ' . $arg;
}
if ($inputString === null) {
fwrite(STDERR, "Error: missing input string." . PHP_EOL);
usage(1);
}
function usage(int $code = 0): void {
$msg = <<<TXT
Hash Visualization (SVG)
Usage:
php hash.php "your input" [--outfile=out.svg]
php hash.php -o out.svg "your input"
Options:
-o, --outfile <path> Output file path (default: out.svg)
-h, --help Show this help
TXT;
if ($code === 0) {
fwrite(STDOUT, $msg . PHP_EOL);
} else {
fwrite(STDERR, $msg . PHP_EOL);
exit($code);
}
}
// -----------------------------
// Generate and write SVG
// -----------------------------
$svg = Generator::generateSvg($inputString);
$result = @file_put_contents($outFile, $svg);
if ($result === false) {
fwrite(STDERR, "Failed to write to '$outFile'." . PHP_EOL);
exit(2);
}
fwrite(STDOUT, "SVG generated: $outFile" . PHP_EOL);