Переглянути джерело

fix: new way to get nonce

wenzuochao 6 роки тому
батько
коміт
aceab7c714

+ 2 - 2
sdk/auth/rpc_signature_composer.go

@@ -22,7 +22,7 @@ import (
 	"github.com/aliyun/alibaba-cloud-sdk-go/sdk/utils"
 )
 
-var hookGetUUIDV4 = func(fn func() string) string {
+var hookGetNonce = func(fn func() string) string {
 	return fn()
 }
 
@@ -52,7 +52,7 @@ func completeRpcSignParams(request requests.AcsRequest, signer Signer, regionId
 	queryParams["SignatureMethod"] = signer.GetName()
 	queryParams["SignatureType"] = signer.GetType()
 	queryParams["SignatureVersion"] = signer.GetVersion()
-	queryParams["SignatureNonce"] = hookGetUUIDV4(utils.GetUUIDV4)
+	queryParams["SignatureNonce"] = hookGetNonce(utils.GetUUID)
 	queryParams["AccessKeyId"], err = signer.GetAccessKeyId()
 
 	if err != nil {

+ 4 - 4
sdk/auth/rpc_signature_composer_test.go

@@ -14,7 +14,7 @@ func mockRpcDate(fn func() string) string {
 	return "mock date"
 }
 
-func mockRpcGetUUIDV4(fn func() string) string {
+func mockRpcGetNonce(fn func() string) string {
 	return "MOCK_UUID"
 }
 
@@ -43,9 +43,9 @@ func TestRpcSignatureComposer(t *testing.T) {
 	origTestHookGetDate := hookGetDate
 	defer func() { hookGetDate = origTestHookGetDate }()
 	hookGetDate = mockRpcDate
-	origTestHookGetUUIDV4 := hookGetUUIDV4
-	defer func() { hookGetUUIDV4 = origTestHookGetUUIDV4 }()
-	hookGetUUIDV4 = mockRpcGetUUIDV4
+	origTestHookGetNonce := hookGetNonce
+	defer func() { hookGetNonce = origTestHookGetNonce }()
+	hookGetNonce = mockRpcGetNonce
 	signRpcRequest(request, signer, "regionId")
 	assert.Equal(t, "mock date", request.GetQueryParams()["Timestamp"])
 	assert.Equal(t, "MOCK_UUID", request.GetQueryParams()["SignatureNonce"])

+ 59 - 5
sdk/utils/utils.go

@@ -16,22 +16,35 @@ package utils
 
 import (
 	"crypto/md5"
+	"crypto/rand"
 	"encoding/base64"
 	"encoding/hex"
+	"hash"
+	rand2 "math/rand"
 	"net/url"
 	"reflect"
 	"strconv"
 	"time"
-
-	"github.com/satori/go.uuid"
 )
 
-func GetUUIDV4() (uuidHex string) {
-	uuidV4 := uuid.NewV4()
-	uuidHex = hex.EncodeToString(uuidV4.Bytes())
+type UUID [16]byte
+
+const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
+
+func GetUUID() (uuidHex string) {
+	uuid := NewUUID()
+	uuidHex = hex.EncodeToString(uuid[:])
 	return
 }
 
+func RandStringBytes(n int) string {
+	b := make([]byte, n)
+	for i := range b {
+		b[i] = letterBytes[rand2.Intn(len(letterBytes))]
+	}
+	return string(b)
+}
+
 func GetMD5Base64(bytes []byte) (base64Value string) {
 	md5Ctx := md5.New()
 	md5Ctx.Write(bytes)
@@ -85,3 +98,44 @@ func InitStructWithDefaultTag(bean interface{}) {
 		}
 	}
 }
+
+func NewUUID() UUID {
+	ns := UUID{}
+	safeRandom(ns[:])
+	u := newFromHash(md5.New(), ns, RandStringBytes(16))
+	u[6] = (u[6] & 0x0f) | (byte(2) << 4)
+	u[8] = (u[8]&(0xff>>2) | (0x02 << 6))
+
+	return u
+}
+
+func newFromHash(h hash.Hash, ns UUID, name string) UUID {
+	u := UUID{}
+	h.Write(ns[:])
+	h.Write([]byte(name))
+	copy(u[:], h.Sum(nil))
+
+	return u
+}
+
+func safeRandom(dest []byte) {
+	if _, err := rand.Read(dest); err != nil {
+		panic(err)
+	}
+}
+
+func (u UUID) String() string {
+	buf := make([]byte, 36)
+
+	hex.Encode(buf[0:8], u[0:4])
+	buf[8] = '-'
+	hex.Encode(buf[9:13], u[4:6])
+	buf[13] = '-'
+	hex.Encode(buf[14:18], u[6:8])
+	buf[18] = '-'
+	hex.Encode(buf[19:23], u[8:10])
+	buf[23] = '-'
+	hex.Encode(buf[24:], u[10:])
+
+	return string(buf)
+}

+ 6 - 4
sdk/utils/utils_test.go

@@ -25,10 +25,12 @@ func TestInitStructWithDefaultTag(t *testing.T) {
 	assert.Equal(t, 0, config.E)
 }
 
-func TestGetUUIDV4(t *testing.T) {
-	uuid := GetUUIDV4()
-	assert.Equal(t, 32, len(uuid))
-	assert.NotEqual(t, GetUUIDV4(), GetUUIDV4())
+func TestGetUUID(t *testing.T) {
+	uuid := NewUUID()
+	assert.Equal(t, 16, len(uuid))
+	assert.Equal(t, 36, len(uuid.String()))
+	uuidString := GetUUID()
+	assert.Equal(t, 32, len(uuidString))
 }
 
 func TestGetMD5Base64(t *testing.T) {