Salt Pepper Encrypter PHP


SUBMITTED BY: Guest

DATE: July 23, 2014, 12:10 p.m.

FORMAT: Text only

SIZE: 5.7 kB

HITS: 721

  1. <?php
  2. ////////////////////////////////////////
  3. // Salt & Pepper Encrypter v1.0
  4. // (C) 2005 Nathan Bolender
  5. // www.nathanbolender.com
  6. ////////////////////////////////////////
  7. ////////////////////////////////////////
  8. // Feel free to use as you wish, but do
  9. // not remove this copyright notice.
  10. ////////////////////////////////////////
  11. // Redistribution prohibited! May only
  12. // be distributed through
  13. // www.nathanbolender.com
  14. // Full license at:
  15. // http://creativecommons.org/licenses/by-nc-nd/2.0/
  16. ////////////////////////////////////////
  17. ////////////////////////////
  18. // Configuration
  19. ////////////////////////////
  20. // Salt Key
  21. // Set this to anything you wish
  22. // but it must be specific to your
  23. // website and should never be
  24. // revealed to the public
  25. $saltkey = '0987274882';
  26. // Note that if you change this key all of your stored passwords
  27. // will STOP WORKING! This value must be set correctly for pepper() to function correctly
  28. // If you have some experience you can set a different key for each password
  29. // But you must be able to retrieve that key to check the password !
  30. ////////////////////////////
  31. // That's all!
  32. // Now here is some usage instructions:
  33. //
  34. // To get a hash to put into your database (encrypted password)
  35. // include this file and use this function:
  36. // salt('mypassword')
  37. // You can also set a static position and key hash like this:
  38. // salt('mypassword', 15, 'n')
  39. // Options for this is:
  40. // Position must be between 10 and 38
  41. // hash types are 'n' or 'b' where n is sha1 and b is md5
  42. //
  43. // To check a string against a hash from the database:
  44. // pepper('mypass', '8fe5ccb19ba61c4c0873ddc')
  45. // This will return TRUE or FALSE, letting you do the action you
  46. // wish depending on the result.
  47. //
  48. // Both of these functions also have a debug function which works like this:
  49. // salt('mypass', 'a', 'a', 1) (note that a value of 'a' is the same as no value at all in this case
  50. // pepper('mypass', '8fe5ccb19ba61c4c0873ddc', 1)
  51. //
  52. // This will echo the value of all of the variables set.
  53. /////////////////////////////////////////////////////////////////////////////////
  54. /////////////////////////////////////////////////////////////////////////////////
  55. /////////////////////////////////////////////////////////////////////////////////
  56. /////////////////////////////////////////////////////////////////////////////////
  57. ///////////////////////// DO NOT EDIT BELOW THIS BLOCK! /////////////////////////
  58. /////////////////////////////////////////////////////////////////////////////////
  59. /////////////////////////////////////////////////////////////////////////////////
  60. /////////////////////////////////////////////////////////////////////////////////
  61. /////////////////////////////////////////////////////////////////////////////////
  62. //////////////////////////////////
  63. // You should not be down here!
  64. //////////////////////////////////
  65. function salt($string, $pos = 'a', $stype = 'a', $debug = 0) {
  66. global $saltkey;
  67. $stringA = sha1($string);
  68. if ($pos == 'a'): $pos = rand(10, 38);
  69. endif;
  70. if ((rand(1, 3) == 1) || ($stype == 'b')) {
  71. $salt = md5($saltkey);
  72. $stype = 'b';
  73. $slen = 32;
  74. } else {
  75. $salt = sha1($saltkey);
  76. $stype = 'n';
  77. $slen = 40;
  78. }
  79. $afterstr = substr($stringA, $pos);
  80. $startbeginning = -(strlen($afterstr));
  81. $beforestr = substr($stringA, 0, $startbeginning);
  82. $salted = $beforestr . $salt . $afterstr . $stype . $pos;
  83. if ($debug == 1) {
  84. echo '<br>$saltkey = '.$saltkey;
  85. echo '<br>$stringA = '.$stringA;
  86. echo '<br>$pos = '.$pos;
  87. echo '<br>$salt = '.$salt.'<br>$stype = '.$stype.'<br>$slen = '.$slen;
  88. echo '<br>$afterstr = '.$afterstr;
  89. echo '<br>$startbeginning = '.$startbeginning;
  90. echo '<br>$beforestr = '.$beforestr;
  91. echo '<br><br>$salted = '.$salted;
  92. }
  93. return $salted;
  94. }
  95. function pepper($str, $dbhash, $debug = 0) { // str = string to be checked against DBHASH
  96. global $saltkey;
  97. // Find the original sha1 hash and check it with the new one
  98. $hashA = sha1($str); // new hash to be checked
  99. $pos = substr($dbhash, -2);
  100. $stype = substr($dbhash, -3, 1); // n or b
  101. if ($stype == 'n') {
  102. $slen = 40;
  103. } else {
  104. $slen = 32;
  105. }
  106. $beforesalt = substr($dbhash, 0, $pos);
  107. $aftersaltA = substr($dbhash, ($pos + $slen));
  108. $aftersalt = substr($aftersaltA, 0, -3);
  109. $saltA = substr($dbhash, $pos, ((-strlen($aftersalt)) - 3));
  110. if ($stype == 'n') {
  111. $salt = sha1($saltkey);
  112. } else {
  113. $salt = md5($saltkey);
  114. }
  115. $unsalted = $beforesalt . $aftersalt;
  116. if ($debug == 1) {
  117. echo '<br><br>$saltkey = '.$saltkey;
  118. echo '<br>$str = '.$str;
  119. echo '<br>$dbhash = '.$dbhash;
  120. echo '<br>$hashA = '.$hashA;
  121. echo '<br>$pos = '.$pos;
  122. echo '<br>$stype = '.$stype;
  123. echo '<br>$slen = '.$slen;
  124. echo '<br>$beforesalt = '.$beforesalt;
  125. echo '<br>$aftersaltA = '.$aftersaltA;
  126. echo '<br>$aftersalt = '.$aftersalt;
  127. echo '<br>$saltA = '.$saltA;
  128. echo '<br>$salt = '.$salt;
  129. echo '<br>$unsalted = '.$unsalted.'<br>if = ';
  130. }
  131. if (($hashA == $unsalted) && ($salt == $saltA)) {
  132. if ($debug == 1): echo 'true'; endif;
  133. return true;
  134. } else {
  135. if ($debug == 1): echo 'false'; endif;
  136. return false;
  137. }
  138. }
  139. ?>

comments powered by Disqus