PageRenderTime 67ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/dokuwiki/html/inc/PassHash.class.php

http://portablenginxdokuwik.codeplex.com
PHP | 453 lines | 216 code | 31 blank | 206 comment | 45 complexity | 779e05014cf31b04872b4dca29321ef1 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. <?php
  2. /**
  3. * Password Hashing Class
  4. *
  5. * This class implements various mechanisms used to hash passwords
  6. *
  7. * @author Andreas Gohr <andi@splitbrain.org>
  8. * @license LGPL2
  9. */
  10. class PassHash {
  11. /**
  12. * Verifies a cleartext password against a crypted hash
  13. *
  14. * The method and salt used for the crypted hash is determined automatically,
  15. * then the clear text password is crypted using the same method. If both hashs
  16. * match true is is returned else false
  17. *
  18. * @author Andreas Gohr <andi@splitbrain.org>
  19. * @param $clear string Clear-Text password
  20. * @param $hash string Hash to compare against
  21. * @return bool
  22. */
  23. function verify_hash($clear, $hash) {
  24. $method = '';
  25. $salt = '';
  26. $magic = '';
  27. //determine the used method and salt
  28. $len = strlen($hash);
  29. if(preg_match('/^\$1\$([^\$]{0,8})\$/', $hash, $m)) {
  30. $method = 'smd5';
  31. $salt = $m[1];
  32. $magic = '1';
  33. } elseif(preg_match('/^\$apr1\$([^\$]{0,8})\$/', $hash, $m)) {
  34. $method = 'apr1';
  35. $salt = $m[1];
  36. $magic = 'apr1';
  37. } elseif(preg_match('/^\$P\$(.{31})$/', $hash, $m)) {
  38. $method = 'pmd5';
  39. $salt = $m[1];
  40. $magic = 'P';
  41. } elseif(preg_match('/^\$H\$(.{31})$/', $hash, $m)) {
  42. $method = 'pmd5';
  43. $salt = $m[1];
  44. $magic = 'H';
  45. } elseif(preg_match('/^sha1\$(.{5})\$/', $hash, $m)) {
  46. $method = 'djangosha1';
  47. $salt = $m[1];
  48. } elseif(preg_match('/^md5\$(.{5})\$/', $hash, $m)) {
  49. $method = 'djangomd5';
  50. $salt = $m[1];
  51. } elseif(preg_match('/^\$2a\$(.{2})\$/', $hash, $m)) {
  52. $method = 'bcrypt';
  53. $salt = $hash;
  54. } elseif(substr($hash, 0, 6) == '{SSHA}') {
  55. $method = 'ssha';
  56. $salt = substr(base64_decode(substr($hash, 6)), 20);
  57. } elseif(substr($hash, 0, 6) == '{SMD5}') {
  58. $method = 'lsmd5';
  59. $salt = substr(base64_decode(substr($hash, 6)), 16);
  60. } elseif($len == 32) {
  61. $method = 'md5';
  62. } elseif($len == 40) {
  63. $method = 'sha1';
  64. } elseif($len == 16) {
  65. $method = 'mysql';
  66. } elseif($len == 41 && $hash[0] == '*') {
  67. $method = 'my411';
  68. } elseif($len == 34) {
  69. $method = 'kmd5';
  70. $salt = $hash;
  71. } else {
  72. $method = 'crypt';
  73. $salt = substr($hash, 0, 2);
  74. }
  75. //crypt and compare
  76. $call = 'hash_'.$method;
  77. if($this->$call($clear, $salt, $magic) === $hash) {
  78. return true;
  79. }
  80. return false;
  81. }
  82. /**
  83. * Create a random salt
  84. *
  85. * @param int $len The length of the salt
  86. * @return string
  87. */
  88. public function gen_salt($len = 32) {
  89. $salt = '';
  90. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  91. for($i = 0; $i < $len; $i++) {
  92. $salt .= $chars[mt_rand(0, 61)];
  93. }
  94. return $salt;
  95. }
  96. /**
  97. * Initialize the passed variable with a salt if needed.
  98. *
  99. * If $salt is not null, the value is kept, but the lenght restriction is
  100. * applied.
  101. *
  102. * @param string &$salt The salt, pass null if you want one generated
  103. * @param int $len The length of the salt
  104. */
  105. public function init_salt(&$salt, $len = 32) {
  106. if(is_null($salt)) $salt = $this->gen_salt($len);
  107. if(strlen($salt) > $len) $salt = substr($salt, 0, $len);
  108. }
  109. // Password hashing methods follow below
  110. /**
  111. * Password hashing method 'smd5'
  112. *
  113. * Uses salted MD5 hashs. Salt is 8 bytes long.
  114. *
  115. * The same mechanism is used by Apache's 'apr1' method. This will
  116. * fallback to a implementation in pure PHP if MD5 support is not
  117. * available in crypt()
  118. *
  119. * @author Andreas Gohr <andi@splitbrain.org>
  120. * @author <mikey_nich at hotmail dot com>
  121. * @link http://de.php.net/manual/en/function.crypt.php#73619
  122. * @param string $clear The clear text to hash
  123. * @param string $salt The salt to use, null for random
  124. * @return string Hashed password
  125. */
  126. public function hash_smd5($clear, $salt = null) {
  127. $this->init_salt($salt, 8);
  128. if(defined('CRYPT_MD5') && CRYPT_MD5 && $salt !== '') {
  129. return crypt($clear, '$1$'.$salt.'$');
  130. } else {
  131. // Fall back to PHP-only implementation
  132. return $this->hash_apr1($clear, $salt, '1');
  133. }
  134. }
  135. /**
  136. * Password hashing method 'lsmd5'
  137. *
  138. * Uses salted MD5 hashs. Salt is 8 bytes long.
  139. *
  140. * This is the format used by LDAP.
  141. *
  142. * @param string $clear The clear text to hash
  143. * @param string $salt The salt to use, null for random
  144. * @return string Hashed password
  145. */
  146. public function hash_lsmd5($clear, $salt = null) {
  147. $this->init_salt($salt, 8);
  148. return "{SMD5}".base64_encode(md5($clear.$salt, true).$salt);
  149. }
  150. /**
  151. * Password hashing method 'apr1'
  152. *
  153. * Uses salted MD5 hashs. Salt is 8 bytes long.
  154. *
  155. * This is basically the same as smd1 above, but as used by Apache.
  156. *
  157. * @author <mikey_nich at hotmail dot com>
  158. * @link http://de.php.net/manual/en/function.crypt.php#73619
  159. * @param string $clear The clear text to hash
  160. * @param string $salt The salt to use, null for random
  161. * @param string $magic The hash identifier (apr1 or 1)
  162. * @return string Hashed password
  163. */
  164. public function hash_apr1($clear, $salt = null, $magic = 'apr1') {
  165. $this->init_salt($salt, 8);
  166. $len = strlen($clear);
  167. $text = $clear.'$'.$magic.'$'.$salt;
  168. $bin = pack("H32", md5($clear.$salt.$clear));
  169. for($i = $len; $i > 0; $i -= 16) {
  170. $text .= substr($bin, 0, min(16, $i));
  171. }
  172. for($i = $len; $i > 0; $i >>= 1) {
  173. $text .= ($i & 1) ? chr(0) : $clear{0};
  174. }
  175. $bin = pack("H32", md5($text));
  176. for($i = 0; $i < 1000; $i++) {
  177. $new = ($i & 1) ? $clear : $bin;
  178. if($i % 3) $new .= $salt;
  179. if($i % 7) $new .= $clear;
  180. $new .= ($i & 1) ? $bin : $clear;
  181. $bin = pack("H32", md5($new));
  182. }
  183. $tmp = '';
  184. for($i = 0; $i < 5; $i++) {
  185. $k = $i + 6;
  186. $j = $i + 12;
  187. if($j == 16) $j = 5;
  188. $tmp = $bin[$i].$bin[$k].$bin[$j].$tmp;
  189. }
  190. $tmp = chr(0).chr(0).$bin[11].$tmp;
  191. $tmp = strtr(
  192. strrev(substr(base64_encode($tmp), 2)),
  193. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
  194. "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  195. );
  196. return '$'.$magic.'$'.$salt.'$'.$tmp;
  197. }
  198. /**
  199. * Password hashing method 'md5'
  200. *
  201. * Uses MD5 hashs.
  202. *
  203. * @param string $clear The clear text to hash
  204. * @return string Hashed password
  205. */
  206. public function hash_md5($clear) {
  207. return md5($clear);
  208. }
  209. /**
  210. * Password hashing method 'sha1'
  211. *
  212. * Uses SHA1 hashs.
  213. *
  214. * @param string $clear The clear text to hash
  215. * @return string Hashed password
  216. */
  217. public function hash_sha1($clear) {
  218. return sha1($clear);
  219. }
  220. /**
  221. * Password hashing method 'ssha' as used by LDAP
  222. *
  223. * Uses salted SHA1 hashs. Salt is 4 bytes long.
  224. *
  225. * @param string $clear The clear text to hash
  226. * @param string $salt The salt to use, null for random
  227. * @return string Hashed password
  228. */
  229. public function hash_ssha($clear, $salt = null) {
  230. $this->init_salt($salt, 4);
  231. return '{SSHA}'.base64_encode(pack("H*", sha1($clear.$salt)).$salt);
  232. }
  233. /**
  234. * Password hashing method 'crypt'
  235. *
  236. * Uses salted crypt hashs. Salt is 2 bytes long.
  237. *
  238. * @param string $clear The clear text to hash
  239. * @param string $salt The salt to use, null for random
  240. * @return string Hashed password
  241. */
  242. public function hash_crypt($clear, $salt = null) {
  243. $this->init_salt($salt, 2);
  244. return crypt($clear, $salt);
  245. }
  246. /**
  247. * Password hashing method 'mysql'
  248. *
  249. * This method was used by old MySQL systems
  250. *
  251. * @link http://www.php.net/mysql
  252. * @author <soren at byu dot edu>
  253. * @param string $clear The clear text to hash
  254. * @return string Hashed password
  255. */
  256. public function hash_mysql($clear) {
  257. $nr = 0x50305735;
  258. $nr2 = 0x12345671;
  259. $add = 7;
  260. $charArr = preg_split("//", $clear);
  261. foreach($charArr as $char) {
  262. if(($char == '') || ($char == ' ') || ($char == '\t')) continue;
  263. $charVal = ord($char);
  264. $nr ^= ((($nr & 63) + $add) * $charVal) + ($nr << 8);
  265. $nr2 += ($nr2 << 8) ^ $nr;
  266. $add += $charVal;
  267. }
  268. return sprintf("%08x%08x", ($nr & 0x7fffffff), ($nr2 & 0x7fffffff));
  269. }
  270. /**
  271. * Password hashing method 'my411'
  272. *
  273. * Uses SHA1 hashs. This method is used by MySQL 4.11 and above
  274. *
  275. * @param string $clear The clear text to hash
  276. * @return string Hashed password
  277. */
  278. public function hash_my411($clear) {
  279. return '*'.sha1(pack("H*", sha1($clear)));
  280. }
  281. /**
  282. * Password hashing method 'kmd5'
  283. *
  284. * Uses salted MD5 hashs.
  285. *
  286. * Salt is 2 bytes long, but stored at position 16, so you need to pass at
  287. * least 18 bytes. You can pass the crypted hash as salt.
  288. *
  289. * @param string $clear The clear text to hash
  290. * @param string $salt The salt to use, null for random
  291. * @return string Hashed password
  292. */
  293. public function hash_kmd5($clear, $salt = null) {
  294. $this->init_salt($salt);
  295. $key = substr($salt, 16, 2);
  296. $hash1 = strtolower(md5($key.md5($clear)));
  297. $hash2 = substr($hash1, 0, 16).$key.substr($hash1, 16);
  298. return $hash2;
  299. }
  300. /**
  301. * Password hashing method 'pmd5'
  302. *
  303. * Uses salted MD5 hashs. Salt is 1+8 bytes long, 1st byte is the
  304. * iteration count when given, for null salts $compute is used.
  305. *
  306. * The actual iteration count is the given count squared, maximum is
  307. * 30 (-> 1073741824). If a higher one is given, the function throws
  308. * an exception.
  309. *
  310. * @link http://www.openwall.com/phpass/
  311. * @param string $clear The clear text to hash
  312. * @param string $salt The salt to use, null for random
  313. * @param string $magic The hash identifier (P or H)
  314. * @param int $compute The iteration count for new passwords
  315. * @throws Exception
  316. * @return string Hashed password
  317. */
  318. public function hash_pmd5($clear, $salt = null, $magic = 'P', $compute = 8) {
  319. $itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  320. if(is_null($salt)) {
  321. $this->init_salt($salt);
  322. $salt = $itoa64[$compute].$salt; // prefix iteration count
  323. }
  324. $iterc = $salt[0]; // pos 0 of salt is iteration count
  325. $iter = strpos($itoa64, $iterc);
  326. if($iter > 30) {
  327. throw new Exception("Too high iteration count ($iter) in ".
  328. __CLASS__.'::'.__FUNCTION__);
  329. }
  330. $iter = 1 << $iter;
  331. $salt = substr($salt, 1, 8);
  332. // iterate
  333. $hash = md5($salt.$clear, true);
  334. do {
  335. $hash = md5($hash.$clear, true);
  336. } while(--$iter);
  337. // encode
  338. $output = '';
  339. $count = 16;
  340. $i = 0;
  341. do {
  342. $value = ord($hash[$i++]);
  343. $output .= $itoa64[$value & 0x3f];
  344. if($i < $count)
  345. $value |= ord($hash[$i]) << 8;
  346. $output .= $itoa64[($value >> 6) & 0x3f];
  347. if($i++ >= $count)
  348. break;
  349. if($i < $count)
  350. $value |= ord($hash[$i]) << 16;
  351. $output .= $itoa64[($value >> 12) & 0x3f];
  352. if($i++ >= $count)
  353. break;
  354. $output .= $itoa64[($value >> 18) & 0x3f];
  355. } while($i < $count);
  356. return '$'.$magic.'$'.$iterc.$salt.$output;
  357. }
  358. /**
  359. * Alias for hash_pmd5
  360. */
  361. public function hash_hmd5($clear, $salt = null, $magic = 'H', $compute = 8) {
  362. return $this->hash_pmd5($clear, $salt, $magic, $compute);
  363. }
  364. /**
  365. * Password hashing method 'djangosha1'
  366. *
  367. * Uses salted SHA1 hashs. Salt is 5 bytes long.
  368. * This is used by the Django Python framework
  369. *
  370. * @link http://docs.djangoproject.com/en/dev/topics/auth/#passwords
  371. * @param string $clear The clear text to hash
  372. * @param string $salt The salt to use, null for random
  373. * @return string Hashed password
  374. */
  375. public function hash_djangosha1($clear, $salt = null) {
  376. $this->init_salt($salt, 5);
  377. return 'sha1$'.$salt.'$'.sha1($salt.$clear);
  378. }
  379. /**
  380. * Password hashing method 'djangomd5'
  381. *
  382. * Uses salted MD5 hashs. Salt is 5 bytes long.
  383. * This is used by the Django Python framework
  384. *
  385. * @link http://docs.djangoproject.com/en/dev/topics/auth/#passwords
  386. * @param string $clear The clear text to hash
  387. * @param string $salt The salt to use, null for random
  388. * @return string Hashed password
  389. */
  390. public function hash_djangomd5($clear, $salt = null) {
  391. $this->init_salt($salt, 5);
  392. return 'md5$'.$salt.'$'.md5($salt.$clear);
  393. }
  394. /**
  395. * Passwordhashing method 'bcrypt'
  396. *
  397. * Uses a modified blowfish algorithm called eksblowfish
  398. * This method works on PHP 5.3+ only and will throw an exception
  399. * if the needed crypt support isn't available
  400. *
  401. * A full hash should be given as salt (starting with $a2$) or this
  402. * will break. When no salt is given, the iteration count can be set
  403. * through the $compute variable.
  404. *
  405. * @param string $clear The clear text to hash
  406. * @param string $salt The salt to use, null for random
  407. * @param int $compute The iteration count (between 4 and 31)
  408. * @throws Exception
  409. * @return string Hashed password
  410. */
  411. public function hash_bcrypt($clear, $salt = null, $compute = 8) {
  412. if(!defined('CRYPT_BLOWFISH') || CRYPT_BLOWFISH != 1) {
  413. throw new Exception('This PHP installation has no bcrypt support');
  414. }
  415. if(is_null($salt)) {
  416. if($compute < 4 || $compute > 31) $compute = 8;
  417. $salt = '$2a$'.str_pad($compute, 2, '0', STR_PAD_LEFT).'$'.
  418. $this->gen_salt(22);
  419. }
  420. return crypt($clear, $salt);
  421. }
  422. }