nsign

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2024 License: MIT Imports: 9 Imported by: 1

README

NSign

签名及签名验证,主要用于网络请求参数签名和签名验证。

安装

$ go get github.com/smartwalle/nsign

使用

参考 test 文件

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrVerification = errors.New("verification error")

Functions

This section is empty.

Types

type DefaultEncoder

type DefaultEncoder struct {
}

func (*DefaultEncoder) EncodeBytes

func (encoder *DefaultEncoder) EncodeBytes(buffer *bytes.Buffer, data []byte, opts *SignOptions) ([]byte, error)

func (*DefaultEncoder) EncodeValues

func (encoder *DefaultEncoder) EncodeValues(buffer *bytes.Buffer, values url.Values, opts *SignOptions) ([]byte, error)

EncodeValues 1、将参数名及其对应的值进行升序排序 2、将排序后的参数名及参数名使用等号进行连接,例如:a=10 3、将组合之后的参数使用&号进行连接,例如:a=10&b=20&c=30&c=31

type Encoder

type Encoder interface {
	EncodeValues(buffer *bytes.Buffer, values url.Values, opts *SignOptions) ([]byte, error)

	EncodeBytes(buffer *bytes.Buffer, data []byte, opts *SignOptions) ([]byte, error)
}

type HashMethod

type HashMethod struct {
	// contains filtered or unexported fields
}

func NewHashMethod

func NewHashMethod(h crypto.Hash) *HashMethod

func (*HashMethod) Sign

func (method *HashMethod) Sign(data []byte) ([]byte, error)

func (*HashMethod) Verify

func (method *HashMethod) Verify(data []byte, signature []byte) error

type Method

type Method interface {
	Sign(data []byte) ([]byte, error)

	Verify(data []byte, signature []byte) error
}

type Option

type Option func(signer *signer)

func WithEncoder

func WithEncoder(encoder Encoder) Option

func WithMethod

func WithMethod(method Method) Option

type RSAMethod

type RSAMethod struct {
	// contains filtered or unexported fields
}

func NewRSAMethod

func NewRSAMethod(h crypto.Hash, privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey) *RSAMethod

func (*RSAMethod) Sign

func (method *RSAMethod) Sign(data []byte) ([]byte, error)

func (*RSAMethod) Verify

func (method *RSAMethod) Verify(data []byte, signature []byte) error

type SignOption

type SignOption func(opt *SignOptions)

func WithIgnore

func WithIgnore(keys ...string) SignOption

func WithPrefix

func WithPrefix(s string) SignOption

func WithSuffix

func WithSuffix(s string) SignOption

type SignOptions

type SignOptions struct {
	Prefix  string
	Suffix  string
	Ignores map[string]struct{}
}

type Signer

type Signer interface {
	SignValues(values url.Values, opts ...SignOption) ([]byte, error)

	SignBytes(data []byte, opts ...SignOption) ([]byte, error)

	VerifyValues(values url.Values, signature []byte, opts ...SignOption) error

	VerifyBytes(data []byte, signature []byte, opts ...SignOption) error
}

func New

func New(opts ...Option) Signer

Jump to

Keyboard shortcuts

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