lbry.go/schema/address/base58/encode.go
2021-10-06 16:28:17 -04:00

32 lines
651 B
Go

package base58
import (
"math/big"
)
func EncodeBase58(data []byte) string {
longValue := big.NewInt(0)
result := ""
for i := 0; i < len(data); i++ {
toAdd := big.NewInt(0)
toAdd = toAdd.Exp(big.NewInt(256), big.NewInt(int64(i)), toAdd)
toAdd = toAdd.Mul(big.NewInt(int64(data[24-i])), toAdd)
longValue = longValue.Add(toAdd, longValue)
}
i := 0
for {
m := big.NewInt(0)
longValue, m = longValue.DivMod(longValue, big.NewInt(58), m)
bs := m.Bytes()
if len(bs) == 0 {
bs = append(bs, 0x00)
}
b := b58Characters[bs[0]]
result = string(b) + result
if longValue.Int64() == 0 {
break
}
i += 1
}
return result
}