You are here

public static function Google2FA::oathHotp in Google Authenticator login 7

Takes the secret key and the timestamp and returns the OTP.

Parameters

binary $key: Secret key in binary form.

int $counter: Timestamp as returned by getTimestamp.

Return value

string The OTP

2 calls to Google2FA::oathHotp()
ga_login_test_generate_code in tests/ga_login_test/ga_login_test.module
Generate the code for a given secret.
Google2FA::verifyKey in tests/ga_login_test/ga_login_test.module
Verifys a key against the current timestamp.

File

tests/ga_login_test/ga_login_test.module, line 160
ga_login_test module.

Class

Google2FA
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Code

public static function oathHotp($key, $counter) {
  if (strlen($key) < 8) {
    throw new Exception('Secret key is too short. Must be at least 16 base 32 characters');
  }

  // Counter must be 64-bit int.
  $bin_counter = pack('N*', 0) . pack('N*', $counter);
  $hash = hash_hmac('sha1', $bin_counter, $key, TRUE);
  return str_pad(self::oathTruncate($hash), self::OTPLENGTH, '0', STR_PAD_LEFT);
}