-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbase58.go
81 lines (58 loc) · 1.57 KB
/
base58.go
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
package main
import (
"bytes"
"math/big"
)
//切片存储base58字母
var b58Alphabet = []byte("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz")
//把普通的字节数组转换成base58编码的字节数组
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 Base58Decode(input []byte) []byte{
result := big.NewInt(0)
zeroBytes :=0
for _,b :=range input{
if b=='1'{
zeroBytes++
}else{
break
}
}
payload:= input[zeroBytes:]
//这个乘58+余数的方法太巧妙了
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
}