3v4l.org

run code in 300+ PHP versions simultaneously
5ba6cf7aca404fe11369decaaeeb50ccea008955ef4d0181ea7e5a65415b59ec<?php /* * Fill in all four below and click the blue 'eval();' button. * ------------------ */ $unhashedServerSeed = ''; $nonce = -1; $clientSeed = ''; $ticketQuantity = -1; /* ------------------ */ if ($unhashedServerSeed == '' || $nonce < 0 || $clientSeed == '' || $ticketQuantity <= 0) { echo "Fill in details"; return; } define('MAX_HEX_SEGMENTS', 6); define('HEX_SEGMENT_SIZE', 2); define('BASE_FOR_HEX_CONVERSION', 256); define('HASH_TYPE', 'sha256'); function calculateDecimalValue(string $preResult): float { $decimalValue = 0; for ($i = 0; $i < MAX_HEX_SEGMENTS; $i++) { $hexValue = substr($preResult, HEX_SEGMENT_SIZE * $i, HEX_SEGMENT_SIZE); $decimalValue += hexdec($hexValue) / pow(BASE_FOR_HEX_CONVERSION, $i + 1); } return $decimalValue; } function getProvablyFairResult(string $init, string $serverSeed, int $qty): array { $preResult = hash_hmac(HASH_TYPE, $init, $serverSeed); $decimalValue = calculateDecimalValue($preResult); $result = (int) ($decimalValue * $qty) + 1; return [ 'preResult' => $preResult, 'result' => $result, ]; } $unhashedServerSeed = preg_replace( "/\r|\n/", "", $unhashedServerSeed); $clientSeed = preg_replace( "/\r|\n/", "", $clientSeed); $nrand = strlen($clientSeed); $stringToHash = "$clientSeed-$nrand-$nonce"; $result = getProvablyFairResult($stringToHash, $unhashedServerSeed, $ticketQuantity); echo "Result: {$result['result']}";
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
5ba6cf7aca404fe11369decaaeeb50ccea008955ef4d0181ea7e5a65415b59ecFill in details

preferences:
48.43 ms | 402 KiB | 62 Q