-
Notifications
You must be signed in to change notification settings - Fork 0
/
base58.go
66 lines (53 loc) · 1.4 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
package main
import (
"bytes"
"math/big"
)
/*
Base256-to-Base58 conversion (treat both quantities like big-endian).
*/
// NOTE: this alphabet did not include:
// `0` (zero), `O` (`o` uppercase), `I` (`i` uppercase), `l` (`l` lowercase).
var alphabet = []byte("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz")
// base58Encode returns a byte slice in base58 encoding.
func base58Encode(input []byte) []byte {
var result []byte
// x := the `big.Int` of a big-endian bytes input.
x := big.NewInt(0).SetBytes(input)
base58 := big.NewInt(int64(len(alphabet)))
zero := big.NewInt(0)
// mod := modulus of the arithmetric.
mod := &big.Int{}
for x.Cmp(zero) != 0 {
x.DivMod(x, base58, mod)
result = append(result, alphabet[mod.Int64()])
}
reverseBytes(result)
for b := range input {
if b == 0x00 {
result = append([]byte{alphabet[0]}, result...)
} else {
break
}
}
return result
}
// base58Decode decodes base58-encoded data.
func base58Decode(input []byte) []byte {
result := big.NewInt(0)
zeroBytes := 0
for b := range input {
if b == 0x00 {
zeroBytes++
}
}
payload := input[zeroBytes:]
for _, b := range payload {
charIndex := bytes.IndexByte(alphabet, b)
result.Mul(result, big.NewInt(58))
result.Add(result, big.NewInt(int64(charIndex)))
}
decoded := result.Bytes()
decoded = append(bytes.Repeat([]byte{byte(0x00)}, zeroBytes), decoded...)
return decoded
}