Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.42 KB | None | 0 0
  1. <?php
  2. Class SecureHash
  3. {
  4. private $rawOutput;
  5.  
  6. /**
  7. @usage - boolRawOutput determines whether the hashing functions return raw binary data
  8. */
  9. public function __construct($boolRawOutput = false)
  10. {
  11. $this -> rawOutput = $boolRawOutput ? true : false;
  12. }
  13.  
  14. /**
  15. @usage - used to return two 10-byte salts, called internally through the hash functions
  16. @return - array containing two 10-byte salts
  17. */
  18. private function getSalts($intDataLength)
  19. {
  20. $dataLength = (int) intval($intDataLength);
  21. if($dataLength > 40)
  22. return false;
  23.  
  24. $seedbox = array(
  25. 0xAA . 0x0D . 0x96 . 0xE9 . 0x1A . 0x4B . 0x7D . 0x90 . 0xAB . 0x5E,
  26. 0xDD . 0x5C . 0x3F . 0x6E . 0x29 . 0x9F . 0x52 . 0xEB . 0xC8 . 0xC2,
  27. 0xDE . 0x57 . 0x4D . 0xD0 . 0xAE . 0xFC . 0x2C . 0x37 . 0x6F . 0xAA,
  28. 0xCB . 0xC5 . 0x8A . 0x9B . 0xB4 . 0xB0 . 0x7D . 0x2D . 0x95 . 0xBD,
  29. 0xC2 . 0xA1 . 0xA5 . 0xDF . 0x8E . 0x12 . 0xF2 . 0xEF . 0x5D . 0x54,
  30. 0x89 . 0x07 . 0xA8 . 0x10 . 0x1F . 0xCB . 0x3E . 0x73 . 0x3B . 0x74,
  31. 0xCB . 0xFC . 0xE4 . 0xC3 . 0x56 . 0xE2 . 0x88 . 0x76 . 0x75 . 0x57,
  32. 0xC7 . 0x5D . 0xB1 . 0x1D . 0x27 . 0x8B . 0x57 . 0x80 . 0x36 . 0xF6,
  33. 0x36 . 0x8E . 0x12 . 0xD8 . 0x35 . 0xF9 . 0x6A . 0xC9 . 0xE5 . 0x2F,
  34. 0xB4 . 0x95 . 0x40 . 0x48 . 0x80 . 0x07 . 0x54 . 0x79 . 0xBB . 0xB7,
  35. 0xC2 . 0x7C . 0x6F . 0x7B . 0xA7 . 0xF2 . 0xE1 . 0x51 . 0xB3 . 0x7F,
  36. 0xCA . 0xE3 . 0x06 . 0x46 . 0x16 . 0x35 . 0xC2 . 0x42 . 0x29 . 0x2A,
  37. 0xF9 . 0xA1 . 0x4E . 0x85 . 0xD6 . 0x6C . 0xCB . 0xDC . 0xAA . 0x12,
  38. 0x1A . 0x26 . 0xC0 . 0x72 . 0x63 . 0x4F . 0x87 . 0x40 . 0xA5 . 0xE6,
  39. 0xB1 . 0x93 . 0x4A . 0x9A . 0xB1 . 0x8D . 0x27 . 0x26 . 0xBD . 0x53,
  40. 0xA7 . 0xFD . 0x47 . 0x37 . 0xC2 . 0xAD . 0xA4 . 0xB5 . 0x88 . 0x65,
  41. 0xF7 . 0xD0 . 0xDC . 0x78 . 0x6F . 0xF0 . 0x6B . 0xBD . 0xA6 . 0x91,
  42. 0x80 . 0x62 . 0x4A . 0xCF . 0xCA . 0xCA . 0x5B . 0x40 . 0x75 . 0x81,
  43. 0x0E . 0x59 . 0x3E . 0x5C . 0xCC . 0xCE . 0xCF . 0xA6 . 0x71 . 0xF7,
  44. 0xD2 . 0x86 . 0x21 . 0xC8 . 0x15 . 0x2D . 0x6D . 0x22 . 0xA4 . 0xD6,
  45. 0x62 . 0xCB . 0x79 . 0xCE . 0xD1 . 0xD8 . 0x4F . 0xBA . 0xBD . 0x5F,
  46. 0x5F . 0x0A . 0x7D . 0x5D . 0x6C . 0x84 . 0x11 . 0xCA . 0x65 . 0x52,
  47. 0xB0 . 0xB3 . 0xCD . 0xFE . 0x2D . 0x21 . 0x28 . 0x6A . 0x06 . 0xF4,
  48. 0x2B . 0x52 . 0x48 . 0x17 . 0x8D . 0xC9 . 0x96 . 0xF5 . 0x3C . 0x0B,
  49. 0x65 . 0xC7 . 0x4A . 0xF5 . 0xD0 . 0x67 . 0x83 . 0xB5 . 0x4F . 0x1C,
  50. 0x5C . 0x8B . 0x1F . 0x72 . 0xB7 . 0xB3 . 0xBE . 0x3B . 0xF5 . 0x18,
  51. 0x4D . 0x2F . 0xCE . 0x39 . 0x8C . 0xE5 . 0x2A . 0xD5 . 0x34 . 0xFD,
  52. 0x23 . 0x57 . 0x28 . 0x3F . 0xD3 . 0x38 . 0x55 . 0xEC . 0xD0 . 0x1B,
  53. 0xFF . 0x1E . 0xD1 . 0x65 . 0x13 . 0x08 . 0x3E . 0x45 . 0x89 . 0xAB,
  54. 0x70 . 0x06 . 0x2B . 0x86 . 0xED . 0x38 . 0xE3 . 0x7B . 0xE1 . 0x00,
  55. 0x54 . 0xD0 . 0x34 . 0xD6 . 0x45 . 0x45 . 0x01 . 0x08 . 0xE6 . 0xF2,
  56. 0xE1 . 0xF8 . 0x4F . 0x96 . 0x08 . 0xDE . 0x3F . 0x2B . 0xAD . 0x2B,
  57. 0x51 . 0x4A . 0x76 . 0x3D . 0xDC . 0x8E . 0xA4 . 0xDF . 0x4B . 0xFE,
  58. 0xEB . 0xCD . 0x1A . 0xF5 . 0x2C . 0xE9 . 0xF6 . 0x23 . 0x7E . 0x1C,
  59. 0xCC . 0xE2 . 0xCE . 0x35 . 0x4A . 0x4A . 0xE4 . 0x89 . 0x79 . 0xDD,
  60. 0x82 . 0xF4 . 0xF9 . 0x52 . 0x6B . 0xD4 . 0x81 . 0x07 . 0x42 . 0xF6,
  61. 0x0A . 0x7F . 0x55 . 0x00 . 0xAF . 0x8E . 0xBC . 0xFB . 0xBB . 0x90,
  62. 0x86 . 0xB2 . 0x28 . 0x42 . 0x4E . 0xB6 . 0x0E . 0xF2 . 0x67 . 0x71,
  63. 0xA8 . 0xE4 . 0x85 . 0xAE . 0x82 . 0x92 . 0x61 . 0xF7 . 0x0F . 0xC0,
  64. 0x3E . 0x11 . 0x48 . 0x60 . 0xDD . 0x7D . 0x7D . 0xDC . 0xE5 . 0x10,
  65. 0xDD . 0xBD . 0x4D . 0x7A . 0x0D . 0xE7 . 0xC0 . 0x1C . 0x36 . 0x85);
  66.  
  67. $presalt = $seedbox[$dataLength];
  68. $postsalt = $seedbox[40 - $dataLength];
  69.  
  70. return array($presalt, $postsalt);
  71. }
  72.  
  73. /**
  74. @usage - used to hash data with two 10-byte salts with support for data lengths from 0-40
  75. @return - 128-bit, 16-byte or 32 character binary data or string depending on the value of SecureHash::rawOutput
  76. */
  77. public function sh_md5($data)
  78. {
  79. if($salt_data = $this -> getSalts(strlen($data)))
  80. {
  81. list($presalt, $postsalt) = $salt_data;
  82. return md5($presalt . $data . $postsalt, $this -> rawOutput);
  83. }
  84.  
  85. return false;
  86. }
  87.  
  88. /**
  89. @usage - used to hash data with two 10-byte salts with support for data lengths from 0-40
  90. @return - 160-bit, 20-byte or 40 character binary data or string depending on the value of SecureHash::rawOutput
  91. */
  92. public function sh_sha1($data)
  93. {
  94. if($salt_data = $this -> getSalts(strlen($data)))
  95. {
  96. list($presalt, $postsalt) = $salt_data;
  97. return sha1($presalt . $data . $postsalt, $this -> rawOutput);
  98. }
  99.  
  100. return false;
  101. }
  102.  
  103. /**
  104. @usage - used to hash data with two 10-byte salts with support for data lengths from 0-40
  105. @return - 224-bit, 28-byte or 56 character binary data or string depending on the value of SecureHash::rawOutput
  106. @requires - usage of this function requires MHASH be installed in your php version, this extension is usually included with mcrypt.
  107. */
  108. public function sh_sha224($data)
  109. {
  110. if($salt_data = $this -> getSalts(strlen($data)))
  111. {
  112. list($presalt, $postsalt) = $salt_data;
  113. return $this -> rawOutput ? mhash(MHASH_SHA224, $presalt . $data . $postsalt) : bin2hex(mhash(MHASH_SHA224, $presalt . $data . $postsalt));
  114. }
  115.  
  116. return false;
  117. }
  118.  
  119. /**
  120. @usage - used to hash data with two 10-byte salts with support for data lengths from 0-40
  121. @return - 256-bit, 32-byte or 64 character binary data or string depending on the value of SecureHash::rawOutput
  122. @requires - usage of this function requires MHASH be installed in your php version, this extension is usually included with mcrypt.
  123. */
  124. public function sh_sha256($data)
  125. {
  126. if($salt_data = $this -> getSalts(strlen($data)))
  127. {
  128. list($presalt, $postsalt) = $salt_data;
  129. return $this -> rawOutput ? mhash(MHASH_SHA256, $presalt . $data . $postsalt) : bin2hex(mhash(MHASH_SHA256, $presalt . $data . $postsalt));
  130. }
  131. }
  132.  
  133. /**
  134. @usage - used to hash data with two 10-byte salts with support for data lengths from 0-40
  135. @return - 384-bit, 48-byte or 96 character binary data or string depending on the value of SecureHash::rawOutput
  136. @requires - usage of this function requires MHASH be installed in your php version, this extension is usually included with mcrypt.
  137. */
  138. public function sh_sha384($data)
  139. {
  140. if($salt_data = $this -> getSalts(strlen($data)))
  141. {
  142. list($presalt, $postsalt) = $salt_data;
  143. return $this -> rawOutput ? mhash(MHASH_SHA384, $presalt . $data . $postsalt) : bin2hex(mhash(MHASH_SHA384, $presalt . $data . $postsalt));
  144. }
  145. }
  146.  
  147. /**
  148. @usage - used to hash data with two 10-byte salts with support for data lengths from 0-40
  149. @return - 512-bit, 64-byte or 128 character binary data or string depending on the value of SecureHash::rawOutput
  150. @requires - usage of this function requires MHASH be installed in your php version, this extension is usually included with mcrypt.
  151. */
  152. public function sh_sha512($data)
  153. {
  154. if($salt_data = $this -> getSalts(strlen($data)))
  155. {
  156. list($presalt, $postsalt) = $salt_data;
  157. return $this -> rawOutput ? mhash(MHASH_SHA512, $presalt . $data . $postsalt) : bin2hex(mhash(MHASH_SHA512, $presalt . $data . $postsalt));
  158. }
  159. }
  160.  
  161. /**
  162. @usage - used to hash data with two 10-byte salts with support for data lengths from 0-40
  163. @return - 160-bit, 20-byte or 40-character binary data or string depending on the value of SecureHash::rawOutput
  164. @requires - usage of this function requires MHASH be installed in your php version, this extension is usually included with mcrypt.
  165. */
  166. public function sh_tiger($data)
  167. {
  168. if($salt_data = $this -> getSalts(strlen($data)))
  169. {
  170. list($presalt, $postsalt) = $salt_data;
  171. return $this -> rawOutput ? mhash(MHASH_TIGER, $presalt . $data . $postsalt) : bin2hex(mhash(MHASH_TIGER, $presalt . $data . $postsalt));
  172. }
  173. }
  174.  
  175. /**
  176. @usage - used to hash data with two 10-byte salts with support for data lengths from 0-40
  177. @return - 512-bit, 64-byte or 128-character binary data or string depending on the value of SecureHash::rawOutput
  178. @requires - usage of this function requires MHASH be installed in your php version, this extension is usually included with mcrypt.
  179. */
  180. public function sh_whirlpool($data)
  181. {
  182. if($salt_data = $this -> getSalts(strlen($data)))
  183. {
  184. list($presalt, $postsalt) = $salt_data;
  185. return $this -> rawOutput ? mhash(MHASH_WHIRLPOOL, $presalt . $data . $postsalt) : bin2hex(mhash(MHASH_WHIRLPOOL, $presalt . $data . $postsalt));
  186. }
  187. }
  188.  
  189. /**
  190. @usage - used to hash data with two 10-byte salts with support for data lengths from 0-40
  191. @return - 256-bit, 32-byte or 64-character binary data or string depending on the value of SecureHash::rawOutput
  192. @requires - usage of this function requires MHASH be installed in your php version, this extension is usually included with mcrypt.
  193. */
  194. public function sh_gost($data)
  195. {
  196. if($salt_data = $this -> getSalts(strlen($data)))
  197. {
  198. list($presalt, $postsalt) = $salt_data;
  199. return $this -> rawOutput ? mhash(MHASH_GOST, $presalt . $data . $postsalt) : bin2hex(mhash(MHASH_GOST, $presalt . $data . $postsalt));
  200. }
  201. }
  202. }
  203.  
  204. $SecureHash= new SecureHash(false);
  205.  
  206. $hash_string = $SecureHash -> sh_gost('teststring');
  207.  
  208. echo $hash_string;
  209. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement