/
base58.go
69 lines (64 loc) · 1.61 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
package BLC
import (
"bytes"
"math/big"
)
//base58编码实现
//1.生成一个base58的编码基数表
var b58Alphabet=[]byte(""+
"123456789"+
"abcdefghilkmnopqrstuvwxyz"+
"ABCDEFGHJKLMNPQRSTUVWXYZ")
//编码函数
func Base58Encode(input []byte) []byte {
var result []byte //结果
//big.int
//byte字节数组转换为big.int
x:=big.NewInt(0).SetBytes(input)
//求余的基本长度
base:=big.NewInt(int64(len(b58Alphabet)))
//求余数和商
//判断条件,除掉的最终结果是否为0
zero:=big.NewInt(0)
//设置余数,代表base58技术表的索引位置
mod:=&big.Int{}
for x.Cmp(zero)!=0{
x.DivMod(x,base,mod)
//得到的result是一个倒叙的base58编码结果
result=append(result,b58Alphabet[mod.Int64()])
}
//返回result切片
Reverse(result)
//添加一个前缀1,代表是一个地址
result=append([]byte{b58Alphabet[0]},result...)
return result
}
//反转切片函数
func Reverse(data []byte) {
for i,j:=0,len(data)-1;i<j;i,j=i+1,j-1{
data[i],data[j]=data[j],data[i]
}
}
//解码函数
//input:base58编码结果
/*
Jh83
*/
func Base58Decode(input[]byte) []byte {
result:=big.NewInt(0)
//前缀索引
zeroBytes:=1
//1.去掉前缀
data:=input[zeroBytes:]
for _,b:=range data{
//2.查找input中指定数字/字符在基数表中出现的索引(mod)
charIndex:=bytes.IndexByte(b58Alphabet,b) //内部函数,返回字符在切片中第一次出现的索引
//3.余数*58
result.Mul(result,big.NewInt(58))
//4.乘积结果+mod(索引)
result.Add(result,big.NewInt(int64(charIndex)))
}
//5.转换为byte字节数组
decoded:=result.Bytes()
return decoded
}