You are here

function GoogleAuthenticator::oath_truncate in Google Authenticator login 6

1 call to GoogleAuthenticator::oath_truncate()
GoogleAuthenticator::oath_hotp in ./ga4php.php

File

./ga4php.php, line 401

Class

GoogleAuthenticator

Code

function oath_truncate($hash, $length = 6) {

  // Convert to dec
  foreach (str_split($hash, 2) as $hex) {
    $hmac_result[] = hexdec($hex);
  }

  // Find offset
  $offset = $hmac_result[19] & 0xf;

  // Algorithm from RFC
  return (($hmac_result[$offset + 0] & 0x7f) << 24 | ($hmac_result[$offset + 1] & 0xff) << 16 | ($hmac_result[$offset + 2] & 0xff) << 8 | $hmac_result[$offset + 3] & 0xff) % pow(10, $length);
}