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