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