diff options
| author | Shulhan <m.shulhan@gmail.com> | 2020-12-23 14:18:33 +0700 |
|---|---|---|
| committer | Shulhan <m.shulhan@gmail.com> | 2020-12-23 14:18:33 +0700 |
| commit | f9804fe2bce4ff427391c433c93f9f70381ec57d (patch) | |
| tree | 9f4c6e7a49849a8046f4dbd1e872bf57ef1bc13e | |
| parent | f30f61a2c140f1c95119e0259d496feea16093d2 (diff) | |
| download | pakakeh.go-f9804fe2bce4ff427391c433c93f9f70381ec57d.tar.xz | |
cmd: add new CLI "totp"
The totp is a program to generate Time-based One-time Password from
secret key.
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | cmd/totp/main.go | 66 |
2 files changed, 68 insertions, 0 deletions
@@ -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] <SECRET_KEY> + +Available OPTIONS: +`, os.Args[0], os.Args[0]) + flag.PrintDefaults() + os.Exit(2) +} |
