mirror of
https://git.numenor-labs.us/dsfx.git
synced 2025-04-29 16:20:34 +00:00
32 lines
852 B
Go
32 lines
852 B
Go
|
package dcrypto
|
||
|
|
||
|
import (
|
||
|
"crypto/ecdh"
|
||
|
"crypto/rand"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
// DefaultDHCurve is the default elliptic curve used for signing.
|
||
|
DefaultDHCurve = ecdh.P384
|
||
|
)
|
||
|
|
||
|
// GenerateDHKey generates a new ECDH private key for key exchange.
|
||
|
func GenerateDHKey() (*ecdh.PrivateKey, error) {
|
||
|
return DefaultDHCurve().GenerateKey(rand.Reader)
|
||
|
}
|
||
|
|
||
|
// ComputeDHSecret computes the shared secret from the private key and the public key.
|
||
|
func ComputeDHSecret(priv *ecdh.PrivateKey, pub *ecdh.PublicKey) ([]byte, error) {
|
||
|
return priv.ECDH(pub)
|
||
|
}
|
||
|
|
||
|
// ExportDHPublicKey exports the public key as a byte slice.
|
||
|
func ExportDHPublicKey(pub *ecdh.PublicKey) ([]byte, error) {
|
||
|
return pub.Bytes(), nil
|
||
|
}
|
||
|
|
||
|
// ImportDHPublicKey imports the public key from a byte slice.
|
||
|
func ImportDHPublicKey(data []byte) (*ecdh.PublicKey, error) {
|
||
|
return DefaultDHCurve().NewPublicKey(data)
|
||
|
}
|