1 // Copyright (c) 2013-2014 Conformal Systems LLC.
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
4 // Modified by Juan Benet (juan@benet.ai)
12 "github.com/pkg/errors"
15 // alphabet is the modified base58 alphabet used by Bitcoin.
16 const BTCAlphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
17 const FlickrAlphabet = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"
19 var bigRadix = big.NewInt(58)
20 var bigZero = big.NewInt(0)
22 // Decode decodes a modified base58 string to a byte slice, using BTCAlphabet
23 func Decode(b string) ([]byte, error) {
24 return DecodeAlphabet(b, BTCAlphabet)
27 // Encode encodes a byte slice to a modified base58 string, using BTCAlphabet
28 func Encode(b []byte) string {
29 return EncodeAlphabet(b, BTCAlphabet)
32 // DecodeAlphabet decodes a modified base58 string to a byte slice, using alphabet.
33 func DecodeAlphabet(b, alphabet string) ([]byte, error) {
34 answer := big.NewInt(0)
37 for i := len(b) - 1; i >= 0; i-- {
38 tmp := strings.IndexAny(alphabet, string(b[i]))
40 return nil, errors.Errorf("Encountered unknown character: %s", string(b[i]))
42 idx := big.NewInt(int64(tmp))
46 answer.Add(answer, tmp1)
50 tmpval := answer.Bytes()
53 for numZeros = 0; numZeros < len(b); numZeros++ {
54 if b[numZeros] != alphabet[0] {
58 flen := numZeros + len(tmpval)
59 val := make([]byte, flen, flen)
60 copy(val[numZeros:], tmpval)
65 // Encode encodes a byte slice to a modified base58 string, using alphabet
66 func EncodeAlphabet(b []byte, alphabet string) string {
70 answer := make([]byte, 0, len(b)*136/100)
71 for x.Cmp(bigZero) > 0 {
73 x.DivMod(x, bigRadix, mod)
74 answer = append(answer, alphabet[mod.Int64()])
82 answer = append(answer, alphabet[0])
87 for i := 0; i < alen/2; i++ {
88 answer[i], answer[alen-1-i] = answer[alen-1-i], answer[i]