1*c3cc6e05SAndreas Gohr<?php 2*c3cc6e05SAndreas Gohr// phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps 3*c3cc6e05SAndreas Gohr 4*c3cc6e05SAndreas Gohrnamespace dokuwiki; 5*c3cc6e05SAndreas Gohr 6*c3cc6e05SAndreas Gohr/** 7*c3cc6e05SAndreas Gohr * Password Hashing Class 8*c3cc6e05SAndreas Gohr * 9*c3cc6e05SAndreas Gohr * This class implements various mechanisms used to hash passwords 10*c3cc6e05SAndreas Gohr * 11*c3cc6e05SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 12*c3cc6e05SAndreas Gohr * @license LGPL2 13*c3cc6e05SAndreas Gohr */ 14*c3cc6e05SAndreas Gohrclass PassHash { 15*c3cc6e05SAndreas Gohr /** 16*c3cc6e05SAndreas Gohr * Verifies a cleartext password against a crypted hash 17*c3cc6e05SAndreas Gohr * 18*c3cc6e05SAndreas Gohr * The method and salt used for the crypted hash is determined automatically, 19*c3cc6e05SAndreas Gohr * then the clear text password is crypted using the same method. If both hashs 20*c3cc6e05SAndreas Gohr * match true is is returned else false 21*c3cc6e05SAndreas Gohr * 22*c3cc6e05SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 23*c3cc6e05SAndreas Gohr * 24*c3cc6e05SAndreas Gohr * @param string $clear Clear-Text password 25*c3cc6e05SAndreas Gohr * @param string $hash Hash to compare against 26*c3cc6e05SAndreas Gohr * @return bool 27*c3cc6e05SAndreas Gohr */ 28*c3cc6e05SAndreas Gohr public function verify_hash($clear, $hash) { 29*c3cc6e05SAndreas Gohr $method = ''; 30*c3cc6e05SAndreas Gohr $salt = ''; 31*c3cc6e05SAndreas Gohr $magic = ''; 32*c3cc6e05SAndreas Gohr 33*c3cc6e05SAndreas Gohr //determine the used method and salt 34*c3cc6e05SAndreas Gohr $len = strlen($hash); 35*c3cc6e05SAndreas Gohr if(preg_match('/^\$1\$([^\$]{0,8})\$/', $hash, $m)) { 36*c3cc6e05SAndreas Gohr $method = 'smd5'; 37*c3cc6e05SAndreas Gohr $salt = $m[1]; 38*c3cc6e05SAndreas Gohr $magic = '1'; 39*c3cc6e05SAndreas Gohr } elseif(preg_match('/^\$apr1\$([^\$]{0,8})\$/', $hash, $m)) { 40*c3cc6e05SAndreas Gohr $method = 'apr1'; 41*c3cc6e05SAndreas Gohr $salt = $m[1]; 42*c3cc6e05SAndreas Gohr $magic = 'apr1'; 43*c3cc6e05SAndreas Gohr } elseif(preg_match('/^\$P\$(.{31})$/', $hash, $m)) { 44*c3cc6e05SAndreas Gohr $method = 'pmd5'; 45*c3cc6e05SAndreas Gohr $salt = $m[1]; 46*c3cc6e05SAndreas Gohr $magic = 'P'; 47*c3cc6e05SAndreas Gohr } elseif(preg_match('/^\$H\$(.{31})$/', $hash, $m)) { 48*c3cc6e05SAndreas Gohr $method = 'pmd5'; 49*c3cc6e05SAndreas Gohr $salt = $m[1]; 50*c3cc6e05SAndreas Gohr $magic = 'H'; 51*c3cc6e05SAndreas Gohr } elseif(preg_match('/^pbkdf2_(\w+?)\$(\d+)\$(.{12})\$/', $hash, $m)) { 52*c3cc6e05SAndreas Gohr $method = 'djangopbkdf2'; 53*c3cc6e05SAndreas Gohr $magic = array( 54*c3cc6e05SAndreas Gohr 'algo' => $m[1], 55*c3cc6e05SAndreas Gohr 'iter' => $m[2], 56*c3cc6e05SAndreas Gohr ); 57*c3cc6e05SAndreas Gohr $salt = $m[3]; 58*c3cc6e05SAndreas Gohr } elseif(preg_match('/^sha1\$(.{5})\$/', $hash, $m)) { 59*c3cc6e05SAndreas Gohr $method = 'djangosha1'; 60*c3cc6e05SAndreas Gohr $salt = $m[1]; 61*c3cc6e05SAndreas Gohr } elseif(preg_match('/^md5\$(.{5})\$/', $hash, $m)) { 62*c3cc6e05SAndreas Gohr $method = 'djangomd5'; 63*c3cc6e05SAndreas Gohr $salt = $m[1]; 64*c3cc6e05SAndreas Gohr } elseif(preg_match('/^\$2(a|y)\$(.{2})\$/', $hash, $m)) { 65*c3cc6e05SAndreas Gohr $method = 'bcrypt'; 66*c3cc6e05SAndreas Gohr $salt = $hash; 67*c3cc6e05SAndreas Gohr } elseif(substr($hash, 0, 6) == '{SSHA}') { 68*c3cc6e05SAndreas Gohr $method = 'ssha'; 69*c3cc6e05SAndreas Gohr $salt = substr(base64_decode(substr($hash, 6)), 20); 70*c3cc6e05SAndreas Gohr } elseif(substr($hash, 0, 6) == '{SMD5}') { 71*c3cc6e05SAndreas Gohr $method = 'lsmd5'; 72*c3cc6e05SAndreas Gohr $salt = substr(base64_decode(substr($hash, 6)), 16); 73*c3cc6e05SAndreas Gohr } elseif(preg_match('/^:B:(.+?):.{32}$/', $hash, $m)) { 74*c3cc6e05SAndreas Gohr $method = 'mediawiki'; 75*c3cc6e05SAndreas Gohr $salt = $m[1]; 76*c3cc6e05SAndreas Gohr } elseif(preg_match('/^\$6\$(rounds=\d+)?\$?(.+?)\$/', $hash, $m)) { 77*c3cc6e05SAndreas Gohr $method = 'sha512'; 78*c3cc6e05SAndreas Gohr $salt = $m[2]; 79*c3cc6e05SAndreas Gohr $magic = $m[1]; 80*c3cc6e05SAndreas Gohr } elseif($len == 32) { 81*c3cc6e05SAndreas Gohr $method = 'md5'; 82*c3cc6e05SAndreas Gohr } elseif($len == 40) { 83*c3cc6e05SAndreas Gohr $method = 'sha1'; 84*c3cc6e05SAndreas Gohr } elseif($len == 16) { 85*c3cc6e05SAndreas Gohr $method = 'mysql'; 86*c3cc6e05SAndreas Gohr } elseif($len == 41 && $hash[0] == '*') { 87*c3cc6e05SAndreas Gohr $method = 'my411'; 88*c3cc6e05SAndreas Gohr } elseif($len == 34) { 89*c3cc6e05SAndreas Gohr $method = 'kmd5'; 90*c3cc6e05SAndreas Gohr $salt = $hash; 91*c3cc6e05SAndreas Gohr } else { 92*c3cc6e05SAndreas Gohr $method = 'crypt'; 93*c3cc6e05SAndreas Gohr $salt = substr($hash, 0, 2); 94*c3cc6e05SAndreas Gohr } 95*c3cc6e05SAndreas Gohr 96*c3cc6e05SAndreas Gohr //crypt and compare 97*c3cc6e05SAndreas Gohr $call = 'hash_'.$method; 98*c3cc6e05SAndreas Gohr $newhash = $this->$call($clear, $salt, $magic); 99*c3cc6e05SAndreas Gohr if(\hash_equals($newhash, $hash)) { 100*c3cc6e05SAndreas Gohr return true; 101*c3cc6e05SAndreas Gohr } 102*c3cc6e05SAndreas Gohr return false; 103*c3cc6e05SAndreas Gohr } 104*c3cc6e05SAndreas Gohr 105*c3cc6e05SAndreas Gohr /** 106*c3cc6e05SAndreas Gohr * Create a random salt 107*c3cc6e05SAndreas Gohr * 108*c3cc6e05SAndreas Gohr * @param int $len The length of the salt 109*c3cc6e05SAndreas Gohr * @return string 110*c3cc6e05SAndreas Gohr */ 111*c3cc6e05SAndreas Gohr public function gen_salt($len = 32) { 112*c3cc6e05SAndreas Gohr $salt = ''; 113*c3cc6e05SAndreas Gohr $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; 114*c3cc6e05SAndreas Gohr for($i = 0; $i < $len; $i++) { 115*c3cc6e05SAndreas Gohr $salt .= $chars[$this->random(0, 61)]; 116*c3cc6e05SAndreas Gohr } 117*c3cc6e05SAndreas Gohr return $salt; 118*c3cc6e05SAndreas Gohr } 119*c3cc6e05SAndreas Gohr 120*c3cc6e05SAndreas Gohr /** 121*c3cc6e05SAndreas Gohr * Initialize the passed variable with a salt if needed. 122*c3cc6e05SAndreas Gohr * 123*c3cc6e05SAndreas Gohr * If $salt is not null, the value is kept, but the lenght restriction is 124*c3cc6e05SAndreas Gohr * applied (unless, $cut is false). 125*c3cc6e05SAndreas Gohr * 126*c3cc6e05SAndreas Gohr * @param string|null &$salt The salt, pass null if you want one generated 127*c3cc6e05SAndreas Gohr * @param int $len The length of the salt 128*c3cc6e05SAndreas Gohr * @param bool $cut Apply length restriction to existing salt? 129*c3cc6e05SAndreas Gohr */ 130*c3cc6e05SAndreas Gohr public function init_salt(&$salt, $len = 32, $cut = true) { 131*c3cc6e05SAndreas Gohr if(is_null($salt)) { 132*c3cc6e05SAndreas Gohr $salt = $this->gen_salt($len); 133*c3cc6e05SAndreas Gohr $cut = true; // for new hashes we alway apply length restriction 134*c3cc6e05SAndreas Gohr } 135*c3cc6e05SAndreas Gohr if(strlen($salt) > $len && $cut) $salt = substr($salt, 0, $len); 136*c3cc6e05SAndreas Gohr } 137*c3cc6e05SAndreas Gohr 138*c3cc6e05SAndreas Gohr // Password hashing methods follow below 139*c3cc6e05SAndreas Gohr 140*c3cc6e05SAndreas Gohr /** 141*c3cc6e05SAndreas Gohr * Password hashing method 'smd5' 142*c3cc6e05SAndreas Gohr * 143*c3cc6e05SAndreas Gohr * Uses salted MD5 hashs. Salt is 8 bytes long. 144*c3cc6e05SAndreas Gohr * 145*c3cc6e05SAndreas Gohr * The same mechanism is used by Apache's 'apr1' method. This will 146*c3cc6e05SAndreas Gohr * fallback to a implementation in pure PHP if MD5 support is not 147*c3cc6e05SAndreas Gohr * available in crypt() 148*c3cc6e05SAndreas Gohr * 149*c3cc6e05SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 150*c3cc6e05SAndreas Gohr * @author <mikey_nich at hotmail dot com> 151*c3cc6e05SAndreas Gohr * @link http://php.net/manual/en/function.crypt.php#73619 152*c3cc6e05SAndreas Gohr * 153*c3cc6e05SAndreas Gohr * @param string $clear The clear text to hash 154*c3cc6e05SAndreas Gohr * @param string $salt The salt to use, null for random 155*c3cc6e05SAndreas Gohr * @return string Hashed password 156*c3cc6e05SAndreas Gohr */ 157*c3cc6e05SAndreas Gohr public function hash_smd5($clear, $salt = null) { 158*c3cc6e05SAndreas Gohr $this->init_salt($salt, 8); 159*c3cc6e05SAndreas Gohr 160*c3cc6e05SAndreas Gohr if(defined('CRYPT_MD5') && CRYPT_MD5 && $salt !== '') { 161*c3cc6e05SAndreas Gohr return crypt($clear, '$1$'.$salt.'$'); 162*c3cc6e05SAndreas Gohr } else { 163*c3cc6e05SAndreas Gohr // Fall back to PHP-only implementation 164*c3cc6e05SAndreas Gohr return $this->hash_apr1($clear, $salt, '1'); 165*c3cc6e05SAndreas Gohr } 166*c3cc6e05SAndreas Gohr } 167*c3cc6e05SAndreas Gohr 168*c3cc6e05SAndreas Gohr /** 169*c3cc6e05SAndreas Gohr * Password hashing method 'lsmd5' 170*c3cc6e05SAndreas Gohr * 171*c3cc6e05SAndreas Gohr * Uses salted MD5 hashs. Salt is 8 bytes long. 172*c3cc6e05SAndreas Gohr * 173*c3cc6e05SAndreas Gohr * This is the format used by LDAP. 174*c3cc6e05SAndreas Gohr * 175*c3cc6e05SAndreas Gohr * @param string $clear The clear text to hash 176*c3cc6e05SAndreas Gohr * @param string $salt The salt to use, null for random 177*c3cc6e05SAndreas Gohr * @return string Hashed password 178*c3cc6e05SAndreas Gohr */ 179*c3cc6e05SAndreas Gohr public function hash_lsmd5($clear, $salt = null) { 180*c3cc6e05SAndreas Gohr $this->init_salt($salt, 8); 181*c3cc6e05SAndreas Gohr return "{SMD5}".base64_encode(md5($clear.$salt, true).$salt); 182*c3cc6e05SAndreas Gohr } 183*c3cc6e05SAndreas Gohr 184*c3cc6e05SAndreas Gohr /** 185*c3cc6e05SAndreas Gohr * Password hashing method 'apr1' 186*c3cc6e05SAndreas Gohr * 187*c3cc6e05SAndreas Gohr * Uses salted MD5 hashs. Salt is 8 bytes long. 188*c3cc6e05SAndreas Gohr * 189*c3cc6e05SAndreas Gohr * This is basically the same as smd1 above, but as used by Apache. 190*c3cc6e05SAndreas Gohr * 191*c3cc6e05SAndreas Gohr * @author <mikey_nich at hotmail dot com> 192*c3cc6e05SAndreas Gohr * @link http://php.net/manual/en/function.crypt.php#73619 193*c3cc6e05SAndreas Gohr * 194*c3cc6e05SAndreas Gohr * @param string $clear The clear text to hash 195*c3cc6e05SAndreas Gohr * @param string $salt The salt to use, null for random 196*c3cc6e05SAndreas Gohr * @param string $magic The hash identifier (apr1 or 1) 197*c3cc6e05SAndreas Gohr * @return string Hashed password 198*c3cc6e05SAndreas Gohr */ 199*c3cc6e05SAndreas Gohr public function hash_apr1($clear, $salt = null, $magic = 'apr1') { 200*c3cc6e05SAndreas Gohr $this->init_salt($salt, 8); 201*c3cc6e05SAndreas Gohr 202*c3cc6e05SAndreas Gohr $len = strlen($clear); 203*c3cc6e05SAndreas Gohr $text = $clear.'$'.$magic.'$'.$salt; 204*c3cc6e05SAndreas Gohr $bin = pack("H32", md5($clear.$salt.$clear)); 205*c3cc6e05SAndreas Gohr for($i = $len; $i > 0; $i -= 16) { 206*c3cc6e05SAndreas Gohr $text .= substr($bin, 0, min(16, $i)); 207*c3cc6e05SAndreas Gohr } 208*c3cc6e05SAndreas Gohr for($i = $len; $i > 0; $i >>= 1) { 209*c3cc6e05SAndreas Gohr $text .= ($i & 1) ? chr(0) : $clear{0}; 210*c3cc6e05SAndreas Gohr } 211*c3cc6e05SAndreas Gohr $bin = pack("H32", md5($text)); 212*c3cc6e05SAndreas Gohr for($i = 0; $i < 1000; $i++) { 213*c3cc6e05SAndreas Gohr $new = ($i & 1) ? $clear : $bin; 214*c3cc6e05SAndreas Gohr if($i % 3) $new .= $salt; 215*c3cc6e05SAndreas Gohr if($i % 7) $new .= $clear; 216*c3cc6e05SAndreas Gohr $new .= ($i & 1) ? $bin : $clear; 217*c3cc6e05SAndreas Gohr $bin = pack("H32", md5($new)); 218*c3cc6e05SAndreas Gohr } 219*c3cc6e05SAndreas Gohr $tmp = ''; 220*c3cc6e05SAndreas Gohr for($i = 0; $i < 5; $i++) { 221*c3cc6e05SAndreas Gohr $k = $i + 6; 222*c3cc6e05SAndreas Gohr $j = $i + 12; 223*c3cc6e05SAndreas Gohr if($j == 16) $j = 5; 224*c3cc6e05SAndreas Gohr $tmp = $bin[$i].$bin[$k].$bin[$j].$tmp; 225*c3cc6e05SAndreas Gohr } 226*c3cc6e05SAndreas Gohr $tmp = chr(0).chr(0).$bin[11].$tmp; 227*c3cc6e05SAndreas Gohr $tmp = strtr( 228*c3cc6e05SAndreas Gohr strrev(substr(base64_encode($tmp), 2)), 229*c3cc6e05SAndreas Gohr "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", 230*c3cc6e05SAndreas Gohr "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" 231*c3cc6e05SAndreas Gohr ); 232*c3cc6e05SAndreas Gohr return '$'.$magic.'$'.$salt.'$'.$tmp; 233*c3cc6e05SAndreas Gohr } 234*c3cc6e05SAndreas Gohr 235*c3cc6e05SAndreas Gohr /** 236*c3cc6e05SAndreas Gohr * Password hashing method 'md5' 237*c3cc6e05SAndreas Gohr * 238*c3cc6e05SAndreas Gohr * Uses MD5 hashs. 239*c3cc6e05SAndreas Gohr * 240*c3cc6e05SAndreas Gohr * @param string $clear The clear text to hash 241*c3cc6e05SAndreas Gohr * @return string Hashed password 242*c3cc6e05SAndreas Gohr */ 243*c3cc6e05SAndreas Gohr public function hash_md5($clear) { 244*c3cc6e05SAndreas Gohr return md5($clear); 245*c3cc6e05SAndreas Gohr } 246*c3cc6e05SAndreas Gohr 247*c3cc6e05SAndreas Gohr /** 248*c3cc6e05SAndreas Gohr * Password hashing method 'sha1' 249*c3cc6e05SAndreas Gohr * 250*c3cc6e05SAndreas Gohr * Uses SHA1 hashs. 251*c3cc6e05SAndreas Gohr * 252*c3cc6e05SAndreas Gohr * @param string $clear The clear text to hash 253*c3cc6e05SAndreas Gohr * @return string Hashed password 254*c3cc6e05SAndreas Gohr */ 255*c3cc6e05SAndreas Gohr public function hash_sha1($clear) { 256*c3cc6e05SAndreas Gohr return sha1($clear); 257*c3cc6e05SAndreas Gohr } 258*c3cc6e05SAndreas Gohr 259*c3cc6e05SAndreas Gohr /** 260*c3cc6e05SAndreas Gohr * Password hashing method 'ssha' as used by LDAP 261*c3cc6e05SAndreas Gohr * 262*c3cc6e05SAndreas Gohr * Uses salted SHA1 hashs. Salt is 4 bytes long. 263*c3cc6e05SAndreas Gohr * 264*c3cc6e05SAndreas Gohr * @param string $clear The clear text to hash 265*c3cc6e05SAndreas Gohr * @param string $salt The salt to use, null for random 266*c3cc6e05SAndreas Gohr * @return string Hashed password 267*c3cc6e05SAndreas Gohr */ 268*c3cc6e05SAndreas Gohr public function hash_ssha($clear, $salt = null) { 269*c3cc6e05SAndreas Gohr $this->init_salt($salt, 4); 270*c3cc6e05SAndreas Gohr return '{SSHA}'.base64_encode(pack("H*", sha1($clear.$salt)).$salt); 271*c3cc6e05SAndreas Gohr } 272*c3cc6e05SAndreas Gohr 273*c3cc6e05SAndreas Gohr /** 274*c3cc6e05SAndreas Gohr * Password hashing method 'crypt' 275*c3cc6e05SAndreas Gohr * 276*c3cc6e05SAndreas Gohr * Uses salted crypt hashs. Salt is 2 bytes long. 277*c3cc6e05SAndreas Gohr * 278*c3cc6e05SAndreas Gohr * @param string $clear The clear text to hash 279*c3cc6e05SAndreas Gohr * @param string $salt The salt to use, null for random 280*c3cc6e05SAndreas Gohr * @return string Hashed password 281*c3cc6e05SAndreas Gohr */ 282*c3cc6e05SAndreas Gohr public function hash_crypt($clear, $salt = null) { 283*c3cc6e05SAndreas Gohr $this->init_salt($salt, 2); 284*c3cc6e05SAndreas Gohr return crypt($clear, $salt); 285*c3cc6e05SAndreas Gohr } 286*c3cc6e05SAndreas Gohr 287*c3cc6e05SAndreas Gohr /** 288*c3cc6e05SAndreas Gohr * Password hashing method 'mysql' 289*c3cc6e05SAndreas Gohr * 290*c3cc6e05SAndreas Gohr * This method was used by old MySQL systems 291*c3cc6e05SAndreas Gohr * 292*c3cc6e05SAndreas Gohr * @link http://php.net/mysql 293*c3cc6e05SAndreas Gohr * @author <soren at byu dot edu> 294*c3cc6e05SAndreas Gohr * @param string $clear The clear text to hash 295*c3cc6e05SAndreas Gohr * @return string Hashed password 296*c3cc6e05SAndreas Gohr */ 297*c3cc6e05SAndreas Gohr public function hash_mysql($clear) { 298*c3cc6e05SAndreas Gohr $nr = 0x50305735; 299*c3cc6e05SAndreas Gohr $nr2 = 0x12345671; 300*c3cc6e05SAndreas Gohr $add = 7; 301*c3cc6e05SAndreas Gohr $charArr = preg_split("//", $clear); 302*c3cc6e05SAndreas Gohr foreach($charArr as $char) { 303*c3cc6e05SAndreas Gohr if(($char == '') || ($char == ' ') || ($char == '\t')) continue; 304*c3cc6e05SAndreas Gohr $charVal = ord($char); 305*c3cc6e05SAndreas Gohr $nr ^= ((($nr & 63) + $add) * $charVal) + ($nr << 8); 306*c3cc6e05SAndreas Gohr $nr2 += ($nr2 << 8) ^ $nr; 307*c3cc6e05SAndreas Gohr $add += $charVal; 308*c3cc6e05SAndreas Gohr } 309*c3cc6e05SAndreas Gohr return sprintf("%08x%08x", ($nr & 0x7fffffff), ($nr2 & 0x7fffffff)); 310*c3cc6e05SAndreas Gohr } 311*c3cc6e05SAndreas Gohr 312*c3cc6e05SAndreas Gohr /** 313*c3cc6e05SAndreas Gohr * Password hashing method 'my411' 314*c3cc6e05SAndreas Gohr * 315*c3cc6e05SAndreas Gohr * Uses SHA1 hashs. This method is used by MySQL 4.11 and above 316*c3cc6e05SAndreas Gohr * 317*c3cc6e05SAndreas Gohr * @param string $clear The clear text to hash 318*c3cc6e05SAndreas Gohr * @return string Hashed password 319*c3cc6e05SAndreas Gohr */ 320*c3cc6e05SAndreas Gohr public function hash_my411($clear) { 321*c3cc6e05SAndreas Gohr return '*'.strtoupper(sha1(pack("H*", sha1($clear)))); 322*c3cc6e05SAndreas Gohr } 323*c3cc6e05SAndreas Gohr 324*c3cc6e05SAndreas Gohr /** 325*c3cc6e05SAndreas Gohr * Password hashing method 'kmd5' 326*c3cc6e05SAndreas Gohr * 327*c3cc6e05SAndreas Gohr * Uses salted MD5 hashs. 328*c3cc6e05SAndreas Gohr * 329*c3cc6e05SAndreas Gohr * Salt is 2 bytes long, but stored at position 16, so you need to pass at 330*c3cc6e05SAndreas Gohr * least 18 bytes. You can pass the crypted hash as salt. 331*c3cc6e05SAndreas Gohr * 332*c3cc6e05SAndreas Gohr * @param string $clear The clear text to hash 333*c3cc6e05SAndreas Gohr * @param string $salt The salt to use, null for random 334*c3cc6e05SAndreas Gohr * @return string Hashed password 335*c3cc6e05SAndreas Gohr */ 336*c3cc6e05SAndreas Gohr public function hash_kmd5($clear, $salt = null) { 337*c3cc6e05SAndreas Gohr $this->init_salt($salt); 338*c3cc6e05SAndreas Gohr 339*c3cc6e05SAndreas Gohr $key = substr($salt, 16, 2); 340*c3cc6e05SAndreas Gohr $hash1 = strtolower(md5($key.md5($clear))); 341*c3cc6e05SAndreas Gohr $hash2 = substr($hash1, 0, 16).$key.substr($hash1, 16); 342*c3cc6e05SAndreas Gohr return $hash2; 343*c3cc6e05SAndreas Gohr } 344*c3cc6e05SAndreas Gohr 345*c3cc6e05SAndreas Gohr /** 346*c3cc6e05SAndreas Gohr * Password hashing method 'pmd5' 347*c3cc6e05SAndreas Gohr * 348*c3cc6e05SAndreas Gohr * Uses salted MD5 hashs. Salt is 1+8 bytes long, 1st byte is the 349*c3cc6e05SAndreas Gohr * iteration count when given, for null salts $compute is used. 350*c3cc6e05SAndreas Gohr * 351*c3cc6e05SAndreas Gohr * The actual iteration count is the given count squared, maximum is 352*c3cc6e05SAndreas Gohr * 30 (-> 1073741824). If a higher one is given, the function throws 353*c3cc6e05SAndreas Gohr * an exception. 354*c3cc6e05SAndreas Gohr * 355*c3cc6e05SAndreas Gohr * @link http://www.openwall.com/phpass/ 356*c3cc6e05SAndreas Gohr * 357*c3cc6e05SAndreas Gohr * @param string $clear The clear text to hash 358*c3cc6e05SAndreas Gohr * @param string $salt The salt to use, null for random 359*c3cc6e05SAndreas Gohr * @param string $magic The hash identifier (P or H) 360*c3cc6e05SAndreas Gohr * @param int $compute The iteration count for new passwords 361*c3cc6e05SAndreas Gohr * @throws \Exception 362*c3cc6e05SAndreas Gohr * @return string Hashed password 363*c3cc6e05SAndreas Gohr */ 364*c3cc6e05SAndreas Gohr public function hash_pmd5($clear, $salt = null, $magic = 'P', $compute = 8) { 365*c3cc6e05SAndreas Gohr $itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; 366*c3cc6e05SAndreas Gohr if(is_null($salt)) { 367*c3cc6e05SAndreas Gohr $this->init_salt($salt); 368*c3cc6e05SAndreas Gohr $salt = $itoa64[$compute].$salt; // prefix iteration count 369*c3cc6e05SAndreas Gohr } 370*c3cc6e05SAndreas Gohr $iterc = $salt[0]; // pos 0 of salt is iteration count 371*c3cc6e05SAndreas Gohr $iter = strpos($itoa64, $iterc); 372*c3cc6e05SAndreas Gohr 373*c3cc6e05SAndreas Gohr if($iter > 30) { 374*c3cc6e05SAndreas Gohr throw new \Exception("Too high iteration count ($iter) in ". 375*c3cc6e05SAndreas Gohr __CLASS__.'::'.__FUNCTION__); 376*c3cc6e05SAndreas Gohr } 377*c3cc6e05SAndreas Gohr 378*c3cc6e05SAndreas Gohr $iter = 1 << $iter; 379*c3cc6e05SAndreas Gohr $salt = substr($salt, 1, 8); 380*c3cc6e05SAndreas Gohr 381*c3cc6e05SAndreas Gohr // iterate 382*c3cc6e05SAndreas Gohr $hash = md5($salt.$clear, true); 383*c3cc6e05SAndreas Gohr do { 384*c3cc6e05SAndreas Gohr $hash = md5($hash.$clear, true); 385*c3cc6e05SAndreas Gohr } while(--$iter); 386*c3cc6e05SAndreas Gohr 387*c3cc6e05SAndreas Gohr // encode 388*c3cc6e05SAndreas Gohr $output = ''; 389*c3cc6e05SAndreas Gohr $count = 16; 390*c3cc6e05SAndreas Gohr $i = 0; 391*c3cc6e05SAndreas Gohr do { 392*c3cc6e05SAndreas Gohr $value = ord($hash[$i++]); 393*c3cc6e05SAndreas Gohr $output .= $itoa64[$value & 0x3f]; 394*c3cc6e05SAndreas Gohr if($i < $count) 395*c3cc6e05SAndreas Gohr $value |= ord($hash[$i]) << 8; 396*c3cc6e05SAndreas Gohr $output .= $itoa64[($value >> 6) & 0x3f]; 397*c3cc6e05SAndreas Gohr if($i++ >= $count) 398*c3cc6e05SAndreas Gohr break; 399*c3cc6e05SAndreas Gohr if($i < $count) 400*c3cc6e05SAndreas Gohr $value |= ord($hash[$i]) << 16; 401*c3cc6e05SAndreas Gohr $output .= $itoa64[($value >> 12) & 0x3f]; 402*c3cc6e05SAndreas Gohr if($i++ >= $count) 403*c3cc6e05SAndreas Gohr break; 404*c3cc6e05SAndreas Gohr $output .= $itoa64[($value >> 18) & 0x3f]; 405*c3cc6e05SAndreas Gohr } while($i < $count); 406*c3cc6e05SAndreas Gohr 407*c3cc6e05SAndreas Gohr return '$'.$magic.'$'.$iterc.$salt.$output; 408*c3cc6e05SAndreas Gohr } 409*c3cc6e05SAndreas Gohr 410*c3cc6e05SAndreas Gohr /** 411*c3cc6e05SAndreas Gohr * Alias for hash_pmd5 412*c3cc6e05SAndreas Gohr * 413*c3cc6e05SAndreas Gohr * @param string $clear 414*c3cc6e05SAndreas Gohr * @param null|string $salt 415*c3cc6e05SAndreas Gohr * @param string $magic 416*c3cc6e05SAndreas Gohr * @param int $compute 417*c3cc6e05SAndreas Gohr * 418*c3cc6e05SAndreas Gohr * @return string 419*c3cc6e05SAndreas Gohr * @throws \Exception 420*c3cc6e05SAndreas Gohr */ 421*c3cc6e05SAndreas Gohr public function hash_hmd5($clear, $salt = null, $magic = 'H', $compute = 8) { 422*c3cc6e05SAndreas Gohr return $this->hash_pmd5($clear, $salt, $magic, $compute); 423*c3cc6e05SAndreas Gohr } 424*c3cc6e05SAndreas Gohr 425*c3cc6e05SAndreas Gohr /** 426*c3cc6e05SAndreas Gohr * Password hashing method 'djangosha1' 427*c3cc6e05SAndreas Gohr * 428*c3cc6e05SAndreas Gohr * Uses salted SHA1 hashs. Salt is 5 bytes long. 429*c3cc6e05SAndreas Gohr * This is used by the Django Python framework 430*c3cc6e05SAndreas Gohr * 431*c3cc6e05SAndreas Gohr * @link http://docs.djangoproject.com/en/dev/topics/auth/#passwords 432*c3cc6e05SAndreas Gohr * 433*c3cc6e05SAndreas Gohr * @param string $clear The clear text to hash 434*c3cc6e05SAndreas Gohr * @param string $salt The salt to use, null for random 435*c3cc6e05SAndreas Gohr * @return string Hashed password 436*c3cc6e05SAndreas Gohr */ 437*c3cc6e05SAndreas Gohr public function hash_djangosha1($clear, $salt = null) { 438*c3cc6e05SAndreas Gohr $this->init_salt($salt, 5); 439*c3cc6e05SAndreas Gohr return 'sha1$'.$salt.'$'.sha1($salt.$clear); 440*c3cc6e05SAndreas Gohr } 441*c3cc6e05SAndreas Gohr 442*c3cc6e05SAndreas Gohr /** 443*c3cc6e05SAndreas Gohr * Password hashing method 'djangomd5' 444*c3cc6e05SAndreas Gohr * 445*c3cc6e05SAndreas Gohr * Uses salted MD5 hashs. Salt is 5 bytes long. 446*c3cc6e05SAndreas Gohr * This is used by the Django Python framework 447*c3cc6e05SAndreas Gohr * 448*c3cc6e05SAndreas Gohr * @link http://docs.djangoproject.com/en/dev/topics/auth/#passwords 449*c3cc6e05SAndreas Gohr * 450*c3cc6e05SAndreas Gohr * @param string $clear The clear text to hash 451*c3cc6e05SAndreas Gohr * @param string $salt The salt to use, null for random 452*c3cc6e05SAndreas Gohr * @return string Hashed password 453*c3cc6e05SAndreas Gohr */ 454*c3cc6e05SAndreas Gohr public function hash_djangomd5($clear, $salt = null) { 455*c3cc6e05SAndreas Gohr $this->init_salt($salt, 5); 456*c3cc6e05SAndreas Gohr return 'md5$'.$salt.'$'.md5($salt.$clear); 457*c3cc6e05SAndreas Gohr } 458*c3cc6e05SAndreas Gohr 459*c3cc6e05SAndreas Gohr /** 460*c3cc6e05SAndreas Gohr * Password hashing method 'djangopbkdf2' 461*c3cc6e05SAndreas Gohr * 462*c3cc6e05SAndreas Gohr * An algorithm and iteration count should be given in the opts array. 463*c3cc6e05SAndreas Gohr * Defaults to sha256 and 24000 iterations 464*c3cc6e05SAndreas Gohr * 465*c3cc6e05SAndreas Gohr * @param string $clear The clear text to hash 466*c3cc6e05SAndreas Gohr * @param string $salt The salt to use, null for random 467*c3cc6e05SAndreas Gohr * @param array $opts ('algo' => hash algorithm, 'iter' => iterations) 468*c3cc6e05SAndreas Gohr * @return string Hashed password 469*c3cc6e05SAndreas Gohr * @throws \Exception when PHP is missing support for the method/algo 470*c3cc6e05SAndreas Gohr */ 471*c3cc6e05SAndreas Gohr public function hash_djangopbkdf2($clear, $salt=null, $opts=array()) { 472*c3cc6e05SAndreas Gohr $this->init_salt($salt, 12); 473*c3cc6e05SAndreas Gohr if(empty($opts['algo'])) { 474*c3cc6e05SAndreas Gohr $algo = 'sha256'; 475*c3cc6e05SAndreas Gohr } else { 476*c3cc6e05SAndreas Gohr $algo = $opts['algo']; 477*c3cc6e05SAndreas Gohr } 478*c3cc6e05SAndreas Gohr if(empty($opts['iter'])) { 479*c3cc6e05SAndreas Gohr $iter = 24000; 480*c3cc6e05SAndreas Gohr } else { 481*c3cc6e05SAndreas Gohr $iter = (int) $opts['iter']; 482*c3cc6e05SAndreas Gohr } 483*c3cc6e05SAndreas Gohr if(!function_exists('hash_pbkdf2')) { 484*c3cc6e05SAndreas Gohr throw new \Exception('This PHP installation has no PBKDF2 support'); 485*c3cc6e05SAndreas Gohr } 486*c3cc6e05SAndreas Gohr if(!in_array($algo, hash_algos())) { 487*c3cc6e05SAndreas Gohr throw new \Exception("This PHP installation has no $algo support"); 488*c3cc6e05SAndreas Gohr } 489*c3cc6e05SAndreas Gohr 490*c3cc6e05SAndreas Gohr $hash = base64_encode(hash_pbkdf2($algo, $clear, $salt, $iter, 0, true)); 491*c3cc6e05SAndreas Gohr return "pbkdf2_$algo\$$iter\$$salt\$$hash"; 492*c3cc6e05SAndreas Gohr } 493*c3cc6e05SAndreas Gohr 494*c3cc6e05SAndreas Gohr /** 495*c3cc6e05SAndreas Gohr * Alias for djangopbkdf2 defaulting to sha256 as hash algorithm 496*c3cc6e05SAndreas Gohr * 497*c3cc6e05SAndreas Gohr * @param string $clear The clear text to hash 498*c3cc6e05SAndreas Gohr * @param string $salt The salt to use, null for random 499*c3cc6e05SAndreas Gohr * @param array $opts ('iter' => iterations) 500*c3cc6e05SAndreas Gohr * @return string Hashed password 501*c3cc6e05SAndreas Gohr * @throws \Exception when PHP is missing support for the method/algo 502*c3cc6e05SAndreas Gohr */ 503*c3cc6e05SAndreas Gohr public function hash_djangopbkdf2_sha256($clear, $salt=null, $opts=array()) { 504*c3cc6e05SAndreas Gohr $opts['algo'] = 'sha256'; 505*c3cc6e05SAndreas Gohr return $this->hash_djangopbkdf2($clear, $salt, $opts); 506*c3cc6e05SAndreas Gohr } 507*c3cc6e05SAndreas Gohr 508*c3cc6e05SAndreas Gohr /** 509*c3cc6e05SAndreas Gohr * Alias for djangopbkdf2 defaulting to sha1 as hash algorithm 510*c3cc6e05SAndreas Gohr * 511*c3cc6e05SAndreas Gohr * @param string $clear The clear text to hash 512*c3cc6e05SAndreas Gohr * @param string $salt The salt to use, null for random 513*c3cc6e05SAndreas Gohr * @param array $opts ('iter' => iterations) 514*c3cc6e05SAndreas Gohr * @return string Hashed password 515*c3cc6e05SAndreas Gohr * @throws \Exception when PHP is missing support for the method/algo 516*c3cc6e05SAndreas Gohr */ 517*c3cc6e05SAndreas Gohr public function hash_djangopbkdf2_sha1($clear, $salt=null, $opts=array()) { 518*c3cc6e05SAndreas Gohr $opts['algo'] = 'sha1'; 519*c3cc6e05SAndreas Gohr return $this->hash_djangopbkdf2($clear, $salt, $opts); 520*c3cc6e05SAndreas Gohr } 521*c3cc6e05SAndreas Gohr 522*c3cc6e05SAndreas Gohr /** 523*c3cc6e05SAndreas Gohr * Passwordhashing method 'bcrypt' 524*c3cc6e05SAndreas Gohr * 525*c3cc6e05SAndreas Gohr * Uses a modified blowfish algorithm called eksblowfish 526*c3cc6e05SAndreas Gohr * This method works on PHP 5.3+ only and will throw an exception 527*c3cc6e05SAndreas Gohr * if the needed crypt support isn't available 528*c3cc6e05SAndreas Gohr * 529*c3cc6e05SAndreas Gohr * A full hash should be given as salt (starting with $a2$) or this 530*c3cc6e05SAndreas Gohr * will break. When no salt is given, the iteration count can be set 531*c3cc6e05SAndreas Gohr * through the $compute variable. 532*c3cc6e05SAndreas Gohr * 533*c3cc6e05SAndreas Gohr * @param string $clear The clear text to hash 534*c3cc6e05SAndreas Gohr * @param string $salt The salt to use, null for random 535*c3cc6e05SAndreas Gohr * @param int $compute The iteration count (between 4 and 31) 536*c3cc6e05SAndreas Gohr * @throws \Exception 537*c3cc6e05SAndreas Gohr * @return string Hashed password 538*c3cc6e05SAndreas Gohr */ 539*c3cc6e05SAndreas Gohr public function hash_bcrypt($clear, $salt = null, $compute = 10) { 540*c3cc6e05SAndreas Gohr if(!defined('CRYPT_BLOWFISH') || CRYPT_BLOWFISH != 1) { 541*c3cc6e05SAndreas Gohr throw new \Exception('This PHP installation has no bcrypt support'); 542*c3cc6e05SAndreas Gohr } 543*c3cc6e05SAndreas Gohr 544*c3cc6e05SAndreas Gohr if(is_null($salt)) { 545*c3cc6e05SAndreas Gohr if($compute < 4 || $compute > 31) $compute = 8; 546*c3cc6e05SAndreas Gohr $salt = '$2y$'.str_pad($compute, 2, '0', STR_PAD_LEFT).'$'. 547*c3cc6e05SAndreas Gohr $this->gen_salt(22); 548*c3cc6e05SAndreas Gohr } 549*c3cc6e05SAndreas Gohr 550*c3cc6e05SAndreas Gohr return crypt($clear, $salt); 551*c3cc6e05SAndreas Gohr } 552*c3cc6e05SAndreas Gohr 553*c3cc6e05SAndreas Gohr /** 554*c3cc6e05SAndreas Gohr * Password hashing method SHA512 555*c3cc6e05SAndreas Gohr * 556*c3cc6e05SAndreas Gohr * This is only supported on PHP 5.3.2 or higher and will throw an exception if 557*c3cc6e05SAndreas Gohr * the needed crypt support is not available 558*c3cc6e05SAndreas Gohr * 559*c3cc6e05SAndreas Gohr * @param string $clear The clear text to hash 560*c3cc6e05SAndreas Gohr * @param string $salt The salt to use, null for random 561*c3cc6e05SAndreas Gohr * @param string $magic The rounds for sha512 (for example "rounds=3000"), null for default value 562*c3cc6e05SAndreas Gohr * @return string Hashed password 563*c3cc6e05SAndreas Gohr * @throws \Exception 564*c3cc6e05SAndreas Gohr */ 565*c3cc6e05SAndreas Gohr public function hash_sha512($clear, $salt = null, $magic = null) { 566*c3cc6e05SAndreas Gohr if(!defined('CRYPT_SHA512') || CRYPT_SHA512 != 1) { 567*c3cc6e05SAndreas Gohr throw new \Exception('This PHP installation has no SHA512 support'); 568*c3cc6e05SAndreas Gohr } 569*c3cc6e05SAndreas Gohr $this->init_salt($salt, 8, false); 570*c3cc6e05SAndreas Gohr if(empty($magic)) { 571*c3cc6e05SAndreas Gohr return crypt($clear, '$6$'.$salt.'$'); 572*c3cc6e05SAndreas Gohr }else{ 573*c3cc6e05SAndreas Gohr return crypt($clear, '$6$'.$magic.'$'.$salt.'$'); 574*c3cc6e05SAndreas Gohr } 575*c3cc6e05SAndreas Gohr } 576*c3cc6e05SAndreas Gohr 577*c3cc6e05SAndreas Gohr /** 578*c3cc6e05SAndreas Gohr * Password hashing method 'mediawiki' 579*c3cc6e05SAndreas Gohr * 580*c3cc6e05SAndreas Gohr * Uses salted MD5, this is referred to as Method B in MediaWiki docs. Unsalted md5 581*c3cc6e05SAndreas Gohr * method 'A' is not supported. 582*c3cc6e05SAndreas Gohr * 583*c3cc6e05SAndreas Gohr * @link http://www.mediawiki.org/wiki/Manual_talk:User_table#user_password_column 584*c3cc6e05SAndreas Gohr * 585*c3cc6e05SAndreas Gohr * @param string $clear The clear text to hash 586*c3cc6e05SAndreas Gohr * @param string $salt The salt to use, null for random 587*c3cc6e05SAndreas Gohr * @return string Hashed password 588*c3cc6e05SAndreas Gohr */ 589*c3cc6e05SAndreas Gohr public function hash_mediawiki($clear, $salt = null) { 590*c3cc6e05SAndreas Gohr $this->init_salt($salt, 8, false); 591*c3cc6e05SAndreas Gohr return ':B:'.$salt.':'.md5($salt.'-'.md5($clear)); 592*c3cc6e05SAndreas Gohr } 593*c3cc6e05SAndreas Gohr 594*c3cc6e05SAndreas Gohr /** 595*c3cc6e05SAndreas Gohr * Wraps around native hash_hmac() or reimplents it 596*c3cc6e05SAndreas Gohr * 597*c3cc6e05SAndreas Gohr * This is not directly used as password hashing method, and thus isn't callable via the 598*c3cc6e05SAndreas Gohr * verify_hash() method. It should be used to create signatures and might be used in other 599*c3cc6e05SAndreas Gohr * password hashing methods. 600*c3cc6e05SAndreas Gohr * 601*c3cc6e05SAndreas Gohr * @see hash_hmac() 602*c3cc6e05SAndreas Gohr * @author KC Cloyd 603*c3cc6e05SAndreas Gohr * @link http://php.net/manual/en/function.hash-hmac.php#93440 604*c3cc6e05SAndreas Gohr * 605*c3cc6e05SAndreas Gohr * @param string $algo Name of selected hashing algorithm (i.e. "md5", "sha256", "haval160,4", 606*c3cc6e05SAndreas Gohr * etc..) See hash_algos() for a list of supported algorithms. 607*c3cc6e05SAndreas Gohr * @param string $data Message to be hashed. 608*c3cc6e05SAndreas Gohr * @param string $key Shared secret key used for generating the HMAC variant of the message digest. 609*c3cc6e05SAndreas Gohr * @param bool $raw_output When set to TRUE, outputs raw binary data. FALSE outputs lowercase hexits. 610*c3cc6e05SAndreas Gohr * @return string 611*c3cc6e05SAndreas Gohr */ 612*c3cc6e05SAndreas Gohr public static function hmac($algo, $data, $key, $raw_output = false) { 613*c3cc6e05SAndreas Gohr // use native function if available and not in unit test 614*c3cc6e05SAndreas Gohr if(function_exists('hash_hmac') && !defined('SIMPLE_TEST')){ 615*c3cc6e05SAndreas Gohr return hash_hmac($algo, $data, $key, $raw_output); 616*c3cc6e05SAndreas Gohr } 617*c3cc6e05SAndreas Gohr 618*c3cc6e05SAndreas Gohr $algo = strtolower($algo); 619*c3cc6e05SAndreas Gohr $pack = 'H' . strlen($algo('test')); 620*c3cc6e05SAndreas Gohr $size = 64; 621*c3cc6e05SAndreas Gohr $opad = str_repeat(chr(0x5C), $size); 622*c3cc6e05SAndreas Gohr $ipad = str_repeat(chr(0x36), $size); 623*c3cc6e05SAndreas Gohr 624*c3cc6e05SAndreas Gohr if(strlen($key) > $size) { 625*c3cc6e05SAndreas Gohr $key = str_pad(pack($pack, $algo($key)), $size, chr(0x00)); 626*c3cc6e05SAndreas Gohr } else { 627*c3cc6e05SAndreas Gohr $key = str_pad($key, $size, chr(0x00)); 628*c3cc6e05SAndreas Gohr } 629*c3cc6e05SAndreas Gohr 630*c3cc6e05SAndreas Gohr for($i = 0; $i < strlen($key) - 1; $i++) { 631*c3cc6e05SAndreas Gohr $opad[$i] = $opad[$i] ^ $key[$i]; 632*c3cc6e05SAndreas Gohr $ipad[$i] = $ipad[$i] ^ $key[$i]; 633*c3cc6e05SAndreas Gohr } 634*c3cc6e05SAndreas Gohr 635*c3cc6e05SAndreas Gohr $output = $algo($opad . pack($pack, $algo($ipad . $data))); 636*c3cc6e05SAndreas Gohr 637*c3cc6e05SAndreas Gohr return ($raw_output) ? pack($pack, $output) : $output; 638*c3cc6e05SAndreas Gohr } 639*c3cc6e05SAndreas Gohr 640*c3cc6e05SAndreas Gohr /** 641*c3cc6e05SAndreas Gohr * Use a secure random generator 642*c3cc6e05SAndreas Gohr * 643*c3cc6e05SAndreas Gohr * @param int $min 644*c3cc6e05SAndreas Gohr * @param int $max 645*c3cc6e05SAndreas Gohr * @return int 646*c3cc6e05SAndreas Gohr */ 647*c3cc6e05SAndreas Gohr protected function random($min, $max){ 648*c3cc6e05SAndreas Gohr try { 649*c3cc6e05SAndreas Gohr return random_int($min, $max); 650*c3cc6e05SAndreas Gohr } catch (\Exception $e) { 651*c3cc6e05SAndreas Gohr // availability of random source is checked elsewhere in DokuWiki 652*c3cc6e05SAndreas Gohr // we demote this to an unchecked runtime exception here 653*c3cc6e05SAndreas Gohr throw new \RuntimeException($e->getMessage(), $e->getCode(), $e); 654*c3cc6e05SAndreas Gohr } 655*c3cc6e05SAndreas Gohr } 656*c3cc6e05SAndreas Gohr} 657