httpx

package module
v0.0.0-...-94d6cc8 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2025 License: MIT Imports: 13 Imported by: 0

README

httpx

APISign API参数签名验证

    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
    1. 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
    1. rawStr+timestamp => a=2&n=m1626167650 (1626167650 is unix timestamp), verify sign time valid(default 10s)
    1. Sign Method: Method(rawStr+timestamp, secretKey) signed text encode [Base64, Hex(default)] // Method=[HMAC-SHA256,HMAC-SHA1] Encode=[Base64,Hex] Default = HMAC-SHA256-HEX
    1. default: signStr=Hex(HMAC-SHA256(rawStr+timestamp,secretKey))
    1. Sign http request Header X-Signature=accessKey:signStr:timestamp (: split elem)
USEAGE

see test example

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APISign

type APISign struct {
	AccessKey    string
	AccessSecret string
	TTL          int64
	Method
}

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(accessKey, accessSecret string, ttl int64, method Method) *APISign

func (*APISign) Sign

func (p *APISign) Sign(req *http.Request, deadline int64) (string, error)

Sign sign and return signature,deadline is validity period of signature

func (*APISign) ToSignRaw

func (p *APISign) ToSignRaw(req *http.Request) (string, error)

ToSignRaw 从req解析并生成已排序待签名参数字符串 如果contentType是JSON,只取JSON参数 其他:取URL PARAMS和BODY PARAMS

func (*APISign) Verify

func (p *APISign) Verify(req *http.Request, authHeaderName string) error

Verify param sign result verify req:the http.Request authHeaderName:header name,like X-Signature,value:accessKey:signature:deadline

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"
)

Jump to

Keyboard shortcuts

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