搜尋分類、套件與程式碼儲存庫

套件詳情v3.2.1

otp

RFC6238 時間為基礎的一次性密碼 / Google Authenticator 庫

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
8.7萬
喜歡數
204
Pub 分數
160

安裝套件

flutter pub add otp

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個 Dart 庫,實作 RFC 6238 時間型一次性密碼(TOTP)演算法,相容 Google Authenticator 與其他 TOTP 相容應用。它能產生安全、時間限制的驗證碼,用於雙因素認證(2FA)。此套件支援在 Android、iOS、Web、macOS、Windows 與 Linux 等多個平台產生與驗證 OTP。非常適合在 Flutter 應用中以最少設定整合 2FA。

適用情境

  • 行動應用程式中的雙因素驗證
  • 帶2FA的安全登入系統
  • 與 Google Authenticator 集成
  • 產生時限性存取碼
  • 使用者帳戶驗證
  • 備份代碼生成

主要特色

  • 符合 RFC 6238 的 TOTP 實現
  • 跨平台支援
  • 簡單的OTP生成和驗證
  • 支援自定時間步長和數字
  • 與驗證器應用相容
  • 輕量且無依賴

適合對象

  • 需要2FA的應用
  • 驗證服務
  • 注重安全的應用程式
  • 開發安全登入流程的開發者
  • 與現有 TOTP 系統整合

使用建議

  • 需要正確的時間同步
  • 不支援QR碼產生
  • 無內建使用者介面
  • 手動處理金鑰儲存
  • 不適用於長期令牌儲存

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

MAJOR BEHAVIOR CHANGE IN 3.1.0: Explanation of Changes from 3.0 to 3.1

Dart

dart-otp

RFC4226/RFC6238 One-Time Password / Google Authenticator Library

Features:

Getting Started

Pubspec

pub.dartlang.org: (you can use 'any' instead of a version if you just want the latest always)

dependencies:
  otp: 3.2.0
import 'package:otp/otp.dart';

Start generating tokens.

// Generate TOTP code. (String versin of function incase of leading 0)
OTP.generateTOTPCodeString("JBSWY3DPEHPK3PXP", 1362302550000); // -> '505548'

// Generate HOTP Code.
OTP.generateHOTPCodeString("JBSWY3DPEHPK3PXP", 7); // -> '006676'

API

Notes

This library does not support any other secret input other than Base32. It is what is used standard most places, and by Google Authenticator. If your secrets are not Base32 forms, please use my Base32 library (the one I use as a dependency for this library) or any other base32 library to encode your secret before passing it into the functions. All generate functions force decode of Base32.

Global Settings

useTOTPPaddingForHOTP (bool, default: false) Uses the TOTP padding method for handling secrets bigger or smaller than the mandatory sizes for SHA256/SHA512. This is needed as HOTP does not have an official method of using SHA256 or SHA512 in the RFC spec and most libraries don't pad HOTP for use with SHA256 or SHA512. (examples: otplib and speakeasy from Node.js)

If you enable this, it will use the same padding as TOTP (repeating the secret to the right length) but might cause incompatibilies with other libraries. I am defaulting to no padding, as this is the predominant behavior I am finding for HOTP.

OTP.generateTOTPCode(String secret, int currentTime, {int length: 6, int interval: 30, Algorithm algorithm: Algorithm.SHA256, bool isGoogle: false})

Generate a code for the provided secret and time.

  • secret - (String) A Base32 String.
  • currentTime - (int) The current time in milliseconds.
  • length - (int) The length of the resulting code.
  • interval - (int) Refresh interval to get a new code.
  • algorithm - (Algorithm) Hashing method.
  • isGoogle - (bool) flag to turn off secret padding for Google Auth.

Returns an int code. Does not preserve leading zeros.

OTP.generateTOTPCodeString(String secret, int currentTime, {int length: 6, int interval: 30, Algorithm algorithm: Algorithm.SHA256, bool isGoogle: false})

Generate a code for the provided secret and time.

  • secret - (String) A Base32 String.
  • currentTime - (int) The current time in milliseconds.
  • length - (int) The length of the resulting code.
  • interval - (int) Refresh interval to get a new code.
  • algorithm - (Algorithm) Hashing method.
  • isGoogle - (bool) flag to turn off secret padding for Google Auth.

Returns an String code. Preserves leading zeros.

OTP.generateHOTPCode(String secret, int counter, {int length: 6})

Generate a code for the provided secret and time.

  • secret - (String) A Base32 String.
  • counter - (int) An int counter.
  • length - (int) the length of the resulting code.

Returns an int code. Does not preserve leading zeros

OTP.generateHOTPCodeString(String secret, int counter, {int length: 6})

Generate a code for the provided secret and time.

  • secret - (String) A Base32 String.
  • counter - (int) An int counter.
  • length - (int) the length of the resulting code.

Returns an String code. Preserves leading zeros

OTP.constantTimeVerification(final String code, final String othercode)

!DISCLAIMER! I can only get this to be within 1 millisecond between different codes and same codes. I have yet to get them to be equal. but 1 millisecond should be an OK room for error though.

Compares 2 codes in constant time to minimize risk of timing attacks.

  • code - (String) A Base32 String.
  • othercode - (String) A Base32 String.

Returns an bool if they match or not.

OTP.remainingSeconds({int interval = 30})

This will return the remaining seconds based on the lastUsedTime variable, that is only set when using generateTOTP* functions.

  • interval - (int) The interval you are using for your codes, defaults to the standard 30 seconds.

Returns an int of the remaining seconds.

Testing

pub run test

Release notes

See CHANGELOG.md

所屬分類

依賴

依賴清單 3 項

原始碼儲存庫

使用此套件的專案