Một số thao tác đơn giản với chuẩn secp256k1 được dùng trong Bitcoin, Ethereum với golang 

  1. Blockchain

package main

import (

    "crypto/rand"

    "crypto/elliptic"

    "crypto/ecdsa"

    "github.com/ethereum/go-ethereum/crypto"

    "encoding/hex"

    "fmt"

    "math/big"

)



func PrivateKeyFromString(str string) (*ecdsa.PrivateKey , error) {

    aKey := new (ecdsa.PrivateKey)

    aKey.Curve = crypto.S256();

    aKey.D = new(big.Int)

    aKey.D.SetString(str, 16)


    aKey.PublicKey.X, aKey.PublicKey.Y = aKey.Curve.ScalarBaseMult(aKey.D.Bytes())

    return aKey, nil

    

}


func main(){

    

    key, _ := crypto.GenerateKey()


    // Get the address

    address := crypto.PubkeyToAddress(key.PublicKey).Hex()

    // 0x8ee3333cDE801ceE9471ADf23370c48b011f82a6


    // Get the private key

    privateKey := hex.EncodeToString(key.D.Bytes())

    // 05b14254a1d0c77a49eae3bdf080f926a2df17d8e2ebdf7af941ea001481e57f


    fmt.Println("key: ",key);

    fmt.Println("Address: ", address)

    fmt.Println("PrivateKey: ", privateKey)

    fmt.Println("PublickKey: ", key.PublicKey);


    PriKey, _ := PrivateKeyFromString(privateKey)


    fmt.Println("Org Key", key);

    fmt.Println("Recoverd Key", PriKey);

}

Từ khóa: 

sample_code

,

secp256k1

,

blockchain