Skip to content

Instantly share code, notes, and snippets.

@xupengzhuo
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save xupengzhuo/b72634b0ce7694ba9680 to your computer and use it in GitHub Desktop.

Select an option

Save xupengzhuo/b72634b0ce7694ba9680 to your computer and use it in GitHub Desktop.
用于32位的unsigned int数据加密解密
<?php
function api_encode_uid($uid)
{
$sid = ($uid & 0x0000ff00) << 16;
$sid += (($uid & 0xff000000) >> 8) & 0x00ff0000;
$sid += ($uid & 0x000000ff) << 8;
$sid += ($uid & 0x00ff0000) >> 16;
$sid ^= 282335;
return $sid;
}
function decode($int)
{
$result = 0;
$int ^= 282335;
$result += (($int & 0xff000000) >> 16) & 0x0000ff00;
$result += ($int & 0x00ff0000) << 8;
$result += (($int & 0x0000ff00) >> 8);
$result += ($int & 0x000000ff) << 16;
return $result;
}
$id = 1024;
echo 'raw:', dechex($id);
echo '<br>';
$a = api_encode_uid($id);
echo '<br>';
$b = decode($a);
echo "<br>";
echo 'result:', dechex($b);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment