Flutter動(dòng)態(tài)驗(yàn)證碼TOTP和HOTP方法

1. 前言

otp_util是一個(gè)package,用于生成和驗(yàn)證一次性密碼,用于在App應(yīng)用程序和其他需要登錄的系統(tǒng)中實(shí)現(xiàn)2FA和MFA身份驗(yàn)證方法。

該插件基于RFC6238(TOTP:基于時(shí)間的一次性密碼算法)和 RFC4226(HOTP:基于HMAC的一次性密碼算法)。

2.功能

  • 創(chuàng)建并驗(yàn)證TOTP對(duì)象
  • 創(chuàng)建并驗(yàn)證HOTP對(duì)象
  • 使用b32編碼的字符串生成一個(gè)otpauth url
  • 支持使用SHA1、SHA256、SHA384和SHA512加密的OTP令牌

3.安裝

dependencies:
 otp_util: ^1.0.2

點(diǎn)擊pub.dev查看。

4.示例

基于時(shí)間的OTP

import 'package:dart_otp/dart_otp.dart';

void main() {

 /// default initialization for intervals of 30 seconds and 6 digit tokens
 TOTP totp = TOTP(secret: "BASE32ENCODEDSECRET");

 /// initialization for custom interval and digit values
 TOTP totp = TOTP(secret: "BASE32ENCODEDSECRET", interval: 60, digits: 8);

 totp.now(); /// => 745872

 /// verify for the current time
 totp.verify(otp: '745872'); /// => true

 /// verify after 30s
 totp.verify(otp: '745872'); /// => false
}

基于計(jì)數(shù)器的OTP

import 'package:dart_otp/dart_otp.dart';

void main() {

 /// default initialization for intervals of 30 seconds and 6 digit tokens
 HOTP hotp = HOTP(secret: "BASE32ENCODEDSECRET");

 /// initialization for custom counter and digit value
 HOTP hotp = HOTP(secret: "BASE32ENCODEDSECRET", counter: 50, digits: 8);

 hotp.at(counter: 0); /// => 145089
 hotp.at(counter: 1); /// => 899133
 hotp.at(counter: 2438); /// => 146071

 /// verify with a counter
 hotp.verify(otp: '397732', counter: 2438); /// => true
 hotp.verify(otp: '397732', counter: 2438); /// => false
}

5.Demo

git地址:otp_util

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容