Created
November 28, 2020 12:40
-
-
Save ergofriend/8f2c54eb790e6af7a28f11b3f1b427cf to your computer and use it in GitHub Desktop.
golang totp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/pquerna/otp" | |
"github.com/pquerna/otp/totp" | |
"fmt" | |
"os" | |
"time" | |
) | |
func main() { | |
key, _ := totp.Generate(totp.GenerateOpts{ | |
Issuer: "aaaaa", | |
AccountName: "xxx", | |
Algorithm: otp.AlgorithmSHA512, | |
Digits: otp.DigitsEight, | |
Period: 10, | |
}) | |
fmt.Printf("%+v\n", key) | |
ops := totp.ValidateOpts{ | |
Period: 10, | |
Skew: 0, | |
Digits: otp.DigitsEight, | |
Algorithm: otp.AlgorithmSHA512, | |
} | |
passcode, _ := totp.GenerateCodeCustom(key.Secret(), time.Now(), ops) | |
fmt.Println(passcode) | |
valid, _ := totp.ValidateCustom(passcode, key.Secret(), time.Now(), ops) | |
if valid { | |
println("Valid passcode!") | |
os.Exit(0) | |
} else { | |
println("Invalid passcode!") | |
os.Exit(1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment