-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbase58check.go
129 lines (106 loc) · 2.86 KB
/
base58check.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
package base58check
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"errors"
"math/big"
"reflect"
)
const alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
// Encode encodes the given version and data to a base58check encoded string
func Encode(version, data string) (string, error) {
prefix, err := hex.DecodeString(version)
if err != nil {
return "", err
}
dataBytes, err := hex.DecodeString(data)
if err != nil {
return "", err
}
dataBytes = append(prefix, dataBytes...)
// Performing SHA256 twice
sha256hash := sha256.New()
sha256hash.Write(dataBytes)
middleHash := sha256hash.Sum(nil)
sha256hash = sha256.New()
sha256hash.Write(middleHash)
hash := sha256hash.Sum(nil)
checksum := hash[:4]
dataBytes = append(dataBytes, checksum...)
// For all the "00" versions or any prepended zeros as base58 removes them
zeroCount := 0
for i := 0; i < len(dataBytes); i++ {
if dataBytes[i] == 0 {
zeroCount++
} else {
break
}
}
// Performing base58 encoding
encoded := b58encode(dataBytes)
for i := 0; i < zeroCount; i++ {
encoded = "1" + encoded
}
return encoded, nil
}
// Decode decodes the given base58check encoded string and returns the version prepended decoded string
func Decode(encoded string) (string, error) {
zeroCount := 0
for i := 0; i < len(encoded); i++ {
if encoded[i] == 49 {
zeroCount++
} else {
break
}
}
dataBytes, err := b58decode(encoded)
if err != nil {
return "", err
}
dataBytesLen := len(dataBytes)
if dataBytesLen <= 4 {
return "", errors.New("base58check data cannot be less than 4 bytes")
}
data, checksum := dataBytes[:dataBytesLen-4], dataBytes[dataBytesLen-4:]
for i := 0; i < zeroCount; i++ {
data = append([]byte{0}, data...)
}
// Performing SHA256 twice to validate checksum
sha256hash := sha256.New()
sha256hash.Write(data)
middleHash := sha256hash.Sum(nil)
sha256hash = sha256.New()
sha256hash.Write(middleHash)
hash := sha256hash.Sum(nil)
if !reflect.DeepEqual(checksum, hash[:4]) {
return "", errors.New("Data and checksum don't match")
}
return hex.EncodeToString(data), nil
}
func b58encode(data []byte) string {
var encoded string
decimalData := new(big.Int)
decimalData.SetBytes(data)
divisor, zero := big.NewInt(58), big.NewInt(0)
for decimalData.Cmp(zero) > 0 {
mod := new(big.Int)
decimalData.DivMod(decimalData, divisor, mod)
encoded = string(alphabet[mod.Int64()]) + encoded
}
return encoded
}
func b58decode(data string) ([]byte, error) {
decimalData := new(big.Int)
alphabetBytes := []byte(alphabet)
multiplier := big.NewInt(58)
for _, value := range data {
pos := bytes.IndexByte(alphabetBytes, byte(value))
if pos == -1 {
return nil, errors.New("Character not found in alphabet")
}
decimalData.Mul(decimalData, multiplier)
decimalData.Add(decimalData, big.NewInt(int64(pos)))
}
return decimalData.Bytes(), nil
}