otp/main.go
mariusschellenberger f2f49009fe initial commit
2019-08-22 08:57:19 +02:00

55 lines
912 B
Go

// Copyright (C) 2019 Marius Schellenberger
package main
import (
"flag"
"fmt"
"github.com/pquerna/otp/totp"
"golang.org/x/crypto/ssh/terminal"
"os"
"strconv"
"time"
)
var loop bool
func init() {
flag.BoolVar(&loop, "loop", false, "print new code every 30 seconds")
flag.Parse()
}
func printCode(secret []byte) {
now := time.Now()
code, err := totp.GenerateCode(string(secret), now)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
s := 30 - (now.Second() % 30)
sec := strconv.Itoa(s)
if s < 10 {
sec = " " + sec
}
fmt.Printf("Code: %s Seconds left: %s", code, sec)
}
func main() {
fmt.Print("Enter secret key: ")
secret, err := terminal.ReadPassword(int(os.Stdin.Fd()))
fmt.Println("")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if loop {
for {
printCode(secret)
fmt.Print("\r")
time.Sleep(time.Second)
}
} else {
printCode(secret)
fmt.Println("")
}
}