Skip to content

Instantly share code, notes, and snippets.

@cereal-s
Created November 28, 2016 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cereal-s/d54486d82dfa259f150f092c326928ad to your computer and use it in GitHub Desktop.
Save cereal-s/d54486d82dfa259f150f092c326928ad to your computer and use it in GitHub Desktop.
ROT13 implementation
<?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