first commit.
This commit is contained in:
Executable
+53
@@ -0,0 +1,53 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user