public function generateSecret() { $secret = ""; for ($i = 1; $i <= self::$SECRET_LENGTH; $i++) { $c = rand(0, 255); $secret .= pack("c", $c); } $base32 = new FixedBitNotation(5, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', true, true); return $base32->encode($secret); }
/** * Generate a code (for a given time) * * @param string $secret * @param timestamp $time * @return int */ public function getCode($secret, $time = null) { if (!$time) { $time = floor(time() / 30); } $base32 = new FixedBitNotation(5, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', TRUE, TRUE); $secret = $base32->decode($secret); $time = pack("N", $time); $time = str_pad($time, 8, chr(0), STR_PAD_LEFT); $hash = hash_hmac('sha1', $time, $secret, true); $offset = ord(substr($hash, -1)); $offset = $offset & 0xf; $truncatedHash = self::hashToInt($hash, $offset) & 0x7fffffff; $pinValue = str_pad($truncatedHash % self::$PIN_MODULO, 6, "0", STR_PAD_LEFT); return $pinValue; }
/** * @return string */ public function generateSecret() { $secret = ""; for ($i = 1; $i <= $this->secretLength; $i++) { $c = rand(0, 255); $secret .= pack("c", $c); } $base32 = new FixedBitNotation(5, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', TRUE, TRUE); return $base32->encode($secret); }