sign

package
v0.3.11 Latest Latest
Warning

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

Go to latest
Published: May 16, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeHeaderVal added in v0.1.3

func DecodeHeaderVal(headerVal string) (accessKey, signStr, timestamp string, err error)

DecodeHeaderVal header value decode to accessKey:signStr:timestamp

func HmacHash

func HmacHash(method Method, rawStr, secretKey string) string

HmacHash hash and encode

func HmacSha1ToBase64

func HmacSha1ToBase64(rawStr, key string) string

HmacSha1ToBase64 HMAC-SHA1

func HmacSha1ToHex

func HmacSha1ToHex(rawStr, key string) string

HmacSha1ToHex

func HmacSha256ToBase64

func HmacSha256ToBase64(rawStr, key string) string

HmacSha256ToBase64 HMAC-SHA256

func HmacSha256ToHex

func HmacSha256ToHex(rawStr, key string) string

HmacSha256ToHex

func SignedValidTime added in v0.1.3

func SignedValidTime(t time.Time) error

SignedValidTime signed TTL verify

func SortParamForm

func SortParamForm(req *http.Request) (string, error)

SortParamForm sort and format URL | form-data param

Types

type APISign

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

APISign API Param Sign

  1. rawStr eg: GET http://example.com/hello?n=1&a=2 Key["n","a"]-ASC Sort["a","n"] GetParam(a) a=2&n=1 param key string attaches the methods
  2. other request http method,for Content-Type: application/json {"n":"m","a":2} Key ASC Sort,param key string attaches the methods => {"a":2,"n":"m"} => a=2&n=m
  3. rawStr+timestamp => a=2&n=m1626167650 (1626167650 is unix timestamp), verify sign time valid(default 10s)
  4. Sign Method: Method(rawStr+timestamp, secretKey) signed text encode [Base64, Hex(default)] Method=[HMAC-SHA256,HMAC-SHA1] Encode=[Base64,Hex] Default = HMAC-SHA256-HEX
  5. default: signStr=Hex(HMAC-SHA256(rawStr+timestamp,secretKey))
  6. Sign http request Header X-Signature=accessKey:signStr:timestamp (: split elem)

func NewAPISign

func NewAPISign(cfg *Config) *APISign

NewAPISign api param sign

func (*APISign) SetGetSecretKey

func (sign *APISign) SetGetSecretKey(f func(accessKey string) (string, error))

SetGetSecretKey setting SecretKey get function

func (*APISign) Verify

func (sign *APISign) Verify(req *http.Request, header string) error

Verify param sign result verify

type Config added in v0.1.0

type Config struct {
	// Sign TTL
	SignValidDuration time.Duration `defval:"10s"`
	Method            `defval:"HMAC-SHA256-HEX"`
}

type Kv

type Kv struct {
	Key   string
	Value interface{}
}

type KvSlice

type KvSlice []Kv

func (KvSlice) Len

func (s KvSlice) Len() int

func (KvSlice) Less

func (s KvSlice) Less(i, j int) bool

func (KvSlice) Swap

func (s KvSlice) Swap(i, j int)

type Method

type Method string
const (
	HmacSha256    Method = "HMAC-SHA256-BASE64"
	HmacSha1      Method = "HMAC-SHA1-BASE64"
	HmacSha1Hex   Method = "HMAC-SHA1-HEX"
	HmacSha256Hex Method = "HMAC-SHA256-HEX"
)

type RequestBodyMap

type RequestBodyMap map[string]interface{}

func (RequestBodyMap) GetStringValue

func (r RequestBodyMap) GetStringValue(key string) (string, error)

func (RequestBodyMap) SortToString

func (r RequestBodyMap) SortToString(separator string) (string, error)

SortToString request body param sort format

func (RequestBodyMap) TrimNewline

func (r RequestBodyMap) TrimNewline(buf []byte) []byte

Jump to

Keyboard shortcuts

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