github.com/sandwich-go/boost@v1.3.29/xcrypto/key/curve25519/generator.go (about) 1 package curve25519 2 3 import ( 4 "crypto/rand" 5 "golang.org/x/crypto/curve25519" 6 ) 7 8 // GenerateSecretKey 生成密钥 SecretKey 9 func GenerateSecretKey() ([]byte, error) { 10 b := make([]byte, 32) 11 _, err := rand.Read(b) 12 if err != nil { 13 return nil, err 14 } 15 return b, nil 16 } 17 18 // GeneratePublicKey 通过密钥 SecretKey 生成公钥 PublicKey 19 func GeneratePublicKey(secretKey []byte) ([]byte, error) { 20 return curve25519.X25519(secretKey, curve25519.Basepoint) 21 } 22 23 // GenerateSharedKey 通过密钥 SecretKey 以及公钥 PublicKey 生成共享公钥 SharedKey 24 func GenerateSharedKey(secretKey, publicKey []byte) ([]byte, error) { 25 return curve25519.X25519(secretKey, publicKey) 26 }