forked from meling/urs
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathbase58.go
234 lines (193 loc) · 6.01 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright 2011 ThePiachu. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
//Subpackage for encoding data (namely Bitcoin Addresses) into base58 strings
import (
"encoding/hex"
"math/big"
)
//Useful materials:
//https://en.bitcoin.it/wiki/Base_58_Encoding
//http://www.strongasanox.co.uk/2011/03/11/base58-encoding-in-python/
//alphabet used by Bitcoins
var alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
//type to hold the Base58 string
type Base58 string
//reverse alphabet used for quckly converting base58 strings into numbers
var revalp = map[string]int{
"1": 0, "2": 1, "3": 2, "4": 3, "5": 4, "6": 5, "7": 6, "8": 7, "9": 8, "A": 9,
"B": 10, "C": 11, "D": 12, "E": 13, "F": 14, "G": 15, "H": 16, "J": 17, "K": 18, "L": 19,
"M": 20, "N": 21, "P": 22, "Q": 23, "R": 24, "S": 25, "T": 26, "U": 27, "V": 28, "W": 29,
"X": 30, "Y": 31, "Z": 32, "a": 33, "b": 34, "c": 35, "d": 36, "e": 37, "f": 38, "g": 39,
"h": 40, "i": 41, "j": 42, "k": 43, "m": 44, "n": 45, "o": 46, "p": 47, "q": 48, "r": 49,
"s": 50, "t": 51, "u": 52, "v": 53, "w": 54, "x": 55, "y": 56, "z": 57,
}
func Hex2Big(b []byte) *big.Int {
answer := big.NewInt(0)
for i := 0; i < len(b); i++ {
answer.Lsh(answer, 8)
answer.Add(answer, big.NewInt(int64(b[i])))
}
return answer
}
func String2Hex(s string) []byte {
answer, _ := hex.DecodeString(s)
return answer
}
//Convert base58 to big.Int
func (b Base58) ToBig() *big.Int {
answer := new(big.Int)
for i := 0; i < len(b); i++ {
answer.Mul(answer, big.NewInt(58)) //multiply current value by 58
answer.Add(answer, big.NewInt(int64(revalp[string(b[i:i+1])]))) //add value of the current letter
}
return answer
}
//convert base58 to int
func (b Base58) ToInt() int {
answer := 0
for i := 0; i < len(b); i++ {
answer *= 58 //multiply current value by 58
answer += revalp[string(b[i:i+1])] //add value of the current letter
}
return answer
}
//convert base58 to hex bytes
func (b Base58) ToHex() []byte {
value := b.ToBig() //convert to big.Int
oneCount := 0
for string(b)[oneCount] == '1' {
oneCount++
}
return append(make([]byte, oneCount), value.Bytes()...) //convert big.Int to bytes
}
func (b Base58) Base582Big() *big.Int {
answer := new(big.Int)
for i := 0; i < len(b); i++ {
answer.Mul(answer, big.NewInt(58)) //multiply current value by 58
answer.Add(answer, big.NewInt(int64(revalp[string(b[i:i+1])]))) //add value of the current letter
}
return answer
}
//convert base58 to int
func (b Base58) Base582Int() int {
answer := 0
for i := 0; i < len(b); i++ {
answer *= 58 //multiply current value by 58
answer += revalp[string(b[i:i+1])] //add value of the current letter
}
return answer
}
//convert base58 to hex bytes
func Base582Hex(b string) []byte {
return Base58(b).ToHex()
}
//convert base58 to hexes used by Bitcoins (keeping the zeroes on the front, 25 bytes long)
func (b Base58) BitHex() []byte {
value := b.ToBig() //convert to big.Int
tmp := value.Bytes() //convert to hex bytes
if len(tmp) == 25 { //if it is exactly 25 bytes, return
return tmp
} else if len(tmp) > 25 { //if it is longer than 25, return nothing
return nil
}
answer := make([]byte, 25) //make 25 byte container
for i := 0; i < len(tmp); i++ { //copy converted bytes
answer[24-i] = tmp[len(tmp)-1-i]
}
return answer
}
//encodes big.Int to base58 string
func Big2Base58(val *big.Int) Base58 {
answer := ""
valCopy := new(big.Int).Abs(val) //copies big.Int
if val.Cmp(big.NewInt(0)) <= 0 { //if it is less than 0, returns empty string
return Base58("")
}
tmpStr := ""
tmp := new(big.Int)
for valCopy.Cmp(big.NewInt(0)) > 0 { //converts the number into base58
tmp.Mod(valCopy, big.NewInt(58)) //takes modulo 58 value
valCopy.Div(valCopy, big.NewInt(58)) //divides the rest by 58
tmpStr += alphabet[tmp.Int64() : tmp.Int64()+1] //encodes
}
for i := (len(tmpStr) - 1); i > -1; i-- {
answer += tmpStr[i : i+1] //reverses the order
}
return Base58(answer) //returns
}
//encodes int to base58 string
func Int2Base58(val int) Base58 {
answer := ""
if val <= 0 { //if it is less than 0, returns empty string
return Base58("")
}
valCopy := val
tmpStr := ""
tmp := 0
for valCopy > 0 { //converts the number into base58
tmp = valCopy % 58 //takes modulo 58 value
valCopy /= 58 //divides the rest by 58
tmpStr += alphabet[tmp : tmp+1] //encodes
}
for i := (len(tmpStr) - 1); i > -1; i-- {
answer += tmpStr[i : i+1] //reverses the order
}
return Base58(answer) //returns
}
//encodes hex bytes into base58
func Hex2Base58(val []byte) Base58 {
tmp := Big2Base58(Hex2Big(val)) //encoding of the number without zeroes in front
//looking for zeros at the beggining
i := 0
for i = 0; val[i] == 0 && i < len(val); i++ {
}
answer := ""
for j := 0; j < i; j++ { //adds zeroes from the front
answer += alphabet[0:1]
}
answer += string(tmp) //concatenates
return Base58(answer) //returns
}
func Hex2Base58String(val []byte) string {
return string(Hex2Base58(val))
}
func Hex2Base58Str(val []byte) string {
return string(Hex2Base58(val))
}
//encodes string stored hex bytes into base58
func StringHex2Base58(val string) Base58 {
tmp := Big2Base58(Hex2Big(String2Hex(val))) //encoding of the number without zeroes in front
//looking for zeros at the beggining
i := 0
for i = 0; val[i:i+2] == string("00") && i < len(val)-2; i += 2 {
}
i /= 2
answer := ""
for j := 0; j < i; j++ {
answer += alphabet[0:1]
}
answer += string(tmp)
return Base58(answer)
}
func StrHex2Base58(val string) Base58 {
return StringHex2Base58(val)
}
func String2Base58(val string) Base58 {
var answer Base58
for i := 0; i < len(val); i++ {
_, err := revalp[val[i:i+1]]
if err == false {
return answer
}
}
answer = Base58(val)
return answer
}
func Str2Hex58(val string) Base58 {
return String2Base58(val)
}
//TODO: do and test everything
func TestBase58() {
}