-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcompat_functions.php
More file actions
105 lines (90 loc) · 2.76 KB
/
compat_functions.php
File metadata and controls
105 lines (90 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
* Compatibility Functions
* Provides backward compatibility and polyfills for missing functions
*/
// Hash function compatibility
if (!function_exists('hash_pbkdf2')) {
function hash_pbkdf2($algo, $password, $salt, $iterations, $length = 0, $raw_output = false) {
// Basic PBKDF2 implementation
$hash_length = strlen(hash($algo, '', true));
$block_count = ceil($length / $hash_length);
$output = '';
for ($i = 1; $i <= $block_count; $i++) {
$last = $salt . pack('N', $i);
$last = $xorsum = hash_hmac($algo, $last, $password, true);
for ($j = 1; $j < $iterations; $j++) {
$xorsum ^= ($last = hash_hmac($algo, $last, $password, true));
}
$output .= $xorsum;
}
if ($length) {
$output = substr($output, 0, $length);
}
return $raw_output ? $output : bin2hex($output);
}
}
// Keccak256 compatibility (for Ethereum)
if (!function_exists('keccak256')) {
function keccak256($data) {
// Use optimized infosave2007/keccak library for proper Keccak-256
return \infosave2007\Keccak::hash($data, 256);
}
}
// Secp256k1 compatibility check
if (!function_exists('secp256k1_context_create')) {
function secp256k1_context_create($flags) {
return null; // Fallback when extension not available
}
}
// JSON compatibility
if (!function_exists('json_validate')) {
function json_validate($json) {
json_decode($json);
return json_last_error() === JSON_ERROR_NONE;
}
}
// String functions
if (!function_exists('str_starts_with')) {
function str_starts_with($haystack, $needle) {
return strpos($haystack, $needle) === 0;
}
}
if (!function_exists('str_ends_with')) {
function str_ends_with($haystack, $needle) {
return substr($haystack, -strlen($needle)) === $needle;
}
}
if (!function_exists('str_contains')) {
function str_contains($haystack, $needle) {
return strpos($haystack, $needle) !== false;
}
}
// Array functions
if (!function_exists('array_is_list')) {
function array_is_list($array) {
return array_keys($array) === range(0, count($array) - 1);
}
}
// Random functions
if (!function_exists('random_int')) {
function random_int($min, $max) {
return mt_rand($min, $max);
}
}
if (!function_exists('random_bytes')) {
function random_bytes($length) {
$bytes = '';
for ($i = 0; $i < $length; $i++) {
$bytes .= chr(mt_rand(0, 255));
}
return $bytes;
}
}
// Define constants
if (!defined('OPENSSL_RAW_DATA')) {
define('OPENSSL_RAW_DATA', 1);
}
if (!defined('OPENSSL_ZERO_PADDING')) {
define('OPENSSL_ZERO_PADDING', 2);
}