github.com/s7techlab/cckit@v0.10.5/extensions/ecdh/ecdh.go (about)

     1  package ecdh
     2  
     3  import (
     4  	"crypto/ecdsa"
     5  )
     6  
     7  func Marshall(pubKey *ecdsa.PublicKey) []byte {
     8  	byteLen := (pubKey.Curve.Params().BitSize + 7) >> 3
     9  
    10  	ret := make([]byte, 1+2*byteLen)
    11  	ret[0] = 4 // uncompressed point
    12  
    13  	xBytes := pubKey.X.Bytes()
    14  	copy(ret[1+byteLen-len(xBytes):], xBytes)
    15  	yBytes := pubKey.Y.Bytes()
    16  	copy(ret[1+2*byteLen-len(yBytes):], yBytes)
    17  	return ret
    18  
    19  }
    20  
    21  func GenerateSharedSecret(privKey *ecdsa.PrivateKey, pubKey *ecdsa.PublicKey) ([]byte, error) {
    22  	x, _ := pubKey.Curve.ScalarMult(pubKey.X, pubKey.Y, privKey.D.Bytes())
    23  	return x.Bytes(), nil
    24  }