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 JacksonTian committed Sep 27, 2023
1 parent 423fe29 commit 45f1bc0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 41 deletions.
60 changes: 24 additions & 36 deletions sdk/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,47 @@
package utils

import (
"bytes"
"crypto/md5"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"hash"

Check failure on line 23 in sdk/utils/utils.go

View workflow job for this annotation

GitHub Actions / build (1.13)

imported and not used: "hash"

Check failure on line 23 in sdk/utils/utils.go

View workflow job for this annotation

GitHub Actions / build (1.14)

imported and not used: "hash"
rand2 "math/rand"
"math/rand"

Check failure on line 24 in sdk/utils/utils.go

View workflow job for this annotation

GitHub Actions / build (1.13)

rand redeclared as imported package name

Check failure on line 24 in sdk/utils/utils.go

View workflow job for this annotation

GitHub Actions / build (1.14)

rand redeclared as imported package name
"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 GetUUID() (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)

Check failure on line 55 in sdk/utils/utils.go

View workflow job for this annotation

GitHub Actions / build (1.13)

undefined: fmt

Check failure on line 55 in sdk/utils/utils.go

View workflow job for this annotation

GitHub Actions / build (1.14)

undefined: fmt
h := md5.New()
h.Write([]byte(msg))
return hex.EncodeToString(h.Sum(nil))
}

func GetMD5Base64(bytes []byte) (base64Value string) {
Expand Down Expand Up @@ -99,31 +112,6 @@ 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)

Expand Down
7 changes: 2 additions & 5 deletions sdk/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ func TestInitStructWithDefaultTag(t *testing.T) {
}

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))
assert.Equal(t, 32, len(GetUUID()))
assert.NotEqual(t, GetUUID(), GetUUID())
}

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

0 comments on commit 45f1bc0

Please sign in to comment.