Implement Base58Decode

This commit is contained in:
Ivan Kuznetsov 2017-09-07 20:47:16 +07:00
parent caf71744f5
commit 4805ce1bdb
1 changed files with 25 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package main
import (
"bytes"
"math/big"
)
@ -33,3 +34,27 @@ func Base58Encode(input []byte) []byte {
return result
}
// Base58Decode decodes Base58 data
func Base58Decode(input []byte) []byte {
result := big.NewInt(0)
zeroBytes := 0
for c := range input {
if c == 0x00 {
zeroBytes++
}
}
address := input[zeroBytes:]
for _, b := range address {
charIndex := bytes.IndexByte(alphabet, b)
result.Mul(result, big.NewInt(58))
result.Add(result, big.NewInt(int64(charIndex)))
}
raw := result.Bytes()
raw = append(bytes.Repeat([]byte{byte(0x00)}, zeroBytes), raw...)
return raw
}