-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase58.go
50 lines (42 loc) · 1.12 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
package radix
import (
"errors"
"unicode/utf8"
)
var ErrBase58BadAlphabet = errors.New("base58: alphabet length less than 58")
const AlphabetBitcoin = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
const AlphabetRipple = "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz"
const AlphabetFlickr = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"
func Base58Encode(input []byte, alphabet string) (string, error) {
if utf8.RuneCountInString(alphabet) < 58 {
return "", ErrBase58BadAlphabet
}
input58, err := ConvertBytes(input, 58)
if err != nil {
return "", err
}
zeror, _ := utf8.DecodeRuneInString(alphabet)
zero := string(zeror)
padding := ""
for _, in := range input {
if in == 0 {
padding += zero
} else {
break
}
}
output, err := EncodeBytes(input58, alphabet)
if err != nil {
return "", err
}
if len(padding) == 0 {
return output, nil
}
return padding + output, nil
}
func Base58Decode(input string, alphabet string) ([]byte, error) {
if utf8.RuneCountInString(alphabet) < 58 {
return nil, ErrBase58BadAlphabet
}
return DecodeBytes(input, alphabet)
}