Files
go-base58/base58.go
Jim Myhrberg f8ea86748a Switch from string to []byte slices
The way I'm using the base58 required me to do a lot conversions between
[]byte slices and strings. Not anymore.

Also switching to []byte slices allowed some nice performance
improvements.

Before, with strings:

    BenchmarkEncode-4        3000000               511 ns/op
    BenchmarkDecode-4        5000000               300 ns/op

After, with []byte slices:

    BenchmarkEncode-4        5000000               256 ns/op
    BenchmarkDecode-4       20000000               107 ns/op
2016-07-03 11:14:38 +01:00

55 lines
1.0 KiB
Go

package base58
import (
"bytes"
"errors"
)
// Alphabet is the default alphabet.
var Alphabet = []byte(
"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ",
)
var base = len(Alphabet)
var errInvalidBase58 = errors.New("invalid base58")
// Encode converts a base10 integer to a base58 string using the default
// alphabet.
func Encode(num int) []byte {
str := []byte{}
for num >= base {
mod := num % base
str = prepend(str, Alphabet[mod])
num = (num - mod) / base
}
return prepend(str, Alphabet[num])
}
// Decode converts a base58 string to a base10 integer using the default
// alphabet.
func Decode(str []byte) (int, error) {
num := 0
multi := 1
for i := len(str); i > 0; i-- {
char := str[i-1]
index := bytes.IndexByte(Alphabet, char)
if index == -1 {
return -1, errInvalidBase58
}
num += multi * index
multi = multi * base
}
return num, nil
}
func prepend(slice []byte, elem byte) []byte {
slice = append(slice, byte(0))
copy(slice[1:], slice)
slice[0] = elem
return slice
}