sign

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: MIT Imports: 14 Imported by: 2

README

Aliyun api gateway request signature algorithm implemented by go

English简体中文

Github License Go Doc Go Report Github Latest Release Github Latest Tag Github Stars

For signature methods, see: using digital signature authentication to call api

Installation

Download package by using:

$ go get github.com/sliveryou/aliyun-api-gateway-sign

Usage Example

package main

import (
	"io/ioutil"
	"log"
	"net/http"
	"net/http/httputil"
	"strings"

	sign "github.com/sliveryou/aliyun-api-gateway-sign"
)

func main() {
	var url string = "https://bankcard4c.shumaidata.com/bankcard4c"
	var body string
	var appKey, appKeySecret string

	// Prepare a HTTP request.
	req, err := http.NewRequest(sign.HTTPMethodPost, url, strings.NewReader(body))
	if err != nil {
		// Handle err.
		panic(err)
	}
	// Set the request with headers.
	req.Header.Set(sign.HTTPHeaderAccept, sign.HTTPContentTypeJson)
	req.Header.Set(sign.HTTPHeaderContentType, sign.HTTPContentTypeJson)

	// Sign the request.
	if err := sign.Sign(req, appKey, appKeySecret); err != nil {
		panic(err)
	}

	// Show the dump request.
	dumpReq, err := httputil.DumpRequestOut(req, true)
	if err != nil {
		panic(err)
	}
	log.Println("\n" + string(dumpReq))

	// Do the request.
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	// Handle response.
	content, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}
	log.Println("\n"+string(content), resp.StatusCode, resp.Header.Get("X-Ca-Error-Message"))
}

Documentation

Index

Constants

View Source
const (
	HTTPHeaderAccept      = "Accept"
	HTTPHeaderContentMD5  = "Content-MD5"
	HTTPHeaderContentType = "Content-Type"
	HTTPHeaderDate        = "Date"
	HTTPHeaderUserAgent   = "User-Agent"
)

HTTP header keys.

View Source
const (
	HTTPHeaderCAPrefix           = "X-Ca-"
	HTTPHeaderCAKey              = "X-Ca-Key"
	HTTPHeaderCANonce            = "X-Ca-Nonce"
	HTTPHeaderCASignature        = "X-Ca-Signature"
	HTTPHeaderCASignatureHeaders = "X-Ca-Signature-Headers"
	HTTPHeaderCASignatureMethod  = "X-Ca-Signature-Method"
	HTTPHeaderCATimestamp        = "X-Ca-Timestamp"
)

HTTP header keys used for Aliyun API gateway signature.

View Source
const (
	HTTPContentTypeForm                      = "application/x-www-form-urlencoded"
	HTTPContentTypeMultipartForm             = "multipart/form-data"
	HTTPContentTypeMultipartFormWithBoundary = "multipart/form-data; boundary="
	HTTPContentTypeStream                    = "application/octet-stream"
	HTTPContentTypeJson                      = "application/json"
	HTTPContentTypeXml                       = "application/xml"
	HTTPContentTypeText                      = "text/plain"
)

HTTP header content-type values.

View Source
const (
	HTTPMethodGet     = "GET"
	HTTPMethodPost    = "POST"
	HTTPMethodPut     = "PUT"
	HTTPMethodDelete  = "DELETE"
	HTTPMethodPatch   = "PATCH"
	HTTPMethodHead    = "HEAD"
	HTTPMethodOptions = "OPTIONS"
)

HTTP method values.

Variables

This section is empty.

Functions

func CurrentGMTDate

func CurrentGMTDate() string

CurrentGMTDate returns the GMT date representation of the current time.

func CurrentTimeMillis

func CurrentTimeMillis() string

CurrentTimeMillis returns the millisecond representation of the current time.

func HmacSHA256

func HmacSHA256(b, key []byte) string

HmacSHA256 returns the string encrypted with HmacSHA256 method.

func MD5

func MD5(b []byte) string

MD5 returns the string hashed with MD5 method.

func Sign

func Sign(req *http.Request, appKey, appKeySecret string) error

Sign will sign the request with appKey and appKeySecret.

func UUID4

func UUID4() string

UUID4 returns random generated UUID string.

Types

This section is empty.

Jump to

Keyboard shortcuts

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