From f9804fe2bce4ff427391c433c93f9f70381ec57d Mon Sep 17 00:00:00 2001 From: Shulhan Date: Wed, 23 Dec 2020 14:18:33 +0700 Subject: cmd: add new CLI "totp" The totp is a program to generate Time-based One-time Password from secret key. --- README.md | 2 ++ cmd/totp/main.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 cmd/totp/main.go diff --git a/README.md b/README.md index 7bb5bdf0..077186df 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,8 @@ This library is released every month, usually at the first week of month. * [**smtpcli**](https://pkg.go.dev/github.com/shuLhan/share/cmd/smtpcli): Command line interface to SMTP client protocol. +* [**totp**](https://pkg.go.dev/github.com/shuLhan/share/cmd/totp): + Program to generate Time-based One-time Password using secret key. ## Libraries diff --git a/cmd/totp/main.go b/cmd/totp/main.go new file mode 100644 index 00000000..f175827c --- /dev/null +++ b/cmd/totp/main.go @@ -0,0 +1,66 @@ +// +// Program totp generate Time-based One-time Password from secret key. +// +package main + +import ( + "crypto/sha1" + "crypto/sha256" + "crypto/sha512" + "flag" + "fmt" + "hash" + "log" + "os" + + "github.com/shuLhan/share/lib/totp" +) + +func main() { + log.SetFlags(0) + + flag.Usage = usage + + paramDigits := flag.Int("digits", 6, "number of digits to generated") + paramHash := flag.String("hash", "sha1", "hash names, valid values is sha1, sha256, sha512") + paramTimestep := flag.Int("timestep", 30, "time step in seconds") + paramHelp := flag.Bool("help", false, "show command usage") + flag.Parse() + + if *paramHelp { + flag.Usage() + } + if len(os.Args) == 1 { + flag.Usage() + } + + var hashFn func() hash.Hash + switch *paramHash { + case "sha256": + hashFn = sha256.New + case "sha512": + hashFn = sha512.New + default: + hashFn = sha1.New + } + + totproto := totp.New(hashFn, *paramDigits, *paramTimestep) + secret := []byte(os.Args[1]) + p0, err := totproto.Generate(secret) + if err != nil { + log.Fatal(err) + } + + fmt.Printf("%s\n", p0) +} + +func usage() { + log.Printf(`%s is command line interface to generate time-based one-time password. +Usage: + %s [OPTIONS] + +Available OPTIONS: +`, os.Args[0], os.Args[0]) + flag.PrintDefaults() + os.Exit(2) +} -- cgit v1.3