Skip to content

Instantly share code, notes, and snippets.

@ergofriend
Created November 28, 2020 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ergofriend/8f2c54eb790e6af7a28f11b3f1b427cf to your computer and use it in GitHub Desktop.
Save ergofriend/8f2c54eb790e6af7a28f11b3f1b427cf to your computer and use it in GitHub Desktop.
golang totp
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