Ai
1 Star 0 Fork 0

tomcard/gotron-sdk

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
base58.go 1.16 KB
一键复制 编辑 原始数据 按行查看 历史
zzq 提交于 2021-04-02 17:07 +08:00 . first commit
package common
import (
"crypto/sha256"
"fmt"
"github.com/shengdoushi/base58"
)
func Encode(input []byte) string {
return base58.Encode(input, base58.BitcoinAlphabet)
}
func EncodeCheck(input []byte) string {
h256h0 := sha256.New()
h256h0.Write(input)
h0 := h256h0.Sum(nil)
h256h1 := sha256.New()
h256h1.Write(h0)
h1 := h256h1.Sum(nil)
inputCheck := input
inputCheck = append(inputCheck, h1[:4]...)
return Encode(inputCheck)
}
func Decode(input string) ([]byte, error) {
return base58.Decode(input, base58.BitcoinAlphabet)
}
func DecodeCheck(input string) ([]byte, error) {
decodeCheck, err := Decode(input)
if err != nil {
return nil, err
}
if len(decodeCheck) < 4 {
return nil, fmt.Errorf("b58 check error")
}
decodeData := decodeCheck[:len(decodeCheck)-4]
h256h0 := sha256.New()
h256h0.Write(decodeData)
h0 := h256h0.Sum(nil)
h256h1 := sha256.New()
h256h1.Write(h0)
h1 := h256h1.Sum(nil)
if h1[0] == decodeCheck[len(decodeData)] &&
h1[1] == decodeCheck[len(decodeData)+1] &&
h1[2] == decodeCheck[len(decodeData)+2] &&
h1[3] == decodeCheck[len(decodeData)+3] {
return decodeData, nil
}
return nil, fmt.Errorf("b58 check error")
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/kand_admin/gotron-sdk.git
git@gitee.com:kand_admin/gotron-sdk.git
kand_admin
gotron-sdk
gotron-sdk
044d4103d5c5

搜索帮助