From 980d8a2641b38f60c3fa3e604f5e43617fe0f31d Mon Sep 17 00:00:00 2001 From: Skyxim Date: Fri, 6 May 2022 14:02:34 +0800 Subject: [PATCH] refactor: string map to uuidv5 --- common/utils/uuid.go | 26 ++++++-------------------- common/utils/uuid_test.go | 2 ++ 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/common/utils/uuid.go b/common/utils/uuid.go index 1e63e324..9ece3093 100644 --- a/common/utils/uuid.go +++ b/common/utils/uuid.go @@ -1,8 +1,6 @@ package utils import ( - "crypto/sha1" - "encoding/hex" "github.com/gofrs/uuid" ) @@ -10,24 +8,12 @@ import ( func UUIDMap(str string) (uuid.UUID, error) { u, err := uuid.FromString(str) if err != nil { - var Nil [16]byte - h := sha1.New() - h.Write(Nil[:]) - h.Write([]byte(str)) - u := h.Sum(nil)[:16] - u[6] = (u[6] & 0x0f) | (5 << 4) - u[8] = u[8]&(0xff>>2) | (0x02 << 6) - 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 uuid.FromString(string(buf)) + uuidNamespace, err := uuid.FromString("00000000-0000-0000-0000-000000000000") + if err != nil { + return uuid.UUID{}, err + } + + return uuid.NewV5(uuidNamespace, str), err } return u, nil } diff --git a/common/utils/uuid_test.go b/common/utils/uuid_test.go index 748d72c9..ba00ea08 100644 --- a/common/utils/uuid_test.go +++ b/common/utils/uuid_test.go @@ -10,6 +10,7 @@ func TestUUIDMap(t *testing.T) { type args struct { str string } + tests := []struct { name string args args @@ -40,6 +41,7 @@ func TestUUIDMap(t *testing.T) { want: uuid.FromStringOrNil("f8598425-92f2-5508-a071-4fc67f9040ac"), wantErr: false, }, + // GENERATED BY 'xray uuid -i' { name: "uuid-map-2", args: args{