Skip to content

Commit

Permalink
fix: improve the design of nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
h-hg authored and yndu13 committed Sep 27, 2023
1 parent 9a572a8 commit 3fd1455
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 65 deletions.
2 changes: 1 addition & 1 deletion sdk/auth/rpc_signature_composer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"] = hookGetNonce(utils.GetUUID)
queryParams["SignatureNonce"] = hookGetNonce(utils.GetNonce)
queryParams["AccessKeyId"], err = signer.GetAccessKeyId()

if err != nil {
Expand Down
83 changes: 25 additions & 58 deletions sdk/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,42 @@
package utils

import (
"bytes"
"crypto/md5"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"hash"
rand2 "math/rand"
"fmt"
"math/rand"
"net/url"
"reflect"
"runtime"
"strconv"
"sync/atomic"
"time"
)

type UUID [16]byte

const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

func GetUUID() (uuidHex string) {
uuid := NewUUID()
uuidHex = hex.EncodeToString(uuid[:])
return
var processStartTime int64 = time.Now().UnixNano() / 1e6
var seqId int64 = 0

func getGID() uint64 {
// https://blog.sgmansfield.com/2015/12/goroutine-ids/
b := make([]byte, 64)
b = b[:runtime.Stack(b, false)]
b = bytes.TrimPrefix(b, []byte("goroutine "))
b = b[:bytes.IndexByte(b, ' ')]
n, _ := strconv.ParseUint(string(b), 10, 64)
return n
}

func RandStringBytes(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand2.Intn(len(letterBytes))]
}
return string(b)
func GetNonce() (uuidHex string) {
routineId := getGID()
currentTime := time.Now().UnixNano() / 1e6
seq := atomic.AddInt64(&seqId, 1)
randNum := rand.Int63()
msg := fmt.Sprintf("%d-%d-%d-%d-%d", processStartTime, routineId, currentTime, seq, randNum)
h := md5.New()
h.Write([]byte(msg))
return hex.EncodeToString(h.Sum(nil))
}

func GetMD5Base64(bytes []byte) (base64Value string) {
Expand Down Expand Up @@ -98,44 +106,3 @@ 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)
}
9 changes: 3 additions & 6 deletions sdk/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ func TestInitStructWithDefaultTag(t *testing.T) {
assert.Equal(t, 0, config.E)
}

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 TestGetNonce(t *testing.T) {
assert.Equal(t, 32, len(GetNonce()))
assert.NotEqual(t, GetNonce(), GetNonce())
}

func TestGetMD5Base64(t *testing.T) {
Expand Down

0 comments on commit 3fd1455

Please sign in to comment.