summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAntonio Carlos Ribeiro <acr@antoniocarlosribeiro.com>2015-03-23 23:25:50 -0300
committerAntonio Carlos Ribeiro <acr@antoniocarlosribeiro.com>2015-03-23 23:25:50 -0300
commit73990a3494c63d77bab314faab4cd08bc98debe1 (patch)
tree6bac24ebaf032278138eb2757615fad297759ffb /src
parent1e3af1e12d5d2b83196d8fcee63bdce83e310a16 (diff)
downloadgoogle2fa-73990a3494c63d77bab314faab4cd08bc98debe1.zip
google2fa-73990a3494c63d77bab314faab4cd08bc98debe1.tar.gz
google2fa-73990a3494c63d77bab314faab4cd08bc98debe1.tar.bz2
Accepts a secret prefix
Diffstat (limited to 'src')
-rw-r--r--src/Google2FA.php24
1 files changed, 20 insertions, 4 deletions
diff --git a/src/Google2FA.php b/src/Google2FA.php
index a644c01..2fd4082 100644
--- a/src/Google2FA.php
+++ b/src/Google2FA.php
@@ -30,6 +30,7 @@ namespace PragmaRX\Google2FA;
* @author Antonio Carlos Ribeiro @ PragmaRX
**/
+use Base32\Base32;
use PragmaRX\Google2FA\Exceptions\InvalidCharactersException;
use PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException;
use PragmaRX\Google2FA\Contracts\Google2FA as Google2FAContract;
@@ -84,18 +85,20 @@ class Google2FA implements Google2FAContract
* @param int $length
* @return string
*/
- public function generateSecretKey($length = 16)
+ public function generateSecretKey($length = 16, $secret = '')
{
$b32 = "234567QWERTYUIOPASDFGHJKLZXCVBNM";
- $s = "";
+ $secret = $secret ? $this->toBase32($secret) : '';
for ($i = 0; $i < $length; $i++)
{
- $s .= $b32[$this->getRandomNumber()];
+ $secret .= $b32[$this->getRandomNumber()];
}
- return $s;
+ $this->validateSecret($secret);
+
+ return $secret;
}
/**
@@ -286,4 +289,17 @@ class Google2FA implements Google2FAContract
}
}
+ /**
+ * Encode a string to Base32.
+ *
+ * @param $string
+ * @return mixed
+ */
+ private function toBase32($string)
+ {
+ $encoded = Base32::encode($string);
+
+ return str_replace('=', '', $encoded);
+ }
+
}