PHP Password Encode/Decoder


/ Published in: PHP
Save to your folder(s)

I had the hardest time finding a reliable way to encode/decode the password temporarily for an integration. So here it is.


Copy this code and paste it in your HTML
  1. class Hash{
  2. var $scramble1;
  3. var $scramble2;
  4.  
  5. var $errors;
  6. var $adj;
  7. var $mod;
  8. function Hash(){
  9. $this->errors = array();
  10.  
  11. $this->scramble1 = '! #$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~';
  12. $this->scramble2 = 'f^jAE]okIOzU[2&q1{3`h5w_794p@6s8?BgP>dFV=m D<TcS%Ze|r:lGK/uCy.Jx)HiQ!#$~(;Lt-R}Ma,NvW+Ynb*0X';
  13.  
  14. if (strlen($this->scramble1) <> strlen($this->scramble2)) {
  15. trigger_error('** SCRAMBLE1 is not same length as SCRAMBLE2 **', E_USER_ERROR);
  16. }
  17. $this->adj = 1.75;
  18. $this->mod = 3;
  19. }
  20. function decrypt ($key, $source){
  21. $this->errors = array();
  22. $fudgefactor = $this->_convertKey($key);
  23. if ($this->errors) return;
  24. if (empty($source)) {
  25. $this->errors[] = 'No value has been supplied for decryption';
  26. return;
  27. }
  28. $target = null;
  29. $factor2 = 0;
  30. for ($i = 0; $i < strlen($source); $i++) {
  31. if (function_exists('mb_substr')) {
  32. $char2 = mb_substr($source, $i, 1);
  33. } else {
  34. $char2 = substr($source, $i, 1);
  35. }
  36. $num2 = strpos($this->scramble2, $char2);
  37. if ($num2 === false) {
  38. $this->errors[] = "Source string contains an invalid character ($char2)";
  39. return;
  40. }
  41. $adj = $this->_applyFudgeFactor($fudgefactor);
  42.  
  43. $factor1 = $factor2 + $adj; // accumulate in $factor1
  44. $num1 = $num2 - round($factor1); // generate offset for $scramble1
  45. $num1 = $this->_checkRange($num1); // check range
  46. $factor2 = $factor1 + $num2; // accumulate in $factor2
  47. if (function_exists('mb_substr')) {
  48. $char1 = mb_substr($this->scramble1, $num1, 1);
  49. } else {
  50. $char1 = substr($this->scramble1, $num1, 1);
  51. }
  52. $target .= $char1;
  53. //echo "char1=$char1, num1=$num1, adj= $adj, factor1= $factor1, num2=$num2, char2=$char2, factor2= $factor2<br />\n";
  54. }
  55. return rtrim($target);
  56. }
  57. function encrypt ($key, $source, $sourcelen = 0){
  58. $this->errors = array();
  59. $fudgefactor = $this->_convertKey($key);
  60. if ($this->errors) return;
  61.  
  62. if (empty($source)) {
  63. $this->errors[] = 'No value has been supplied for encryption';
  64. return;
  65. }
  66. $source = str_pad($source, $sourcelen);
  67. $target = null;
  68. $factor2 = 0;
  69. for ($i = 0; $i < strlen($source); $i++) {
  70. if (function_exists('mb_substr')) {
  71. $char1 = mb_substr($source, $i, 1);
  72. } else {
  73. $char1 = substr($source, $i, 1);
  74. }
  75. $num1 = strpos($this->scramble1, $char1);
  76. if ($num1 === false) {
  77. $this->errors[] = "Source string contains an invalid character ($char1)";
  78. return;
  79. }
  80. $adj = $this->_applyFudgeFactor($fudgefactor);
  81. $factor1 = $factor2 + $adj; // accumulate in $factor1
  82. $num2 = round($factor1) + $num1; // generate offset for $scramble2
  83. $num2 = $this->_checkRange($num2); // check range
  84. $factor2 = $factor1 + $num2; // accumulate in $factor2
  85. if (function_exists('mb_substr')) {
  86. $char2 = mb_substr($this->scramble2, $num2, 1);
  87. } else {
  88. $char2 = substr($this->scramble2, $num2, 1);
  89. }
  90. $target .= $char2;
  91. //echo "char1=$char1, num1=$num1, adj= $adj, factor1= $factor1, num2=$num2, char2=$char2, factor2= $factor2<br />\n";
  92. }
  93. return $target;
  94. }
  95. function getAdjustment (){
  96. return $this->adj;
  97. }
  98. function getModulus (){
  99. return $this->mod;
  100. }
  101. function setAdjustment ($adj){
  102. $this->adj = (float)$adj;
  103. }
  104. function setModulus ($mod){
  105. $this->mod = (int)abs($mod); // must be a positive whole number
  106.  
  107. }
  108. function _applyFudgeFactor (&$fudgefactor){
  109. $fudge = array_shift($fudgefactor); // extract 1st number from array
  110. $fudge = $fudge + $this->adj; // add in adjustment value
  111. $fudgefactor[] = $fudge; // put it back at end of array
  112. if (!empty($this->mod)) { // if modifier has been supplied
  113. if ($fudge % $this->mod == 0) { // if it is divisible by modifier
  114. $fudge = $fudge * -1; // make it negative
  115. }
  116. }
  117.  
  118. return $fudge;
  119.  
  120. }
  121. function _checkRange ($num) {
  122. $num = round($num);
  123. $limit = strlen($this->scramble1);
  124. while ($num >= $limit) {
  125. $num = $num - $limit;
  126. }
  127. while ($num < 0) {
  128. $num = $num + $limit;
  129. }
  130. return $num;
  131. }
  132. function _convertKey ($key){
  133. if (empty($key)) {
  134. $this->errors[] = 'No value has been supplied for the encryption key';
  135. return;
  136. }
  137. $array[] = strlen($key);
  138. $tot = 0;
  139. for ($i = 0; $i < strlen($key); $i++) {
  140. if (function_exists('mb_substr')) {
  141. $char = mb_substr($key, $i, 1);
  142. } else {
  143. $char = substr($key, $i, 1);
  144. }
  145. $num = strpos($this->scramble1, $char);
  146. if ($num === false) {
  147. $this->errors[] = "Key contains an invalid character ($char)";
  148. return;
  149. }
  150. $array[] = $num; // store in output array
  151. $tot = $tot + $num; // accumulate total for later
  152. }
  153.  
  154. $array[] = $tot; // insert total as last entry in array
  155. return $array;
  156. }
  157. }

URL: http://harvest-media.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.