Files
cmii-uav-watchdog-project/cmii-uav-watchdog-common/totp_tier_one/totp_tier_one_service.go
2025-12-06 11:26:05 +08:00

40 lines
1.0 KiB
Go

package totp_tier_one
import (
otp "cmii-uav-watchdog-otp"
"cmii-uav-watchdog-otp/totp"
"time"
)
var TierOneTOTPSecretOpts = totp.GenerateOpts{
SecretSize: 64,
Issuer: "cmii-uav-watchdog-center",
AccountName: "cmii-uav-watchdog-center",
Period: 30 * 2 * 30, // 30分钟
Digits: otp.DigitsEight,
Algorithm: otp.AlgorithmSHA256,
}
// GenerateTierOneTOTPCode 生成一级TOTP验证码
func GenerateTierOneTOTPCode(secret string) (string, error) {
validateOpts := totp.ValidateOpts{}
validateOpts.ConvertToValidateOpts(TierOneTOTPSecretOpts)
code, err := totp.GenerateCodeCustom(secret, time.Now(), validateOpts)
if err != nil {
return "", err
}
return code, nil
}
// VerifyTierOneTOTPCode 验证一级TOTP验证码
func VerifyTierOneTOTPCode(code string, secret string) bool {
validateOpts := totp.ValidateOpts{}
validateOpts.ConvertToValidateOpts(TierOneTOTPSecretOpts)
valid, err := totp.ValidateCustom(code, secret, time.Now(), validateOpts)
if err != nil {
return false
}
return valid
}