initial commit

This commit is contained in:
mariusschellenberger 2019-08-22 08:57:19 +02:00
commit f2f49009fe
6 changed files with 122 additions and 0 deletions

55
main.go Normal file
View file

@ -0,0 +1,55 @@
// 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("")
}
}