gosignv2

package module
v0.0.0-...-26ff935 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

README

Go Sign

Http API 签名验证工具包,提供对API请求的签名生成、签名校验等工具类。

0x00 安装

$ go get -u github.com/cjieyan/go-sign-v2

OR

dep ensure --add github.com/cjieyan/go-sign-v2

0x01 生成签名信息

sign.GoSigner工具类,默认支持两种签名生成算法:

  1. MD5: 见 NewGoSignerMd5() 函数
  2. Sha1 + Hmac: 见:NewGoSignerHmac()函数

如果需要使用其它签名生成算法,使用 NewGoSigner(FUNC) 指定实现签名生成算法的实现即可。

Usage

signer := NewGoSignerMd5()

// 设置签名基本参数
signer.SetAppId("9d8a121ce581499d")
signer.SetTimeStamp(1532585241)
signer.SetNonceStr("ibuaiVcKdpRxkhJA")

// 设置参与签名的其它参数
signer.AddBody("plate_number", "豫A66666")

// AppSecretKey,前后包装签名体字符串
signer.SetAppSecretWrapBody("d93047a4d6fe6111")

fmt.Println("生成签字字符串:" + signer.GetUnsignedString())
fmt.Println("输出URL字符串:" + signer.GetSignedQuery())

输出结果为:

生成签字字符串:d93047a4d6fe6111appid=9d8a121ce581499d&nonce_str=ibuaiVcKdpRxkhJA&plate_number=豫A66666&time_stamp=1532585241d93047a4d6fe6111

输出URL字符串:appid=9d8a121ce581499d&nonce_str=ibuaiVcKdpRxkhJA&plate_number=豫A66666&time_stamp=1532585241&sign=072defd1a251dc58e4d1799e17ffe7a4

0x02 校验签名信息

sign.GoVerifier 工具类,用来校验签名参数的格式和时间戳。它与GoSigner一起使用,用于服务端校验API请求的签名信息。

Usage

    requestUri := "/restful/api/numbers?appid=9d8a121ce581499d&nonce_str=ibuaiVcKdpRxkhJA&plate_number=豫A66666" +
		"&time_stamp=1532585241&sign=072defd1a251dc58e4d1799e17ffe7a4"

	// 第一步:创建GoVerifier校验类
	verifier := NewGoVerifier()

	// 假定从RequestUri中读取校验参数
	if err := verifier.ParseQuery(requestUri); nil != err {
		t.Fatal(err)
	}

	// 或者使用verifier.ParseValues(Values)来解析。

	// 第二步:(可选)校验是否包含签名校验必要的参数
	if err := verifier.MustHasOtherFields("plate_number"); nil != err {
		t.Fatal(err)
	}

	// 第三步:检查时间戳是否超时。

	// 时间戳超时:5分钟
	verifier.SetTimeout(time.Minute * 5)
	if err := verifier.CheckTimeStamp(); nil != err {
		t.Fatal(err)
	}

	// 第四步: 创建GoSigner来重现客户端的签名信息
	signer := NewGoSignerMd5()

	// 第五步:从GoVerifier中读取所有请求参数
	signer.SetBody(verifier.GetBodyWithoutSign())

	// 第六步:从数据库读取AppID对应的SecretKey
	// appid := verifier.GetAppId()
	secretKey := "d93047a4d6fe6111"

	// 使用同样的WrapBody方式
	signer.SetAppSecretWrapBody(secretKey)

	// 服务端根据客户端参数生成签名
	sign := signer.GetSignature()

    // 最后,比较服务端生成的签名信息,与客户端提供的签名是否一致即可。
	if verifier.MustString("sign") != sign {
		t.Fatal("校验失败")
	}

注意事项

  1. 请求参数是多值(Slice)的,在进入签名参数体时,会将它们以英文逗号JOIN成单独字符串再进行签名。
  2. 获取Int64类型参数时,如果参数对应的值不存在、不可转换成Int64类型,会返回默认值0;

License

Copyright 2018 Xi'An iRain IoT Technology Service Co.,Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Index

Constants

View Source
const (
	KeyNameTimestamp = "timestamp"
	KeyNameNonceStr  = "nonce"
	KeyNameAppId     = "appid"
	KeyNameSign      = "sign"
)

Variables

This section is empty.

Functions

func Hmac5Sign

func Hmac5Sign(secretKey, body string) []byte

func Md5Sign

func Md5Sign(_, body string) []byte

func RandString

func RandString(num int) string

RandString 返回指定长度的随机字符串。 最小长度为4,最大长度为1024

func SetKeyNameAppId

func SetKeyNameAppId(name string)

func SetKeyNameNonceStr

func SetKeyNameNonceStr(name string)

func SetKeyNameSign

func SetKeyNameSign(name string)

func SetKeyNameTimestamp

func SetKeyNameTimestamp(name string)

func SortKVPairs

func SortKVPairs(m url.Values) string

SortKVPairs 将Map的键值对,按字典顺序拼接成字符串

Types

type CryptoFunc

type CryptoFunc func(secretKey string, args string) []byte

签名加密函数

type DefaultKeyName

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

func (*DefaultKeyName) SetKeyNameAppId

func (slf *DefaultKeyName) SetKeyNameAppId(name string)

func (*DefaultKeyName) SetKeyNameNonceStr

func (slf *DefaultKeyName) SetKeyNameNonceStr(name string)

func (*DefaultKeyName) SetKeyNameSign

func (slf *DefaultKeyName) SetKeyNameSign(name string)

func (*DefaultKeyName) SetKeyNameTimestamp

func (slf *DefaultKeyName) SetKeyNameTimestamp(name string)

type GoSigner

type GoSigner struct {
	*DefaultKeyName
	// contains filtered or unexported fields
}

func NewGoSigner

func NewGoSigner(cryptoFunc CryptoFunc) *GoSigner

func NewGoSignerHmac

func NewGoSignerHmac() *GoSigner

func NewGoSignerMd5

func NewGoSignerMd5() *GoSigner

func (*GoSigner) AddBodies

func (slf *GoSigner) AddBodies(key string, value []string) *GoSigner

func (*GoSigner) AddBody

func (slf *GoSigner) AddBody(key string, value string) *GoSigner

AddBody 添加签名体字段和值

func (*GoSigner) GetAppId

func (slf *GoSigner) GetAppId() string

func (*GoSigner) GetBody

func (slf *GoSigner) GetBody() url.Values

GetBody 返回Body内容

func (*GoSigner) GetNonceStr

func (slf *GoSigner) GetNonceStr() string

GetNonceStr 返回NonceStr字符串

func (*GoSigner) GetSignBodyString

func (slf *GoSigner) GetSignBodyString() string

GetSignBodyString 获取用于签名的原始字符串

func (*GoSigner) GetSignature

func (slf *GoSigner) GetSignature() string

GetSignature 获取签名

func (*GoSigner) GetSignedQuery

func (slf *GoSigner) GetSignedQuery() string

GetSignedQuery 获取带签名参数的字符串

func (*GoSigner) GetTimeStamp

func (slf *GoSigner) GetTimeStamp() string

GetTimeStamp 获取TimeStamp

func (*GoSigner) MakeRawBodyString

func (slf *GoSigner) MakeRawBodyString() string

MakeRawBodyString 获取用于签名的原始字符串

func (*GoSigner) MakeSign

func (slf *GoSigner) MakeSign() string

GetSignature 获取签名

func (*GoSigner) MakeSignedQuery

func (slf *GoSigner) MakeSignedQuery() string

GetSignedQuery 获取带签名参数的字符串

func (*GoSigner) RandNonceStr

func (slf *GoSigner) RandNonceStr() *GoSigner

RandNonceStr 自动生成16位随机字符串参数

func (*GoSigner) SetAppId

func (slf *GoSigner) SetAppId(appId string) *GoSigner

SetAppId 设置AppId参数

func (*GoSigner) SetAppSecret

func (slf *GoSigner) SetAppSecret(appSecret string) *GoSigner

SetAppSecret 设置签名密钥

func (*GoSigner) SetAppSecretWrapBody

func (slf *GoSigner) SetAppSecretWrapBody(appSecret string) *GoSigner

SetAppSecretWrapBody 在签名参数体的首部和尾部,拼接AppSecret字符串。

func (*GoSigner) SetBody

func (slf *GoSigner) SetBody(body url.Values)

SetBody 设置整个参数体Body对象。

func (*GoSigner) SetNonceStr

func (slf *GoSigner) SetNonceStr(nonce string) *GoSigner

SetNonceStr 设置随机字符串参数

func (*GoSigner) SetSignBodyPrefix

func (slf *GoSigner) SetSignBodyPrefix(prefix string) *GoSigner

SetSignBodyPrefix 设置签名字符串的前缀字符串

func (*GoSigner) SetSignBodySuffix

func (slf *GoSigner) SetSignBodySuffix(suffix string) *GoSigner

SetSignBodySuffix 设置签名字符串的后缀字符串

func (*GoSigner) SetSplitChar

func (slf *GoSigner) SetSplitChar(split string) *GoSigner

SetSplitChar设置前缀、后缀与签名体之间的分隔符号。默认为空字符串

func (*GoSigner) SetTimeStamp

func (slf *GoSigner) SetTimeStamp(ts int64) *GoSigner

SetTimeStamp 设置时间戳参数

type GoVerifier

type GoVerifier struct {
	*DefaultKeyName
	// contains filtered or unexported fields
}

func NewGoVerifier

func NewGoVerifier() *GoVerifier

func (*GoVerifier) CheckTimeStamp

func (slf *GoVerifier) CheckTimeStamp() error

检查时间戳有效期

func (*GoVerifier) GetAppId

func (slf *GoVerifier) GetAppId() string

func (*GoVerifier) GetBody

func (slf *GoVerifier) GetBody() url.Values

func (*GoVerifier) GetBodyWithoutSign

func (slf *GoVerifier) GetBodyWithoutSign() url.Values

GetBodyWithoutSign 获取所有参数体。其中不包含sign 字段

func (*GoVerifier) GetNonceStr

func (slf *GoVerifier) GetNonceStr() string

func (*GoVerifier) GetSign

func (slf *GoVerifier) GetSign() string

func (*GoVerifier) GetTimestamp

func (slf *GoVerifier) GetTimestamp() int64

func (*GoVerifier) MustHasKeys

func (slf *GoVerifier) MustHasKeys(keys ...string) error

MustHasKeys 必须包含指定的字段参数

func (*GoVerifier) MustHasOtherKeys

func (slf *GoVerifier) MustHasOtherKeys(keys ...string) error

MustHasKeys 必须包含除特定的[timestamp, nonce, sign, appid]等之外的指定的字段参数

func (*GoVerifier) MustInt64

func (slf *GoVerifier) MustInt64(key string) int64

MustInt64 获取Int64值

func (*GoVerifier) MustString

func (slf *GoVerifier) MustString(key string) string

MustString 获取字符串值

func (*GoVerifier) MustStrings

func (slf *GoVerifier) MustStrings(key string) []string

MustString 获取字符串值数组

func (*GoVerifier) ParseQuery

func (slf *GoVerifier) ParseQuery(requestUri string) error

ParseQuery 将参数字符串解析成参数列表

func (*GoVerifier) ParseValues

func (slf *GoVerifier) ParseValues(values url.Values)

ParseValues 将Values参数列表解析成参数Map。如果参数是多值的,则将它们以逗号Join成字符串。

func (*GoVerifier) SetTimeout

func (slf *GoVerifier) SetTimeout(timeout time.Duration) *GoVerifier

SetTimeout 设置签名校验过期时间

Jump to

Keyboard shortcuts

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