Use STDLib encoder

This commit is contained in:
Netscape Navigator 2020-08-18 08:02:43 -05:00
parent 254954a2aa
commit 8f34a06adf
4 changed files with 13 additions and 83 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
- [ ] Get a good CI system going? Run tests at PR time, prevent coverage slips, etc..
- [ ] Finish all the things below:
|Done?|Verb |Noun | Flag / arg 1 | Flag 2 |

2
go.mod
View File

@ -15,6 +15,6 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.7.0
golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c // indirect
golang.org/x/text v0.3.3 // indirect
golang.org/x/text v0.3.3
gopkg.in/ini.v1 v1.57.0 // indirect
)

View File

@ -1,9 +1,8 @@
package pigeon
import (
"bytes"
"crypto/ed25519"
"encoding/binary"
"encoding/base32"
"fmt"
"os"
)
@ -23,76 +22,12 @@ func CreateKeypair() (ed25519.PublicKey, ed25519.PrivateKey) {
return pub, priv
}
var decodeTable = map[uint16]rune{
0b00000: '0',
0b00001: '1',
0b00010: '2',
0b00011: '3',
0b00100: '4',
0b00101: '5',
0b00110: '6',
0b00111: '7',
0b01000: '8',
0b01001: '9',
0b01010: 'A',
0b01011: 'B',
0b01100: 'C',
0b01101: 'D',
0b01110: 'E',
0b01111: 'F',
0b10000: 'G',
0b10001: 'H',
0b10010: 'J',
0b10011: 'K',
0b10100: 'M',
0b10101: 'N',
0b10110: 'P',
0b10111: 'Q',
0b11000: 'R',
0b11001: 'S',
0b11010: 'T',
0b11011: 'V',
0b11100: 'W',
0b11101: 'X',
0b11110: 'Y',
0b11111: 'Z',
}
// Encoder is an Encoder
var Encoder = base32.NewEncoding("0123456789ABCDEFGHJKMNPQRSTVWXYZ").WithPadding(base32.NoPadding)
// getBase32Size returns the number of characters needed to
// encode a given byte array to base32.
func getBase32Size(data []byte) int {
bits := len(data) * 8
return (bits / 5) + (bits % 5)
}
// getNthBase32Char retrieves the Crockford base 32 character
// at a particular index
func getNthBase32Char(n int, data []byte) rune {
startingBit := n * 5
startingByte := startingBit / 8
lShift := startingBit % 8
byte1 := data[startingByte]
var byte2 byte
if (n + 1) > len(data) {
byte2 = 0b00000000
} else {
byte2 = data[startingByte+1]
}
chunk := binary.BigEndian.Uint16([]byte{byte1, byte2})
pentad := (chunk << lShift) >> 11
return decodeTable[pentad]
}
// B32Encode converts a byte array into a Crockford Base 32
// string.
// B32Encode does Crockford 32 encoding on a string.
func B32Encode(data []byte) string {
var b bytes.Buffer
for i := 0; i < getBase32Size(data); i++ {
b.WriteRune(getNthBase32Char(i, data))
}
return b.String()
return Encoder.EncodeToString(data)
}
// B32Decode takes a Crockford Base32 string and converts it

View File

@ -54,18 +54,12 @@ var tests = []testCase{
}
func TestB32Encode(t *testing.T) {
for _, k := range tests {
actual := B32Encode(k.decoded)
if actual == k.encoded {
fmt.Println("THIS IS OK")
} else {
fmt.Printf(`
EXPECTED: %s
LEN: %d
ACTUAL: %s
LEN: %d
`, k.encoded, len(k.encoded), actual, len(actual))
t.FailNow()
for _, test := range tests {
actual := B32Encode(test.decoded)
expected := test.encoded
if actual != expected {
fmt.Printf("FAIL:\n Exp: %s\n Act: %s\n", expected, actual)
t.Fail()
}
}
}