base58
Base58是用于Bitcoin中使用的一种独特的编码方式,主要用于产生Bitcoin的钱包地址。相比Base64,Base58不使用数字"0",字母大写"O",字母大写"I",和字母小写"l",以及"+“和”/"符号。
成都创新互联长期为上千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为万宁企业提供专业的
成都做网站、成都网站建设,
万宁网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。
设计Base58主要的目的是:
避免混淆。在某些字体下,数字0和字母大写O,以及字母大写I和字母小写l会非常相似。
不使用"+“和”/"的原因是非字母或数字的字符串作为帐号较难被接受。
没有标点符号,通常不会被从中间分行。
大部分的软件支持双击选择整个字符串。
base58编码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| package main
import ( "math/big" "fmt" )
//切片存储base58字母 var b58Alphabet = []byte("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz")
func Base58Encode(input []byte) []byte{ //定义一个字节切片,返回值 var result []byte
//把字节数组input转化为了大整数big.Int x:= big.NewInt(0).SetBytes(input)
//长度58的大整数 base := big.NewInt(int64(len(b58Alphabet))) //0的大整数 zero := big.NewInt(0) //大整数的指针 mod := &big.Int{}
//循环,不停地对x取余数,大小为58 for x.Cmp(zero) != 0 { x.DivMod(x,base,mod) // 对x取余数
//讲余数添加到数组当中 result = append(result, b58Alphabet[mod.Int64()]) }
//反转字节数组 ReverseBytes(result)
//如果这个字节数组的前面为字节0,会把它替换为1. for _,b:=range input{
if b ==0x00{ result = append([]byte{b58Alphabet[0]},result...) }else{ break } }
return result
}
//反转字节数组 func ReverseBytes(data []byte){ for i,j :=0,len(data) - 1;idata[i],data[j] = data[j],data[i] } }
//测试 反转操作 func main(){ org := []byte("qwerty") fmt.Println(string(org))
ReverseBytes(org)
fmt.Println(string(org)) //测试编码 fmt.Printf("%s",string( Base58Encode([]byte("hello jonson")))) }
|
解码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| func Base58Decode(input []byte) []byte{ result := big.NewInt(0) zeroBytes :=0 for _,b :=range input{ if b=='1'{ zeroBytes++ }else{ break } }
payload:= input[zeroBytes:]
for _,b := range payload{ charIndex := bytes.IndexByte(b58Alphabet,b) //反推出余数
result.Mul(result,big.NewInt(58)) //之前的结果乘以58
result.Add(result,big.NewInt(int64(charIndex))) //加上这个余数
}
decoded :=result.Bytes()
decoded = append(bytes.Repeat([]byte{0x00},zeroBytes),decoded...) return decoded }
|
完整代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| package main
import ( "math/big" "fmt" "bytes" )
var b58Alphabet = []byte("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz")
func Base58Encode(input []byte) []byte{ var result []byte
x:= big.NewInt(0).SetBytes(input)
base := big.NewInt(int64(len(b58Alphabet))) zero := big.NewInt(0)
mod := &big.Int{} for x.Cmp(zero) != 0 { x.DivMod(x,base,mod) // 对x取余数 result = append(result, b58Alphabet[mod.Int64()]) }
ReverseBytes(result)
for _,b:=range input{
if b ==0x00{ result = append([]byte{b58Alphabet[0]},result...) }else{ break } }
return result
}
func Base58Decode(input []byte) []byte{ result := big.NewInt(0) zeroBytes :=0 for _,b :=range input{ if b=='1'{ zeroBytes++ }else{ break } }
payload:= input[zeroBytes:]
for _,b := range payload{ charIndex := bytes.IndexByte(b58Alphabet,b) //反推出余数
result.Mul(result,big.NewInt(58)) //之前的结果乘以58
result.Add(result,big.NewInt(int64(charIndex))) //加上这个余数
}
decoded :=result.Bytes()
decoded = append(bytes.Repeat([]byte{0x00},zeroBytes),decoded...) return decoded }
func ReverseBytes(data []byte){ for i,j :=0,len(data) - 1;idata[i],data[j] = data[j],data[i] } }
func main(){ org := []byte("qwerty") fmt.Println(string(org))
ReverseBytes(org)
fmt.Println(string(org))
fmt.Printf("%s\n",string( Base58Encode([]byte("hello jonson"))))
fmt.Printf("%s",string(Base58Decode([]byte("2yGEbwRFyav6CimZ7")))) }
|
参考资料
(比特币wiki-base58编码)[https://en.bitcoin.it/wiki/Base58Check_encoding#Version_bytes]
(维基百科-base58编码)[https://zh.wikipedia.org/wiki/Base58]
![golang[32]-区块链-base58](/upload/otherpic17/238788.jpg)
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
分享题目:golang[32]-区块链-base58-创新互联
文章位置:
http://cdkjz.cn/article/cecjdo.html