|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Utility for generating SimpleCacheRedisSodium configuration file. |
| 7 | + * |
| 8 | + * @package AWonderPHP/SimpleCacheRedis |
| 9 | + * @author Alice Wonder <paypal@domblogger.net> |
| 10 | + * @license https://opensource.org/licenses/MIT MIT |
| 11 | + * @link https://github.com/AliceWonderMiscreations/SimpleCacheRedis |
| 12 | + */ |
| 13 | + |
| 14 | +$PREFIX="DEFAULT"; |
| 15 | +$STRICT_TYPE=false; |
| 16 | + |
| 17 | +/** |
| 18 | + * Part of my silly amusement described below. |
| 19 | + * |
| 20 | + * @param string $s a hex digit to convert to pseudo-upper case. |
| 21 | + * |
| 22 | + * @return string |
| 23 | + */ |
| 24 | +function fancy_strtoupper($s) |
| 25 | +{ |
| 26 | + switch ($s) { |
| 27 | + case "0": |
| 28 | + return "#"; |
| 29 | + case "1": |
| 30 | + return "?"; |
| 31 | + case "2": |
| 32 | + return "@"; |
| 33 | + case "3": |
| 34 | + return "("; |
| 35 | + case "4": |
| 36 | + return ")"; |
| 37 | + case "5": |
| 38 | + return "z"; |
| 39 | + case "6": |
| 40 | + return "Q"; |
| 41 | + case "7": |
| 42 | + return "%"; |
| 43 | + case "8": |
| 44 | + return "!"; |
| 45 | + case "9": |
| 46 | + return ";"; |
| 47 | + default: |
| 48 | + return strtoupper($s); |
| 49 | + } |
| 50 | +}//end fancy_strtoupper() |
| 51 | + |
| 52 | + |
| 53 | +if (! $CWD = getcwd()) { |
| 54 | + exit(1); |
| 55 | +} |
| 56 | + |
| 57 | +if (isset($argv[1])) { |
| 58 | + $str = $argv[1]; |
| 59 | + $str = (string)$str; |
| 60 | + $str = strtoupper(trim($str)); |
| 61 | + if (strlen($str) < 3) { |
| 62 | + echo "WebApp Prefix must be three characters or longer."; |
| 63 | + exit(1); |
| 64 | + } |
| 65 | + if (strlen($str) > 32) { |
| 66 | + echo "WebApp Prefix can not be more than 32 characters in length."; |
| 67 | + exit(1); |
| 68 | + } |
| 69 | + if (preg_match('/[^A-Z0-9]/', $str) !== 0) { |
| 70 | + echo "WebApp Prefix can only contain [A-Z] and [0-9]."; |
| 71 | + exit(1); |
| 72 | + } |
| 73 | + $PREFIX = $str; |
| 74 | +} |
| 75 | + |
| 76 | +if (isset($argv[2])) { |
| 77 | + $str = $argv[2]; |
| 78 | + $str = (string)$str; |
| 79 | + $str = strtoupper(trim($str)); |
| 80 | + if ($str === "1") { |
| 81 | + $STRICT_TYPE = true; |
| 82 | + } elseif ($str === "TRUE") { |
| 83 | + $STRICT_TYPE = true; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +$arr = array(); |
| 88 | + |
| 89 | +// The secret - 32 byte int - valid for both chacha20poly1305 and aes256gcm |
| 90 | +$secret = random_bytes(32); |
| 91 | + |
| 92 | +if (function_exists('sodium_bin2hex')) { |
| 93 | + $arr['hexkey'] = sodium_bin2hex($secret); |
| 94 | +} else { |
| 95 | + $arr['hexkey'] = bin2hex($secret); |
| 96 | +} |
| 97 | +if (function_exists('sodium_memzero')) { |
| 98 | + sodium_memzero($secret); |
| 99 | +} |
| 100 | + |
| 101 | +// The prefix |
| 102 | +$arr['prefix'] = $PREFIX; |
| 103 | + |
| 104 | +// generate ourselves a salt |
| 105 | + |
| 106 | +// what I do here amuses me more than has actual |
| 107 | +// benefit - The salt isn't used for cryto. |
| 108 | +// |
| 109 | +// But hey, it amused me. |
| 110 | +$rand = random_bytes(32); |
| 111 | +if (function_exists('sodium_bin2hex')) { |
| 112 | + $salt = sodium_bin2hex($rand); |
| 113 | +} else { |
| 114 | + $salt = bin2hex($rand); |
| 115 | +} |
| 116 | +// just because we can, really don't need to |
| 117 | +$j = random_int(7, 27); |
| 118 | +for ($i=0; $i<$j; $i++) { |
| 119 | + $salt = str_shuffle($salt); |
| 120 | +} |
| 121 | +// again just because we can |
| 122 | +for ($i=0; $i<64; $i++) { |
| 123 | + $n = random_int(0, 1); |
| 124 | + if ($n === 1) { |
| 125 | + $salt[$i] = fancy_strtoupper($salt[$i]); |
| 126 | + } |
| 127 | +} |
| 128 | +$arr['salt'] = $salt; |
| 129 | +// okay end of silly amusement. |
| 130 | + |
| 131 | +$arr['strict'] = $STRICT_TYPE; |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | +$file = $CWD . '/' . $PREFIX . '.json'; |
| 136 | + |
| 137 | +if (file_exists($file)) { |
| 138 | + $backup = $CWD . '/' . $PREFIX . '-' . time() . '.json'; |
| 139 | + if (! copy($file, $backup)) { |
| 140 | + echo "Could not create backup of existing file. Exiting now\n"; |
| 141 | + exit(1); |
| 142 | + } |
| 143 | + unlink($file); |
| 144 | +} |
| 145 | +$json = json_encode($arr, JSON_PRETTY_PRINT) . "\n"; |
| 146 | + |
| 147 | +if (! $handle = @fopen($file, 'w')) { |
| 148 | + echo "Could not open " . $file . " for writing. Exiting now\n"; |
| 149 | + exit(1); |
| 150 | +} |
| 151 | +fwrite($handle, $json); |
| 152 | +fclose($handle); |
| 153 | +if (function_exists('sodium_memzero')) { |
| 154 | + sodium_memzero($json); |
| 155 | + sodium_memzero($arr['hexkey']); |
| 156 | +} |
| 157 | + |
| 158 | +echo "Configuration file generated: " . $file . "\n"; |
| 159 | + |
| 160 | +?> |
0 commit comments