Beispiel #1
0
/**
 * Calculate a valid code given the shared secret key
 *
 * @param string $key        The shared secret key to use for calculating code.
 * @param mixed  $step_count The time step used to calculate the code, which is the floor of time() divided by step size.
 * @param int    $digits     The number of digits in the returned code.
 * @param string $hash       The hash used to calculate the code.
 * @param int    $time_step  The size of the time step.
 *
 * @return string The totp code
 */
function calc_totp($key, $step_count = false, $digits = 6, $hash = 'sha1', $time_step = 30)
{
    $secret = base32_decode($key);
    if (false === $step_count) {
        $step_count = floor(time() / $time_step);
    }
    $timestamp = pack64($step_count);
    $hash = hash_hmac($hash, $timestamp, $secret, true);
    $offset = ord($hash[19]) & 0xf;
    $code = ((ord($hash[$offset + 0]) & 0x7f) << 24 | (ord($hash[$offset + 1]) & 0xff) << 16 | (ord($hash[$offset + 2]) & 0xff) << 8 | ord($hash[$offset + 3]) & 0xff) % pow(10, $digits);
    return str_pad($code, $digits, '0', STR_PAD_LEFT);
}
Beispiel #2
0
<?
$encodedkey = "RmFzdGx5IFRva2VuIFRlc3Q=";
$interval = 60;
 
$key = base64_decode($encodedkey);
 
$number = pack64(intval(time()/$interval));
$token  = base64_encode(hash_hmac('sha256', $number, $key, true));

$response   = curl_get_contents("http://token.fastly.com/token");
$validation = curl_get_contents("http://token.fastly.com?$token");

print "Your Token:   $token\n";
print "Fastly Token: $response\n";
print "Validation:   $validation\n";

function pack64($value) {
  $l = ($value & 0xffffffff00000000) >>32;
  $r = $value & 0x00000000ffffffff;

  return pack('VV', $r, $l); 
}

function curl_get_contents($url)
{
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  $data = curl_exec($ch);
  curl_close($ch);