Add B32Decode()

This commit is contained in:
Netscape Navigator 2020-08-19 07:36:37 -05:00
parent 8f34a06adf
commit 81c0d66f54
3 changed files with 24 additions and 1 deletions

View File

@ -8,6 +8,7 @@ Don't use the Go version yet. If you want sommething stable, there is a [Ruby ve
# TODO
- [ ] Add a real testing lib to DRY things up.
- [ ] Get a good CI system going? Run tests at PR time, prevent coverage slips, etc..
- [ ] Finish all the things below:

View File

@ -33,5 +33,11 @@ func B32Encode(data []byte) string {
// B32Decode takes a Crockford Base32 string and converts it
// to a byte array.
func B32Decode(input string) []byte {
return []byte("WIP")
output, error := Encoder.DecodeString(input)
if error != nil {
msg := fmt.Sprintf("Error decoding Base32 string %s", input)
panic(msg)
}
return output
}

View File

@ -63,3 +63,19 @@ func TestB32Encode(t *testing.T) {
}
}
}
func TestB32Decode(t *testing.T) {
for i, test := range tests {
actual := B32Decode(test.encoded)
expected := test.decoded
if len(actual) != len(expected) {
fmt.Printf("\nFAIL: length mismatch at tests[%d]", i)
t.Fail()
}
for j, x := range expected {
if actual[j] != x {
fmt.Printf("tests[%d].encoded[%d] did not decode B32 properly (%s)", j, i, test.encoded)
}
}
}
}