Created
November 28, 2016 17:45
-
-
Save cereal-s/d54486d82dfa259f150f092c326928ad to your computer and use it in GitHub Desktop.
ROT13 implementation
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 | |
/** | |
* ROT13 implementation | |
* | |
* @see https://en.wikipedia.org/wiki/ROT13 | |
* @param string $message | |
* @return string | |
*/ | |
function rot13($message) | |
{ | |
$src_bytes = unpack('C*', $message); | |
$dst_bytes = []; | |
foreach($src_bytes as $byte) | |
{ | |
if(($byte >= ord('A') && $byte <= ord('N')) || ($byte >= ord('a') && $byte <= ord('n'))) | |
$dst_bytes[] = $byte + 13; | |
elseif(($byte >= ord('M') && $byte <= ord('Z')) || ($byte >= ord('m') && $byte <= ord('z'))) | |
$dst_bytes[] = $byte - 13; | |
else | |
$dst_bytes[] = $byte; | |
} | |
return call_user_func_array("pack", array_merge(array("C*"), $dst_bytes)); | |
} | |
$message = 'Hello World'; | |
$encode = rot13($message); | |
$decode = rot13($encode); | |
print "{$encode}\n{$decode}\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment