更新记录
0.0.2(2025-09-10)
- chore: 更新文档
0.0.1(2025-09-10)
- 首次发布
- 实现基于 RFC 6238 标准的 TOTP(基于时间的一次性密码)生成功能
- 支持 SHA1、SHA256、SHA512 哈希算法
- 支持自定义验证码位数(6位或8位)
- 支持自定义验证码有效期
- 支持生成标准的 otpauth:// 协议 URI,可用于生成二维码供 Google Authenticator 等应用扫描导入
平台兼容性
uni-app(4.71)
Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
---|---|---|---|---|---|---|---|---|
√ | √ | √ | √ | √ | √ | √ | √ | √ |
微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 快应用-华为 | 快应用-联盟 |
---|---|---|---|---|---|---|---|---|---|---|
√ | √ | √ | √ | √ | √ | √ | √ | √ | √ | √ |
uni-app x(4.73)
Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
---|---|---|---|---|---|
√ | √ | √ | √ | √ | √ |
lime-otpauth 多因素认证插件
一个实用的多因素认证插件,目前支持基于时间的一次性密码(TOTP),符合 RFC 6238 标准。支持多种哈希算法、自定义位数和时间周期等配置,可用于双因素认证、安全登录等多种场景。组件提供了多种自定义选项,可以满足常见的认证需求。
功能特点
- TOTP支持:基于时间的一次性密码生成,符合 RFC 6238 标准
- 多种哈希算法:支持 SHA1、SHA256、SHA512 算法
- 自定义配置:可自定义验证码位数、有效期等参数
- URI生成:支持生成标准的 otpauth:// 协议 URI,可用于生成二维码
未来计划
- HOTP支持:添加基于计数器的一次性密码支持,符合 RFC 4226 标准
- Steam令牌:添加 Steam 令牌认证支持
- 验证功能:增加验证功能,用于验证用户输入的一次性密码是否正确
文档链接
📚 组件详细文档请访问以下站点:
安装方法
- 在uni-app插件市场中搜索并导入
lime-otpauth
- 导入后可能需要重新编译项目
- 在页面中使用
useTOTP
函数
代码演示
基础用法
最简单的一次性密码生成用法,使用默认配置生成6位数字验证码。
<script setup lang="ts">
import { useTOTP } from '@/uni_modules/lime-otpauth'
const totp = useTOTP()
const code: string = totp.generate()
console.log('当前验证码:', code)
</script>
自定义配置
设置自定义密钥、算法和位数。
<script setup lang="ts">
import { useTOTP, type UseTOTPOptions } from '@/uni_modules/lime-otpauth'
const options: UseTOTPOptions = {
secret: 'JBSWY3DPEHPK3PXP', // 自定义密钥
algorithm: 'SHA256', // 使用SHA256算法
digits: 8, // 生成8位验证码
period: 30 // 30秒有效期
}
const totp = useTOTP(options)
const code: string = totp.generate()
console.log('自定义验证码:', code)
</script>
指定时间戳生成验证码
为特定时间点生成验证码,用于测试或验证。
<script setup lang="ts">
import { useTOTP, type LimeTOTPGenerateOptions } from '@/uni_modules/lime-otpauth'
const totp = useTOTP()
// 使用当前时间生成验证码
const currentCode: string = totp.generate()
// 使用指定时间戳生成验证码
const options: LimeTOTPGenerateOptions = {
secret: 'JBSWY3DPEHPK3PXP',
timestamp: 1622234248000 // 指定时间戳(毫秒)
}
const specificCode: string = totp.generate(options)
console.log('当前验证码:', currentCode)
console.log('指定时间验证码:', specificCode)
</script>
完整配置示例
使用所有可用选项配置TOTP生成器。
<script setup lang="ts">
import { useTOTP, type UseTOTPOptions } from '@/uni_modules/lime-otpauth'
const options: UseTOTPOptions = {
issuer: '我的应用', // 发行者/提供商名称
label: 'user@example.com', // 账户标签(通常为用户邮箱)
issuerInLabel: true, // 在标签中包含发行者
secret: 'JBSWY3DPEHPK3PXP', // 密钥
algorithm: 'SHA512', // 使用SHA512算法(更高安全性)
digits: 8, // 8位验证码
period: 60 // 60秒有效期
}
const totp = useTOTP(options)
const code: string = totp.generate()
console.log('完整配置验证码:', code)
</script>
生成 otpauth:// URI
生成标准的 otpauth:// 协议 URI,可用于生成二维码供 Google Authenticator 等应用扫描导入。
<script setup lang="ts">
import { useTOTP, UseTOTPOptions } from '@/uni_modules/lime-otpauth'
const options: UseTOTPOptions = {
issuer: '我的应用',
label: 'user@example.com',
secret: 'JBSWY3DPEHPK3PXP'
}
const totp = useTOTP(options)
// 生成 otpauth:// URI
const uri: string = totp.toString()
console.log('TOTP URI:', uri)
// 输出: otpauth://totp/我的应用:user@example.com?secret=JBSWY3DPEHPK3PXP&issuer=我的应用&algorithm=SHA1&digits=6&period=30
// 此 URI 可用于生成二维码,供 Google Authenticator 等 TOTP 应用扫描导入
</script>
API文档
类型导入
import {
useTOTP,
LimeTOTP,
UseTOTPOptions,
LimeTOTPGenerateOptions,
LimeHMACAlgorithm
} from '@/uni_modules/lime-otpauth'
useTOTP 函数参数
参数名 | 说明 | 类型 | 默认值 |
---|---|---|---|
issuer | 账户发行者/提供商名称 | string | "" |
label | 账户标签/标识(通常为用户邮箱或用户名) | string | "OTPAuth" |
issuerInLabel | 是否在标签中包含发行者前缀 | boolean | true |
secret | 密钥(支持字符串格式,自动转换为Base32格式) | string | 随机生成 |
algorithm | HMAC哈希算法(支持:SHA1/SHA256/SHA512) | LimeHMACAlgorithm | "SHA1" |
digits | 验证码位数(通常为6或8位数字) | number | 6 |
period | 验证码有效期(单位:秒) | number | 30 |
LimeTOTP 对象方法
方法名 | 说明 | 参数 | 返回值 |
---|---|---|---|
generate | 生成一次性密码 | 无参数或 LimeTOTPGenerateOptions | string |
toString | 将TOTP配置转换为标准的otpauth://协议URI字符串 | 无 | string |
LimeTOTPGenerateOptions 参数
参数名 | 说明 | 类型 | 默认值 |
---|---|---|---|
secret | 密钥(必须提供) | string | - |
algorithm | HMAC哈希算法 | LimeHMACAlgorithm | "SHA1" |
digits | 验证码位数 | number | 6 |
period | 时间步长(单位:秒) | number | 30 |
timestamp | 时间戳(毫秒) | number | Date.now() |
支持的算法
组件支持以下HMAC哈希算法:
算法名称 | 安全级别 | 说明 |
---|---|---|
SHA1 | 基本 | RFC 6238默认算法,160位 |
SHA256 | 推荐 | 256位,提供更好的安全性 |
SHA512 | 最高 | 512位,提供最高级别安全性 |
支持与赞赏
如果你觉得本插件解决了你的问题,可以考虑支持作者:
支付宝赞助 | 微信赞助 |
---|---|
![]() |
![]() |