You are here

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

Decodes a base32 string into a binary string.

2 calls to Google2FA::base32Decode()
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 120
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 base32Decode($b32) {
  $b32 = strtoupper($b32);
  if (!preg_match('/^[ABCDEFGHIJKLMNOPQRSTUVWXYZ234567]+$/', $b32, $match)) {
    throw new Exception('Invalid characters in the base32 string.');
  }
  $l = strlen($b32);
  $n = 0;
  $j = 0;
  $binary = "";
  for ($i = 0; $i < $l; $i++) {

    // Move buffer left by 5 to make room.
    $n = $n << 5;

    // Add value into buffer.
    $n = $n + self::$lut[$b32[$i]];

    // Keep track of number of bits in buffer.
    $j = $j + 5;
    if ($j >= 8) {
      $j = $j - 8;
      $binary .= chr(($n & 0xff << $j) >> $j);
    }
  }
  return $binary;
}