initial commit
This commit is contained in:
commit
f2f49009fe
6 changed files with 122 additions and 0 deletions
55
main.go
Normal file
55
main.go
Normal 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("")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue