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