Skip to content

OTP - 一次性密码

OTP 提供一次性密码生成和验证功能。

特性

  • TOTP(基于时间)
  • HOTP(基于计数器)
  • QR 码生成
  • 验证支持

安装

bash
go get github.com/o-zoox/otp

快速开始

基本使用

go
package main

import (
    "fmt"
    "github.com/o-zoox/otp"
)

func main() {
    // 生成密钥
    key, err := otp.GenerateKey("user@example.com", "MyApp")
    if err != nil {
        panic(err)
    }
    
    // 生成 TOTP
    code, err := otp.GenerateTOTP(key.Secret())
    if err != nil {
        panic(err)
    }
    fmt.Println("TOTP Code:", code)
    
    // 验证 TOTP
    valid := otp.ValidateTOTP(code, key.Secret())
    fmt.Println("Valid:", valid)
}

更多信息

基于 VitePress 构建