]>
git.wh0rd.org - tt-rss.git/blob - vendor/OTPHP/TOTP.php
3 * Copyright (c) 2011 Le Lag
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * TOTP - One time password generator
27 * The TOTP class allow for the generation
28 * and verification of one-time password using
29 * the TOTP specified algorithm.
31 * This class is meant to be compatible with
32 * Google Authenticator
34 * This class was originally ported from the rotp
35 * ruby library available at https://github.com/mdp/rotp
37 class TOTP
extends OTP
{
39 * The interval in seconds for a one-time password timeframe
45 public function __construct($s, $opt = Array()) {
46 $this->interval
= isset($opt['interval']) ?
$opt['interval'] : 30;
47 parent
::__construct($s, $opt);
51 * Get the password for a specific timestamp value
53 * @param integer $timestamp the timestamp which is timecoded and
54 * used to seed the hmac hash function.
55 * @return integer the One Time Password
57 public function at($timestamp) {
58 return $this->generateOTP($this->timecode($timestamp));
62 * Get the password for the current timestamp value
64 * @return integer the current One Time Password
66 public function now() {
67 return $this->generateOTP($this->timecode(time()));
71 * Verify if a password is valid for a specific counter value
73 * @param integer $otp the one-time password
74 * @param integer $timestamp the timestamp for the a given time, defaults to current time.
75 * @return bool true if the counter is valid, false otherwise
77 public function verify($otp, $timestamp = null) {
78 if($timestamp === null)
80 return ($otp == $this->at($timestamp));
84 * Returns the uri for a specific secret for totp method.
85 * Can be encoded as a image for simple configuration in
86 * Google Authenticator.
88 * @param string $name the name of the account / profile
89 * @return string the uri for the hmac secret
91 public function provisioning_uri($name) {
92 return "otpauth://totp/".urlencode($name)."?secret={$this->secret}";
96 * Transform a timestamp in a counter based on specified internal
98 * @param integer $timestamp
99 * @return integer the timecode
101 protected function timecode($timestamp) {
102 return (int)( (((int)$timestamp * 1000) / ($this->interval
* 1000)));