Documentation
¶
Overview ¶
Package hpke 提供混合公钥加密 HPKE(RFC9180,基于 go1.26 的 crypto/hpke)。
只持有接收方公钥即可加密;解密需对应私钥。相比"用 RSA 直接加密大数据"的反模式, HPKE 内部用 KEM 协商对称密钥再做 AEAD,没有明文长度限制、且更安全。
默认套件:DHKEM(X25519, HKDF-SHA256) + HKDF-SHA256 + ChaCha20-Poly1305。 info 是可选的上下文绑定(域分隔):Seal 与 Open 必须使用相同的 info。
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateKeyPair ¶
func GenerateKeyPair() (*ecdh.PrivateKey, error)
GenerateKeyPair 生成一对 X25519 密钥用于 HPKE。
func ParsePublicKey ¶
ParsePublicKey 从字节解析 X25519 公钥。
func Seal ¶
Seal 用接收方公钥加密明文,返回 Base64(封装密钥 enc 与密文的拼接)。
Example ¶
package main
import (
"fmt"
"github.com/gtkit/encryx/hpke"
)
func main() {
priv, _ := hpke.GenerateKeyPair()
// 发送方只需接收方公钥。
enc, _ := hpke.Seal(priv.PublicKey(), []byte("ctx"), []byte("hello"))
// 接收方用私钥解密。
got, _ := hpke.Open(priv, []byte("ctx"), enc)
fmt.Println(string(got))
}
Output: hello
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.