Skip to content

Commit

Permalink
Merge pull request pquerna#12 from maxvw/10-fix-padding
Browse files Browse the repository at this point in the history
fix missing padding in secrets
  • Loading branch information
pquerna authored Feb 18, 2017
2 parents 5465390 + 0547845 commit 57ecb72
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions hotp/hotp.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ func GenerateCode(secret string, counter uint64) (string, error) {
// GenerateCodeCustom uses a counter and secret value and options struct to
// create a passcode.
func GenerateCodeCustom(secret string, counter uint64, opts ValidateOpts) (passcode string, err error) {
// As noted in issue #10 this adds support for TOTP secrets that are
// missing their padding.
if n := len(secret) % 8; n != 0 {
secret = secret + strings.Repeat("=", 8-n)
}

secretBytes, err := base32.StdEncoding.DecodeString(secret)
if err != nil {
return "", otp.ErrValidateSecretInvalidBase32
Expand Down
11 changes: 11 additions & 0 deletions hotp/hotp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ func TestValidateInvalid(t *testing.T) {
require.Equal(t, false, valid, "Valid should be false.")
}

// This tests for issue #10 - secrets without padding
func TestValidatePadding(t *testing.T) {
valid, err := ValidateCustom("831097", 0, "JBSWY3DPEHPK3PX",
ValidateOpts{
Digits: otp.DigitsSix,
Algorithm: otp.AlgorithmSHA1,
})
require.NoError(t, err, "Expected no error.")
require.Equal(t, true, valid, "Valid should be true.")
}

func TestGenerate(t *testing.T) {
k, err := Generate(GenerateOpts{
Issuer: "SnakeOil",
Expand Down

0 comments on commit 57ecb72

Please sign in to comment.