idObfuscation.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. function getObfuscationSalt(){
  3. $saltFile=dirname(__FILE__)."/idObfuscation_salt.php";
  4. if(file_exists($saltFile)){
  5. require $saltFile;
  6. }else{
  7. $bytes=openssl_random_pseudo_bytes(4);
  8. $sf=fopen($saltFile,"w");
  9. fwrite($sf,chr(60)."?php\n");
  10. fwrite($sf,'$OBFUSCATION_SALT=0x'.bin2hex($bytes).";\n");
  11. fwrite($sf,"?".chr(62));
  12. fclose($sf);
  13. require $saltFile;
  14. }
  15. return isset($OBFUSCATION_SALT)?$OBFUSCATION_SALT:0;
  16. }
  17. /*
  18. This is a simple reversible hash function I made for encoding and decoding test IDs.
  19. It is not cryptographically secure, don't use it to hash passwords or something!
  20. */
  21. function obfdeobf($id){
  22. $salt=getObfuscationSalt()&0xFFFFFFFF;
  23. $id=$id&0xFFFFFFFF;
  24. for($i=0;$i<16;$i++){
  25. $id=$id^$salt;
  26. $id=(($id>>1)&0xFFFFFFFF)|(($id&0x00000001)<<31);
  27. $salt=(($salt<<1)&0xFFFFFFFF)|(($salt&0xA0000000)>>31);
  28. }
  29. return $id;
  30. }
  31. function obfuscateId($id){
  32. return base_convert(obfdeobf($id),10,36);
  33. }
  34. function deobfuscateId($id){
  35. return obfdeobf(base_convert($id,36,10));
  36. }
  37. //IMPORTANT: DO NOT ADD ANYTHING BELOW THE PHP CLOSING TAG, NOT EVEN EMPTY LINES!
  38. ?>