commit f2f49009fec2e1dc288d6c7d7a6c547691faf8b9 Author: mariusschellenberger Date: Thu Aug 22 08:57:19 2019 +0200 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7727bc4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +otp diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..45785b1 --- /dev/null +++ b/LICENSE @@ -0,0 +1,24 @@ +Copyright (C) 2019 Marius Schellenberger +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * The names of the authors and/or contributors may not be used to + endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL ston1th BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..c50b522 --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +# otp - a simple TOTP code generator written in go + +## Usage + +``` +$ ./otp -h +Usage of ./otp: + -loop + print new code every 30 seconds +``` + +## Example + +``` +$ ./otp +Enter secret key: +Code: 123456 Seconds left: 23 +``` diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..becf391 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module otp + +go 1.12 + +require ( + github.com/pquerna/otp v1.2.0 + golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..8faf767 --- /dev/null +++ b/go.sum @@ -0,0 +1,16 @@ +github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc h1:biVzkmvwrH8WK8raXaxBx6fRVTlJILwEwQGL1I/ByEI= +github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pquerna/otp v1.2.0 h1:/A3+Jn+cagqayeR3iHs/L62m5ue7710D35zl1zJ1kok= +github.com/pquerna/otp v1.2.0/go.mod h1:dkJfzwRKNiegxyNb54X/3fLwhCynbMspSyWKnvi1AEg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586 h1:7KByu05hhLed2MO29w7p1XfZvZ13m8mub3shuVftRs0= +golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/main.go b/main.go new file mode 100644 index 0000000..290e297 --- /dev/null +++ b/main.go @@ -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("") + } +}