lbry.go/schema/address/base58/encode.go

33 lines
659 B
Go
Raw Normal View History

2017-09-12 18:02:30 +02:00
package base58
import (
"math/big"
)
func EncodeBase58(data []byte) string {
2018-02-15 20:51:51 +01:00
longValue := big.NewInt(0)
2017-09-12 18:02:30 +02:00
result := ""
for i := 0; i < len(data); i++ {
to_add := big.NewInt(0)
to_add = to_add.Exp(big.NewInt(256), big.NewInt(int64(i)), to_add)
to_add = to_add.Mul(big.NewInt(int64(data[24-i])), to_add)
2018-02-15 20:51:51 +01:00
longValue = longValue.Add(to_add, longValue)
2017-09-12 18:02:30 +02:00
}
i := 0
for {
m := big.NewInt(0)
2018-02-15 20:51:51 +01:00
longValue, m = longValue.DivMod(longValue, big.NewInt(58), m)
2017-09-12 18:02:30 +02:00
bs := m.Bytes()
if len(bs) == 0 {
bs = append(bs, 0x00)
}
2018-02-15 20:51:51 +01:00
b := b58Characters[bs[0]]
2017-09-12 18:02:30 +02:00
result = string(b) + result
2018-02-15 20:51:51 +01:00
if longValue.Int64() == 0 {
2017-09-12 18:02:30 +02:00
break
}
i += 1
}
return result
}