29 Star 297 Fork 61

GVPdromara/dongle

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
base58.go 1.14 KB
一键复制 编辑 原始数据 按行查看 历史
kuafuRace 提交于 2021-08-24 13:53 +08:00 . add base58 encoding support
package dongle
import (
"bytes"
"math/big"
)
const base58table = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
// ByBase58 encodes by base58.
func (e encode) ByBase58() encode {
input, output := e.input, e.output
if input == nil {
return e
}
if output != nil {
input = output
}
var src []byte
intBytes := big.NewInt(0).SetBytes(input)
int0, int58 := big.NewInt(0), big.NewInt(58)
for intBytes.Cmp(big.NewInt(0)) > 0 {
intBytes.DivMod(intBytes, int58, int0)
src = append(src, string2bytes(base58table)[int0.Int64()])
}
e.output = reverseBytes(src)
return e
}
// ByBase58 encodes by base58.
func (d decode) ByBase58() decode {
input, output := d.input, d.output
if input == nil {
return d
}
if output != nil {
input = output
}
int0 := big.NewInt(0)
for _, val := range input {
index := bytes.IndexByte([]byte(base58table), val)
int0.Mul(int0, big.NewInt(58))
int0.Add(int0, big.NewInt(int64(index)))
}
d.output = int0.Bytes()
return d
}
// reverseBytes reverses byte slice.
func reverseBytes(b []byte) []byte {
for i := 0; i < len(b)/2; i++ {
b[i], b[len(b)-1-i] = b[len(b)-1-i], b[i]
}
return b
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/dromara/dongle.git
git@gitee.com:dromara/dongle.git
dromara
dongle
dongle
v0.0.2

搜索帮助