diff --git a/base58.go b/base58.go index fda5204..c4977ce 100644 --- a/base58.go +++ b/base58.go @@ -59,7 +59,7 @@ func encodeChunkTail(raw []byte, buf []byte) []byte { return buf } -func decodeChunk(buf []byte, encoded string) []byte { +func decodeChunk(buf []byte, encoded []byte) []byte { var intResult uint64 currentMultiplier := uint64(1) for i := len(encoded) - 1; i >= 0; i-- { @@ -94,32 +94,41 @@ func decodeChunk(buf []byte, encoded string) []byte { return nil } -func Encode(data ...[]byte) string { +func EncodeMoneroBase58(data ...[]byte) []byte { + return EncodeMoneroBase58PreAllocated(make([]byte, 0, func() (result int) { + for _, v := range data { + result += len(v) + } + return + }()), data...) +} + +func EncodeMoneroBase58PreAllocated(buf []byte, data ...[]byte) []byte { //preallocate common case combined := make([]byte, 0, 96) for _, item := range data { combined = append(combined, item...) } - result := make([]byte, 0, len(combined)*2) - buf := make([]byte, 0, len(combined)*2) + result := buf + tmpBuf := make([]byte, 0, len(combined)*2) length := len(combined) rounds := length / 8 for i := 0; i < rounds; i++ { - result = append(result, encodeChunk(combined[i*8:(i+1)*8], buf[:0])...) + result = append(result, encodeChunk(combined[i*8:(i+1)*8], tmpBuf[:0])...) } if length%8 > 0 { - result = append(result, encodeChunkTail(combined[rounds*8:], buf[:0])...) + result = append(result, encodeChunkTail(combined[rounds*8:], tmpBuf[:0])...) } - return string(result) + return result } -func Decode(data string) (result []byte) { +func DecodeMoneroBase58(data []byte) (result []byte) { //common case - return DecodePreAllocated(make([]byte, 0, 69), data) + return DecodeMoneroBase58PreAllocated(make([]byte, 0, 69), data) } -func DecodePreAllocated(buf []byte, data string) (result []byte) { +func DecodeMoneroBase58PreAllocated(buf []byte, data []byte) (result []byte) { result = buf length := len(data) rounds := length / 11