|
|
@@ -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)
|
|
|
+}
|