hpke

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: MIT Imports: 4 Imported by: 0

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 Open

func Open(priv *ecdh.PrivateKey, info []byte, cipherText string) ([]byte, error)

Open 用接收方私钥解密 Seal 产生的密文。info 必须与加密时一致。

func ParsePublicKey

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

ParsePublicKey 从字节解析 X25519 公钥。

func Seal

func Seal(pub *ecdh.PublicKey, info, plainText []byte) (string, error)

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.

Jump to

Keyboard shortcuts

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