I have this function:
public function getCode($secret, $timeSlice = null)
{
if ($timeSlice === null) {
$timeSlice = floor(time() / 30);
}
$secretkey = $this->_base32Decode($secret);
// Pack time into binary string
$time = chr(0).chr(0).chr(0).chr(0).pack('N*', $timeSlice);
// Hash it with users secret key
$hm = hash_hmac('SHA1', $time, $secretkey, true);
// Use last nipple of result as index/offset
$offset = ord(substr($hm, -1)) & 0x0F;
// grab 4 bytes of the result
$hashpart = substr($hm, $offset, 4);
// Unpak binary value
$value = unpack('N', $hashpart);
$value = $value[1];
// Only 32 bits
$value = $value & 0x7FFFFFFF;
$modulo = pow(10, $this->_codeLength);
return str_pad($value % $modulo, $this->_codeLength, '0', STR_PAD_LEFT);
}
that give me the 2FA code from the secret,
but I don't know how to call the function on another page and how to config the $timeslice
parameter. Thank you :)
In order to call the function from a different file you need to include or require the file before you can initiate the call. After which you can call the function (assuming you want 300 in your $timeSlice variable) like :
require 'functions_file.php';
$The_code = getCode("this is a secret", 300);
If the function is part of a class, you have to either instantiate the class like (assuming the class is named FunctionClass
) :
require 'functions_file.php';
$Instantiated_Class = new FunctionClass();
$The_code = $Instantiated_Class->getCode("this is a secret", 300);
Or you can do :
require 'functions_file.php';
$The_code = FunctionClass::getCode("this is a secret", 300);
User contributions licensed under CC BY-SA 3.0