当前位置: 首页 > article >正文

PHP实现TOTP: Time-Based One-Time Password Algorithm

项目地址:https://github.com/cook-code-jazor/totp-php

RFC文档: TOTP: Time-Based One-Time Password Algorithm

构造/解析Uri

$uri = new Uri('otpauth://totp/test_account:your-site.com?secret=Y5C4TFC5Q6OZHMXS7NOEDO5AYUP5XWMK&algorithm=sha1&digits=6&period=30');

$secret = $uri->getSecret();

echo 'key = ' . $secret . "\r\n";
// output:
// key = Y5C4TFC5Q6OZHMXS7NOEDO5AYUP5XWMK

$uri = new Uri();
$uri->setLabel('test');
$uri->setType('totp'); //default is 'totp'
$uri->setSecret('Y5C4TFC5Q6OZHMXS7NOEDO5AYUP5XWMK');

echo 'totp uri = ' . $uri . "\r\n";
// output:
// totp uri = otpauth://totp/test?secret=Y5C4TFC5Q6OZHMXS7NOEDO5AYUP5XWMK

快速调用

默认为30秒步长,6个数字的验证码,使用SHA1算法。

//生成
echo TOTP::generate('Y5C4TFC5Q6OZHMXS7NOEDO5AYUP5XWMK', time());

//校验
echo TOTP::verify('123456', 'Y5C4TFC5Q6OZHMXS7NOEDO5AYUP5XWMK', time());

更多测试

// Seed for HMAC-SHA1 - 20 bytes
$seed = Base32::decode("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ");
// Seed for HMAC-SHA256 - 32 bytes
$seed32 = Base32::decode("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZA====");
// Seed for HMAC-SHA512 - 64 bytes
$seed64 = Base32::decode("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQGEZDGNA=");
$T0 = 0;
$X = 30;
$testTime = [59, 1111111109, 1111111111, 1234567890, 2000000000, 20000000000];

ini_set('date.timezone', 'UTC');
for ($t = 0; $t < count($testTime); $t++) {
    echo sprintf(
        "time: %s, date: %s, code: %s, alg: SHA1\r\n",
        $testTime[$t],
        date('Y-m-d H:i:s', $testTime[$t]),
        TOTP::compute($seed, $testTime[$t], 'sha1', 8, $X, $T0));


    echo sprintf(
        "time: %s, date: %s, code: %s, alg: SHA256\r\n",
        $testTime[$t],
        date('Y-m-d H:i:s', $testTime[$t]),
        TOTP::compute($seed32, $testTime[$t], 'sha256', 8, $X, $T0));

    echo sprintf(
        "time: %s, date: %s, code: %s, alg: SHA512\r\n",
        $testTime[$t],
        date('Y-m-d H:i:s', $testTime[$t]),
        TOTP::compute($seed64, $testTime[$t], 'sha512', 8, $X, $T0));
}

/* 输出结果:
time: 59, date: 1970-01-01 00:00:59, code: 94287082, alg: SHA1
time: 59, date: 1970-01-01 00:00:59, code: 46119246, alg: SHA256
time: 59, date: 1970-01-01 00:00:59, code: 90693936, alg: SHA512
time: 1111111109, date: 2005-03-18 01:58:29, code: 07081804, alg: SHA1
time: 1111111109, date: 2005-03-18 01:58:29, code: 68084774, alg: SHA256
time: 1111111109, date: 2005-03-18 01:58:29, code: 25091201, alg: SHA512
time: 1111111111, date: 2005-03-18 01:58:31, code: 14050471, alg: SHA1
time: 1111111111, date: 2005-03-18 01:58:31, code: 67062674, alg: SHA256
time: 1111111111, date: 2005-03-18 01:58:31, code: 99943326, alg: SHA512
time: 1234567890, date: 2009-02-13 23:31:30, code: 89005924, alg: SHA1
time: 1234567890, date: 2009-02-13 23:31:30, code: 91819424, alg: SHA256
time: 1234567890, date: 2009-02-13 23:31:30, code: 93441116, alg: SHA512
time: 2000000000, date: 2033-05-18 03:33:20, code: 69279037, alg: SHA1
time: 2000000000, date: 2033-05-18 03:33:20, code: 90698825, alg: SHA256
time: 2000000000, date: 2033-05-18 03:33:20, code: 38618901, alg: SHA512
time: 20000000000, date: 2603-10-11 11:33:20, code: 65353130, alg: SHA1
time: 20000000000, date: 2603-10-11 11:33:20, code: 77737706, alg: SHA256
time: 20000000000, date: 2603-10-11 11:33:20, code: 47863826, alg: SHA512
 */

在这里插入图片描述


http://www.kler.cn/a/350119.html

相关文章:

  • 微软Win10 RP 19045.5435(KB5050081)预览版发布!
  • 数据库事务详解
  • qiankun+vite+vue3
  • 雷电9最新版安装Magisk+LSPosd(新手速通)
  • oracle之行转列
  • 鸿蒙开发中的骨架图:提升用户体验的关键一环
  • JAVA 中的克隆对象
  • 强化学习和QLearning及GAN到底是什么关系啊
  • SpringSecurity(一)——认证实现
  • 一区大黄蜂!人工蜂群算法优化!ABC-CNN-LSTM-MATT多特征分类预测
  • Jackson在Spring Boot中的开发技巧详解
  • 在顺序结构和链式结构的线性表上实现顺序检索算法
  • Ascend C算子编程和C++基础 Lesson3-4 性能优化
  • 流程图
  • Angular 实现 keep-alive (路由复用)
  • 鸿蒙应用,如何保存用户的 token
  • 【.net core使用minio大文件分片上传】.net core使用minio大文件分片上传以及断点续传、秒传思路
  • 构建可扩展的高校学科竞赛平台:SpringBoot案例分析
  • 2.2.ReactOS系统KSERVICE_TABLE_DESCRIPTOR结构体的声明
  • 基于Spring Boot REST API设计指南
  • 控制Stable Diffusion生成质量的多种方法
  • 水库大坝安全监测预警系统守护大坝安全卫士
  • 余 弦 曲 线
  • FineReport 动态列切换统计维度
  • 【大数据应用开发】2023年全国职业院校技能大赛赛题第10套
  • 【无标题】react组件封装