Skip to content

Commit

Permalink
Use base32.NoPadding rather than mutating the standard padding off af…
Browse files Browse the repository at this point in the history
…terwards. Fixes pquerna#38
  • Loading branch information
pquerna committed Jun 1, 2019
1 parent 66a1f6c commit 0da7463
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 3 additions & 1 deletion hotp/hotp.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ type GenerateOpts struct {
Algorithm otp.Algorithm
}

var b32NoPadding = base32.StdEncoding.WithPadding(base32.NoPadding)

// Generate creates a new HOTP Key.
func Generate(opts GenerateOpts) (*otp.Key, error) {
// url encode the Issuer/AccountName
Expand Down Expand Up @@ -180,7 +182,7 @@ func Generate(opts GenerateOpts) (*otp.Key, error) {
return nil, err
}

v.Set("secret", strings.TrimRight(base32.StdEncoding.EncodeToString(secret), "="))
v.Set("secret", b32NoPadding.EncodeToString(secret))
v.Set("issuer", opts.Issuer)
v.Set("algorithm", opts.Algorithm.String())
v.Set("digits", opts.Digits.String())
Expand Down
6 changes: 3 additions & 3 deletions totp/totp.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
package totp

import (
"strings"

"github.com/pquerna/otp"
"github.com/pquerna/otp/hotp"

Expand Down Expand Up @@ -144,6 +142,8 @@ type GenerateOpts struct {
Algorithm otp.Algorithm
}

var b32NoPadding = base32.StdEncoding.WithPadding(base32.NoPadding)

// Generate a new TOTP Key.
func Generate(opts GenerateOpts) (*otp.Key, error) {
// url encode the Issuer/AccountName
Expand Down Expand Up @@ -176,7 +176,7 @@ func Generate(opts GenerateOpts) (*otp.Key, error) {
return nil, err
}

v.Set("secret", strings.TrimRight(base32.StdEncoding.EncodeToString(secret), "="))
v.Set("secret", b32NoPadding.EncodeToString(secret))
v.Set("issuer", opts.Issuer)
v.Set("period", strconv.FormatUint(uint64(opts.Period), 10))
v.Set("algorithm", opts.Algorithm.String())
Expand Down

0 comments on commit 0da7463

Please sign in to comment.