lucapuddu / php-provably-fair
PhpProvablyFair 是一个生成和验证公平游戏的库。
3.1.1
2022-07-06 12:05 UTC
Requires
- php: ^7.2|^8.0
- ext-bcmath: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.15
- phpunit/phpunit: ^8
- squizlabs/php_codesniffer: ^3.5
README
此库提供生成和验证公平游戏的函数。
安装
通过 Composer
composer require lucapuddu/php-provably-fair
算法
此示例中使用的参数值
$algorithm = 'sha256'; $serverSeed = 'server seed'; $clientSeed = 'client seed'; $nonce = '1'; $min = 15; $max = 96;
步骤
- 使用 hash_hmac 计算一个可变长度的十六进制字符串(取决于所使用的算法)。函数使用
$serverSeed
作为密钥,将$clientSeed
和$nonce
作为数据,通过一个 连字符 连接
$output1 = hash_hmac($algorithm, "{$clientSeed}-{$nonce}", $serverSeed); echo $output1; // 78ed9330f00055f15765cb141088f316d507204a745ad4800fd719fcbfca071a
- 将
$output1
缩放到从0
到1
的数字,通过将其转换为整数然后除以可能的最大结果(等于16 ^ $output1 的长度
)
$output2 = hexdec($output1) / (16 ** strlen($output1)); echo $output2; // 0.47237510628475
$output2
按照相应的min
和max
缩放
$result = $min + $output2 * ($max - $min); echo $result; // 53.26238360906475
- 返回 结果。
使用方法
创建一个 ProvablyFair
对象
$algorithm = 'sha256';
$serverSeed = 'server seed';
$clientSeed = 'client seed';
$nonce = 'nonce';
$min = 23.75;
$max = 44;
$generator = Builder::make()
->algorithm($algorithm)
->serverSeed($serverSeed)
->clientSeed($clientSeed)
->nonce($nonce)
->range($min, $max)
->build();
默认值
$algorithm = 'sha512/256';
$min = 0;
$max = 100;
在此处获取支持的算法完整列表 这里。
掷一个数字
... $output = $generator->roll(); echo $output; //38.325308655221 // Change ProvablyFair state $generator->setNonce('new nonce'); // Roll a new number $output = $generator->roll(); echo $output; //23.752100169784
验证掷骰子结果
...
$generator->verify(38.325308655221); // true
安全性
如果您发现任何与安全性相关的问题,请使用 github 上的问题跟踪器或通过 info__at__lucapuddu.com 邮件联系我
作者
许可证
MIT。有关更多信息,请参阅 许可证文件。