代码拉取完成,页面将自动刷新
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")
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。