php集成动态口令认证

大多数系统目前均使用的静态密码进行身份认证登录,但由于静态密码容易被窃取,其安全性无法满足安全要求。

动态口令采用一次一密、用过密码作废的方式防止了密码被窃取带来的安全问题。
动态口令分为HOTP(基于事件计数的动态口令,RFC4226)、TOTP(基于时间计数的动态口令,RFC6238)、OCRA(挑战应答式动态口令,RFC6287)等方式。

本文介绍了集成TOTP方式的动态口令认证的方案,PHP框架采用Thinkphp3.2.3,动态口令生成器使用的是google authtication。

1、为Thinkphp框架添加oath算法类

oath算法封装类oath.php代码如下:

<?PHP
/**
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * PHP Google two-factor authentication module.
 *
 * See http://www.idontplaydarts.com/2011/07/google-totp-two-factor-authentication-for-php/
 * for more details
 *
 * @author Phil
 **/

class Google2FA {
    

    const keyRegeneration     = 30;    // Interval between key regeneration
    const otpLength        = 6;    // Length of the Token generated

    private static $lut = array(    // Lookup needed for Base32 encoding
        "A" => 0,    "B" => 1,
        "C" => 2,    "D" => 3,
        "E" => 4,    "F" => 5,
        "G" => 6,    "H" => 7,
        "I" => 8,    "J" => 9,
        "K" => 10,    "L" => 11,
        "M" => 12,    "N" => 13,
        "O" => 14,    "P" => 15,
        "Q" => 16,    "R" => 17,
        "S" => 18,    "T" => 19,
        "U" => 20,    "V" => 21,
        "W" => 22,    "X" => 23,
        "Y" => 24,    "Z" => 25,
        "2" => 26,    "3" => 27,
        "4" => 28,    "5" => 29,
        "6" => 30,    "7" => 31
    );

    /**
     * Generates a 16 digit secret key in base32 format
     * @return string
     **/
    public static function generate_secret_key($length = 16) {
    
        $b32     = "234567QWERTYUIOPASDFGHJKLZXCVBNM";
        $s     = "";

        for ($i = 0; $i < $length; $i++)
            $s .= $b32[rand(0,31)];

        return $s;
    }

    /**
  
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值