Subversion Repositories php_clientchallenge

Rev

Rev 6 | Rev 8 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6 Rev 7
Line 21... Line 21...
21
 
21
 
22
class ClientChallenge {
22
class ClientChallenge {
23
 
23
 
24
        const OPEN_TRANS_DIR = __DIR__.'/cache';
24
        const OPEN_TRANS_DIR = __DIR__.'/cache';
25
 
25
 
26
        private static function sha3_512($message, $raw_output=false) {
26
        private static function tryDownloadPhpSha3() {
27
                if (version_compare(PHP_VERSION, '7.1.0') >= 0) {
-
 
28
                        return hash('sha3-512', $message, $raw_output);
-
 
29
                } else {
-
 
30
                        // Download file if required (usually composer should do it)
27
                // Download file if required (usually composer should do it)
31
                        if (file_exists(__DIR__.'/Sha3.php')) include_once __DIR__.'/Sha3.php';
28
                if (file_exists(__DIR__.'/Sha3.php')) include_once __DIR__.'/Sha3.php';
32
                        if (!class_exists('\bb\Sha3\Sha3')) {
29
                if (!class_exists('\bb\Sha3\Sha3')) {
33
                                $sha3_lib = file_get_contents('https://raw.githubusercontent.com/danielmarschall/php-sha3/master/src/Sha3.php');
30
                        $sha3_lib = file_get_contents('https://raw.githubusercontent.com/danielmarschall/php-sha3/master/src/Sha3.php');
34
                                if (file_put_contents(__DIR__.'/Sha3.php', $sha3_lib)) {
31
                        if (@file_put_contents(__DIR__.'/Sha3.php', $sha3_lib)) {
35
                                        include_once __DIR__.'/Sha3.php';
32
                                include_once __DIR__.'/Sha3.php';
36
                                } else {
33
                        } else {
37
                                        eval('?>'.$sha3_lib);
34
                                eval('?>'.$sha3_lib);
38
                                }
35
                        }
39
                        }
36
                }
-
 
37
        }
-
 
38
 
-
 
39
        private static function sha3_512($message, $raw_output=false) {
-
 
40
                if (version_compare(PHP_VERSION, '7.1.0') >= 0) {
-
 
41
                        return hash('sha3-512', $message, $raw_output);
-
 
42
                } else {
-
 
43
                        self::tryDownloadPhpSha3();
40
                        return \bb\Sha3\Sha3::hash($message, 512, $raw_output);
44
                        return \bb\Sha3\Sha3::hash($message, 512, $raw_output);
41
                }
45
                }
42
        }
46
        }
43
 
47
 
44
        private static function sha3_512_hmac($message, $key, $raw_output=false) {
48
        private static function sha3_512_hmac($message, $key, $raw_output=false) {
45
                // RFC 2104 HMAC
49
                // RFC 2104 HMAC
46
                if (version_compare(PHP_VERSION, '7.1.0') >= 0) {
50
                if (version_compare(PHP_VERSION, '7.1.0') >= 0) {
47
                        return hash_hmac('sha3-512', $message, $key, $raw_output);
51
                        return hash_hmac('sha3-512', $message, $key, $raw_output);
48
                } else {
52
                } else {
49
                        $blocksize = 576; // block size of sha-512!
-
 
50
 
-
 
51
                        if (strlen($key) > ($blocksize/8)) {
-
 
52
                                $k_ = self::sha3_512($key,true);
53
                        self::tryDownloadPhpSha3();
53
                        } else {
-
 
54
                                $k_ = $key;
-
 
55
                        }
-
 
56
 
-
 
57
                        $k_opad = str_repeat(chr(0x5C),($blocksize/8));
-
 
58
                        $k_ipad = str_repeat(chr(0x36),($blocksize/8));
-
 
59
                        for ($i=0; $i<strlen($k_); $i++) {
-
 
60
                                $k_opad[$i] = $k_opad[$i] ^ $k_[$i];
-
 
61
                                $k_ipad[$i] = $k_ipad[$i] ^ $k_[$i];
-
 
62
                        }
-
 
63
 
-
 
64
                        return self::sha3_512($k_opad . self::sha3_512($k_ipad . $message, true));
54
                        return \bb\Sha3\Sha3::hash_hmac($message, $key, 512, $raw_output);
65
                }
55
                }
66
        }
56
        }
67
 
57
 
68
        private static function getOpenTransFileName($ip_target, $random) {
58
        private static function getOpenTransFileName($ip_target, $random) {
69
                // Delete challenges which were never completed
59
                // Delete challenges which were never completed