Skip to content

Commit

Permalink
lib/totp: add method GenerateWithTime and GenerateNWithTime
Browse files Browse the repository at this point in the history
The GenerateWithTime and GenerateNWithTime accept parameter
[time.Time] as the relative time for generated password.
  • Loading branch information
shuLhan committed Jan 29, 2024
1 parent e33a335 commit ec2b46e
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 5 deletions.
22 changes: 20 additions & 2 deletions lib/totp/totp.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,31 @@ func (p *Protocol) Generate(secret []byte) (otp string, err error) {
return p.generateWithTimestamp(mac, now)
}

// GenerateN generate n number of passwords from (current time - N*timeStep)
// until the curent time.
// GenerateWithTime generate one time password using ts as time and secret.
func (p *Protocol) GenerateWithTime(ts time.Time, secret []byte) (otp string, err error) {
var mac = hmac.New(p.fnHash, secret)
return p.generateWithTimestamp(mac, ts.Unix())
}

// GenerateN generate n number of passwords from (ts - 0*timeStep)
// until (ts - N*timeStep).
func (p *Protocol) GenerateN(secret []byte, n int) (listOTP []string, err error) {
var (
mac = hmac.New(p.fnHash, secret)
ts = time.Now().Unix()
)
return p.generateN(mac, ts, n)
}

// GenerateNWithTime generate n number of passwords from (ts - 0*timeStep)
// until (ts - N*timeStep).
func (p *Protocol) GenerateNWithTime(ts time.Time, secret []byte, n int) (listOTP []string, err error) {
var mac = hmac.New(p.fnHash, secret)
return p.generateN(mac, ts.Unix(), n)
}

func (p *Protocol) generateN(mac hash.Hash, ts int64, n int) (listOTP []string, err error) {
var (
otp string
t int64
x int
Expand Down
73 changes: 70 additions & 3 deletions lib/totp/totp_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,81 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package totp
package totp_test

import (
"encoding/hex"
"fmt"
"log"
"time"

"github.com/shuLhan/share/lib/totp"
)

func ExampleProtocol_GenerateNWithTime() {
var (
secretHex = `3132333435363738393031323334353637383930`

secret []byte
err error
)

secret, err = hex.DecodeString(secretHex)
if err != nil {
log.Fatal(err)
}

var (
proto = totp.New(totp.CryptoHashSHA1, totp.DefCodeDigits, totp.DefTimeStep)
ts = time.Date(2024, time.January, 29, 23, 37, 0, 0, time.UTC)

listOTP []string
)

listOTP, err = proto.GenerateNWithTime(ts, secret, 3)
if err != nil {
log.Fatal(err)
}

fmt.Println(listOTP)
// Output:
// [933840 870583 802638]
}

func ExampleProtocol_GenerateWithTime() {
var (
secretHex = `3132333435363738393031323334353637383930`

secret []byte
err error
)

secret, err = hex.DecodeString(secretHex)
if err != nil {
log.Fatal(err)
}

var (
proto = totp.New(totp.CryptoHashSHA1, totp.DefCodeDigits, totp.DefTimeStep)
ts = time.Date(2024, time.January, 29, 23, 37, 0, 0, time.UTC)

otp string
)

otp, err = proto.GenerateWithTime(ts, secret)
if err != nil {
log.Fatal(err)
}

fmt.Println(otp)
// Output:
// 933840
}

func ExampleProtocol_Verify() {
var (
secretHex = `3132333435363738393031323334353637383930`
proto = New(CryptoHashSHA1, DefCodeDigits, DefTimeStep)

otp string
err error
secret []byte
)
Expand All @@ -25,6 +86,12 @@ func ExampleProtocol_Verify() {
log.Fatal(err)
}

var (
proto = totp.New(totp.CryptoHashSHA1, totp.DefCodeDigits, totp.DefTimeStep)

otp string
)

otp, _ = proto.Generate(secret)

if proto.Verify(secret, otp, 1) {
Expand Down

0 comments on commit ec2b46e

Please sign in to comment.