Implement Base58Decode
This commit is contained in:
parent
caf71744f5
commit
4805ce1bdb
25
base58.go
25
base58.go
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue