54 lines
999 B
PHP
Executable File
54 lines
999 B
PHP
Executable File
<?php
|
|
|
|
namespace Libs;
|
|
|
|
/**
|
|
* Nonce - DuckBrain
|
|
*
|
|
* Simple class to create and verify nonces.
|
|
*
|
|
* @author KJ
|
|
* @website https://kj2.me
|
|
* @license MIT
|
|
*/
|
|
class Nonce
|
|
{
|
|
/**
|
|
* Returns a nonce code.
|
|
*
|
|
* @param string $passphrase
|
|
*
|
|
* @return string
|
|
*/
|
|
public static function get(string $passphrase = ''): string
|
|
{
|
|
return bin2hex(
|
|
openssl_encrypt(
|
|
time(),
|
|
'aes-128-ecb',
|
|
$passphrase,
|
|
OPENSSL_RAW_DATA
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Verifies a nonce code.
|
|
*
|
|
* @param string $nonce
|
|
* @param string $passphrase
|
|
*
|
|
* @return bool
|
|
*/
|
|
public static function check(string $nonce, string $passphrase = ''): bool
|
|
{
|
|
$decripted = openssl_decrypt(
|
|
hex2bin($nonce),
|
|
'aes-128-ecb',
|
|
$passphrase,
|
|
OPENSSL_RAW_DATA
|
|
);
|
|
return $decripted > (time() - 1800);
|
|
}
|
|
}
|