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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| package main
import ( "crypto/sha256" "golang.org/x/crypto/ripemd160" "fmt" "math/big" "encoding/hex" )
var b58Alphabet = []byte("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz")
func Base58Encode(input []byte) []byte{ var result []byte
x:= big.NewInt(0).SetBytes(input)
base := big.NewInt(int64(len(b58Alphabet))) zero := big.NewInt(0)
mod := &big.Int{} for x.Cmp(zero) != 0 { x.DivMod(x,base,mod) result = append(result, b58Alphabet[mod.Int64()]) }
ReverseBytes(result)
for _,b:=range input{
if b ==0x00{ result = append([]byte{b58Alphabet[0]},result...) }else{ break } }
return result
}
func ReverseBytes(data []byte){ for i,j :=0,len(data) - 1;i<j;i,j = i+1,j - 1{ data[i],data[j] = data[j],data[i] } }
func generateAddress(pubkey []byte) []byte{ pubkeyHash256 :=sha256.Sum256(pubkey)
PIPEMD160Hasher := ripemd160.New()
_,err:= PIPEMD160Hasher.Write(pubkeyHash256[:])
if err!=nil{ fmt.Println("error") }
publicRIPEMD160 := PIPEMD160Hasher.Sum(nil)
versionPayload := append([]byte{0x00},publicRIPEMD160...)
firstSHA := sha256.Sum256(versionPayload) secondSHA := sha256.Sum256(firstSHA[:]) checksum:=secondSHA[:4]
fullPayload := append(versionPayload,checksum...)
address:=Base58Encode(fullPayload) return address }
func main(){ publickpey,_:=hex.DecodeString("D4A6C78C0B13DBD8A07AAB17C7D79ED9CB2523B63EDAC4E7CACE93C6B66CEDC7918EE0E174E8B2B61468D0E6CAA099710EF72094ACBD70BDAE3D8E42C617ACC6")
address:=generateAddress(publickpey)
fmt.Printf("%s",address) }
|