From 299a5c3eb77d52ef0ba158d84634e8bc5df5546e Mon Sep 17 00:00:00 2001 From: wziww Date: Thu, 4 Feb 2021 16:28:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=200=20=E6=8B=B7=E8=B4=9D?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/convert/convert.go | 6 ++++ framework/crypto/cryptos.go | 4 ++- framework/crypto/uuid/uuid.go | 62 ++++++++++++++++------------------- 3 files changed, 38 insertions(+), 34 deletions(-) diff --git a/framework/convert/convert.go b/framework/convert/convert.go index fc973f3..76d9345 100644 --- a/framework/convert/convert.go +++ b/framework/convert/convert.go @@ -5,6 +5,7 @@ import ( "math/big" "strconv" "time" + "unsafe" ) // String2Bytes convert string to []byte @@ -50,3 +51,8 @@ func NSToTime(ns int64) (time.Time, error) { bigNS := big.NewInt(ns) return time.Unix(ns/1e9, int64(bigNS.Mod(bigNS, big.NewInt(1e9)).Uint64())), nil } + +// Bytes2String converts byte slice to string without copy b.data. +func Bytes2String(b []byte) string { + return *(*string)(unsafe.Pointer(&b)) +} diff --git a/framework/crypto/cryptos.go b/framework/crypto/cryptos.go index 3a58009..b34abe5 100644 --- a/framework/crypto/cryptos.go +++ b/framework/crypto/cryptos.go @@ -5,6 +5,8 @@ import ( "encoding/hex" "math/rand" "time" + + "github.com/devfeel/dotweb/framework/convert" ) // GetMd5String compute the md5 sum as string @@ -22,5 +24,5 @@ func GetRandString(length int) string { for i := 0; i < length; i++ { result = append(result, bytes[r.Intn(len(bytes))]) } - return string(result) + return convert.Bytes2String(result) } diff --git a/framework/crypto/uuid/uuid.go b/framework/crypto/uuid/uuid.go index 5bf4e24..ebaae33 100644 --- a/framework/crypto/uuid/uuid.go +++ b/framework/crypto/uuid/uuid.go @@ -1,47 +1,43 @@ package uuid import ( + "bytes" "crypto/md5" + "crypto/rand" "crypto/sha1" "database/sql/driver" + "encoding/binary" "encoding/hex" "fmt" "hash" + "net" "os" "sync" "time" -) - -// Copyright (C) 2013-2015 by Maxim Bublis -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -// Package uuid provides implementation of Universally Unique Identifier (UUID). -// Supported versions are 1, 3, 4 and 5 (as specified in RFC 4122) and -// version 2 (as specified in DCE 1.1). -import ( - "bytes" - "crypto/rand" - "encoding/binary" - "net" + "github.com/devfeel/dotweb/framework/convert" // Copyright (C) 2013-2015 by Maxim Bublis + // + // Permission is hereby granted, free of charge, to any person obtaining + // a copy of this software and associated documentation files (the + // "Software"), to deal in the Software without restriction, including + // without limitation the rights to use, copy, modify, merge, publish, + // distribute, sublicense, and/or sell copies of the Software, and to + // permit persons to whom the Software is furnished to do so, subject to + // the following conditions: + // + // The above copyright notice and this permission notice shall be + // included in all copies or substantial portions of the Software. + // + // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + // Package uuid provides implementation of Universally Unique Identifier (UUID). + // Supported versions are 1, 3, 4 and 5 (as specified in RFC 4122) and + // version 2 (as specified in DCE 1.1). ) // UUID layout variants. @@ -211,7 +207,7 @@ func (u UUID) String() string { buf[23] = dash hex.Encode(buf[24:], u[10:]) - return string(buf) + return convert.Bytes2String(buf) } // String32 return uuid with 32 len @@ -221,7 +217,7 @@ func (u UUID) String32() string { hex.Encode(buf[0:], u[0:]) - return string(buf) + return convert.Bytes2String(buf) } // SetVersion sets version bits.