ecdh

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 6, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package ecdh 提供椭圆曲线 Diffie-Hellman 密钥协商(基于 crypto/ecdh)。

双方各自生成密钥对、交换公钥,即可各自算出相同的共享密钥。默认使用 X25519。

注意:协商出的共享密钥不应直接用作对称加密密钥,应再经 hkdf 等 KDF 派生。

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(curve ecdh.Curve) (*ecdh.PrivateKey, error)

Generate 使用指定曲线生成密钥对(如 ecdh.P256()、ecdh.X25519())。

func GenerateX25519

func GenerateX25519() (*ecdh.PrivateKey, error)

GenerateX25519 生成一对 X25519 密钥。

func ParsePrivateKey

func ParsePrivateKey(curve ecdh.Curve, b []byte) (*ecdh.PrivateKey, error)

ParsePrivateKey 用指定曲线从字节解析私钥。

func ParsePublicKey

func ParsePublicKey(curve ecdh.Curve, b []byte) (*ecdh.PublicKey, error)

ParsePublicKey 用指定曲线从字节解析对端公钥。

func SharedSecret

func SharedSecret(priv *ecdh.PrivateKey, peerPub *ecdh.PublicKey) ([]byte, error)

SharedSecret 用本方私钥与对端公钥计算共享密钥。

Example
package main

import (
	"fmt"

	"github.com/gtkit/encry/ecdh"
)

func main() {
	alice, _ := ecdh.GenerateX25519()
	bob, _ := ecdh.GenerateX25519()

	a, _ := ecdh.SharedSecret(alice, bob.PublicKey())
	b, _ := ecdh.SharedSecret(bob, alice.PublicKey())
	fmt.Println(string(a) == string(b))
}
Output:
true

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL