Created
September 27, 2012 17:14
-
-
Save borisguery/3795209 to your computer and use it in GitHub Desktop.
PHP byteArray?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** @link http://madskristensen.net/post/A-shorter-and-URL-friendly-GUID.aspx */ | |
function fromBase64($input) { | |
$segments = unpack('C*', base64_decode(str_replace(array('_', '-'), array('/', '+'), $input))); | |
array_walk($segments, function(&$segment, $key) { | |
$segment = dechex($segment); | |
}); | |
list ($a, $b, $c, $d, $e) = array( | |
unpack('I*', pack('H*', implode('', array_slice($segments, 0, 4)))), | |
unpack('n*', pack('H*', implode('', array_slice($segments, 4, 2)))), | |
unpack('n*', pack('H*', implode('', array_slice($segments, 6, 2)))), | |
unpack('n*', pack('H*', implode('', array_slice($segments, 8, 2)))), | |
array(hexdec(implode('', array_slice($segments, 10)))), | |
); | |
$result = ''; | |
foreach (array($a, $b, $c, $d, $e) as $segment) { | |
$result .= dechex(end($segment)); | |
$result .= '-'; | |
} | |
return rtrim($result, '-'); | |
} | |
function toBase64($input) { | |
$segments = explode('-', $input); | |
list ($a, $b, $c, $d) = array( | |
pack('I*', hexdec($segments[0])), | |
pack('n*', hexdec($segments[1])), | |
pack('n*', hexdec($segments[2])), | |
pack('n*', hexdec($segments[3])), | |
); | |
$e = str_split($segments[4], 2); | |
array_walk($e, function(&$byte, $key){ | |
$byte = pack('c', hexdec($byte)); | |
}); | |
$e = implode('', $e); | |
$result = implode('', array($a, $b, $c, $d, $e)); | |
$result = base64_encode($result); | |
$result = str_replace(array('/', '+'), array('_', '-'), $result); | |
$result = rtrim($result, '='); | |
return $result; | |
} | |
// Main | |
$options = getopt("edt"); | |
if (isset($options['e'])) { | |
printf("Input => %s => Output => %s\n", $argv[2], toBase64($argv[2])); | |
exit(1); | |
} elseif (isset($options['d'])) { | |
printf("Input => %s => Output => %s\n", $argv[2], fromBase64($argv[2])); | |
exit(1); | |
} elseif (isset($options['t'])) { | |
$dataProvider = array('c9a646d3-9c61-4cb7-bfcd-ee2522c8f633'); | |
for ($i = 30; 0 !== $i; --$i) { | |
$dataProvider[] = sprintf( | |
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x', | |
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), | |
mt_rand(0, 0x0fff) | 0x4000, | |
mt_rand(0, 0x3fff) | 0x8000, | |
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) | |
); | |
} | |
foreach ($dataProvider as $guid) { | |
$encoded = toBase64($guid); | |
$decoded = fromBase64($encoded); | |
$mask = "%30s >> %-0.40s << %40.40s = %2s\n"; | |
printf($mask, $guid, $encoded, $decoded, ($decoded === $guid) ? "\033[0;32m ✓ \033[0m" : "\033[0;31m ✘ \033[0m"); | |
} | |
exit(1); | |
} else { | |
printf("Usage: %s [-d|-e|-t] [string]\n", ltrim($argv[0], './')); | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment