-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pad non-padded secrets. This lets us continue building on <= go1.8.
- Add tests for secrets using various padding methods. - Add a new method/test to append padding to non-padded secrets.
- Loading branch information
Showing
5 changed files
with
113 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package twofactor | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// Pad calculates the number of '='s to add to our encoded string | ||
// to make base32.StdEncoding.DecodeString happy | ||
func Pad(s string) string { | ||
if !strings.HasSuffix(s, "=") && len(s)%8 != 0 { | ||
for len(s)%8 != 0 { | ||
s += "=" | ||
} | ||
} | ||
return s | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package twofactor | ||
|
||
import ( | ||
"encoding/base32" | ||
"fmt" | ||
"math/rand" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
const letters = "1234567890!@#$%^&*()abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
|
||
func randString() string { | ||
b := make([]byte, rand.Intn(len(letters))) | ||
for i := range b { | ||
b[i] = letters[rand.Intn(len(letters))] | ||
} | ||
return base32.StdEncoding.EncodeToString(b) | ||
} | ||
|
||
func TestPadding(t *testing.T) { | ||
for i := 0; i < 300; i++ { | ||
b := randString() | ||
origEncoding := string(b) | ||
modEncoding := strings.Replace(string(b), "=", "", -1) | ||
str, err := base32.StdEncoding.DecodeString(origEncoding) | ||
if err != nil { | ||
fmt.Println("Can't decode: ", string(b)) | ||
t.FailNow() | ||
} | ||
|
||
paddedEncoding := Pad(modEncoding) | ||
if origEncoding != paddedEncoding { | ||
fmt.Println("Padding failed:") | ||
fmt.Printf("Expected: '%s'", origEncoding) | ||
fmt.Printf("Got: '%s'", paddedEncoding) | ||
t.FailNow() | ||
} else { | ||
mstr, err := base32.StdEncoding.DecodeString(paddedEncoding) | ||
if err != nil { | ||
fmt.Println("Can't decode: ", paddedEncoding) | ||
t.FailNow() | ||
} | ||
|
||
if string(mstr) != string(str) { | ||
fmt.Println("Re-padding failed:") | ||
fmt.Printf("Expected: '%s'", str) | ||
fmt.Printf("Got: '%s'", mstr) | ||
t.FailNow() | ||
} | ||
} | ||
} | ||
} |