method

package
v0.3.68 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2026 License: LGPL-2.1 Imports: 26 Imported by: 1

Documentation

Overview

Package method 提供 GTP 使用的密码学与压缩基础能力。

这个包包含哈希、HMAC、填充、压缩流、对称加密、非对称签名、 命名曲线等能力的工厂和适配层,供握手和消息处理流程使用。

Index

Constants

This section is empty.

Variables

View Source
var (
	// GzipCompressionLevel 是新建 Gzip 写入器使用的压缩级别。
	GzipCompressionLevel = gzip.DefaultCompression
	// DeflateCompressionLevel 是新建 Deflate 写入器使用的压缩级别。
	DeflateCompressionLevel = flate.DefaultCompression
	// BrotliCompressionLevel 是新建 Brotli 写入器使用的压缩级别。
	BrotliCompressionLevel = brotli.DefaultCompression
	// LZ4CompressionLevel 是新建 LZ4 写入器使用的压缩级别。
	LZ4CompressionLevel = lz4.Level4
)
View Source
var (
	// ErrInvalidMethod 表示请求的算法或算法组合不受支持。
	ErrInvalidMethod = errors.New("gtp: invalid method")
)

Functions

func NewCipherBlock added in v0.3.58

func NewCipherBlock(se gtp.SymmetricEncryption, key []byte) (block cipher.Block, err error)

NewCipherBlock 创建底层分组密码,并将构造 panic 转换为错误。

func NewHMAC added in v0.3.63

func NewHMAC(h gtp.Hash, key []byte) (hash.Hash, error)

NewHMAC 使用指定摘要算法和密钥创建消息认证码;不支持的摘要返回 ErrInvalidMethod。

func NewNamedCurve

func NewNamedCurve(nc gtp.NamedCurve) (ecdh.Curve, error)

NewNamedCurve 返回指定 ECDH 命名曲线;不支持的曲线返回 ErrInvalidMethod。

Types

type Cipher

type Cipher interface {
	// Transforming 使用 nonce 将 src 变换到 dst,并返回输出字节数。
	Transforming(dst, src, nonce []byte) (int, error)
	// BlockSize 返回分组密码块大小;非分组变换返回零。
	BlockSize() int
	// NonceSize 返回每次变换要求的 nonce 字节数。
	NonceSize() int
	// Overhead 返回认证加密相对明文增加的字节数。
	Overhead() int
	// Pad 报告变换前是否需要填充输入。
	Pad() bool
	// Unpad 报告变换后是否需要去除填充。
	Unpad() bool
	// InputSize 返回容纳 size 字节输入所需的缓冲区大小。
	InputSize(size int) int
	// OutputSize 返回 size 字节输入对应的输出缓冲区大小。
	OutputSize(size int) int
}

Cipher 是有状态的对称加密或解密变换,不支持并发调用。

func NewBlockCipherMode

func NewBlockCipherMode(bcm gtp.BlockCipherMode, block cipher.Block, iv []byte) (encryptor, decrypter Cipher, err error)

NewBlockCipherMode 为分组密码创建成对的加密和解密变换,并将构造 panic 转换为错误。

func NewCipher

func NewCipher(se gtp.SymmetricEncryption, bcm gtp.BlockCipherMode, key, iv, nonce []byte) (encryptor, decrypter Cipher, err error)

NewCipher 根据算法和模式创建相互独立的加密器与解密器。

type CompressionStream

type CompressionStream interface {
	// WrapReader 将输入包装为可读取的解压流。
	WrapReader(r io.Reader) (io.Reader, error)
	// WrapWriter 将输出包装为必须关闭的压缩流。
	WrapWriter(w io.Writer) (io.WriteCloser, error)
}

CompressionStream 复用指定算法的压缩与解压写入器,不支持并发调用。

func NewCompressionStream

func NewCompressionStream(c gtp.Compression) (CompressionStream, error)

NewCompressionStream 创建可复用的压缩流;不支持的算法返回 ErrInvalidMethod。

type Padding

type Padding interface {
	// Pad 将 buf[ori:] 写成填充数据。
	Pad(buf []byte, ori int) error
	// Unpad 验证并返回去除填充后的切片视图。
	Unpad(padded []byte) ([]byte, error)
}

Padding 在预分配缓冲区中添加或验证分组密码填充。

func NewPadding

func NewPadding(pm gtp.PaddingMode) (Padding, error)

NewPadding 创建 PKCS#7 或 ANSI X9.23 填充器。

type Signer

type Signer interface {
	// GenerateKey 生成与签名配置匹配的私钥。
	GenerateKey() (crypto.PrivateKey, error)
	// Sign 使用私钥签名 data。
	Sign(priv crypto.PrivateKey, data []byte) ([]byte, error)
	// Verify 使用公钥验证 data 和 sig。
	Verify(pub crypto.PublicKey, data, sig []byte) error
}

Signer 生成密钥并对握手数据进行非对称签名和验证。

func NewSigner

func NewSigner(ae gtp.AsymmetricEncryption, padding gtp.PaddingMode, hash gtp.Hash) (Signer, error)

NewSigner 创建 RSA 或 ECDSA 签名器,并校验填充与摘要组合。

Jump to

Keyboard shortcuts

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