(string $raw)
| 53 | } |
| 54 | |
| 55 | private static function parseKeyString(string $raw): ?string |
| 56 | { |
| 57 | $s = trim($raw); |
| 58 | if ($s === '') { |
| 59 | return null; |
| 60 | } |
| 61 | |
| 62 | // base64:... convenience |
| 63 | if (stripos($s, 'base64:') === 0) { |
| 64 | $b = substr($s, 7); |
| 65 | $bin = base64_decode($b, true); |
| 66 | if (is_string($bin) && strlen($bin) === SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES) { |
| 67 | return $bin; |
| 68 | } |
| 69 | return null; |
| 70 | } |
| 71 | |
| 72 | // hex key (64 hex chars => 32 bytes) |
| 73 | if (preg_match('/^[a-f0-9]{64}$/i', $s)) { |
| 74 | $bin = hex2bin($s); |
| 75 | if (is_string($bin) && strlen($bin) === SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES) { |
| 76 | return $bin; |
| 77 | } |
| 78 | return null; |
| 79 | } |
| 80 | |
| 81 | // raw (unlikely via env); accept only exact key length |
| 82 | if (strlen($s) === SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES) { |
| 83 | return $s; |
| 84 | } |
| 85 | |
| 86 | return null; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Decode a key string without persisting it. |
no outgoing calls
no test coverage detected