2022-05-06 13:28:09 +08:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2023-04-09 15:40:17 +08:00
|
|
|
"github.com/gofrs/uuid/v5"
|
2023-03-05 11:00:14 +08:00
|
|
|
"github.com/zhangyunhao116/fastrand"
|
2022-05-06 13:28:09 +08:00
|
|
|
)
|
|
|
|
|
2023-03-05 11:00:14 +08:00
|
|
|
type fastRandReader struct{}
|
|
|
|
|
|
|
|
func (r fastRandReader) Read(p []byte) (int, error) {
|
|
|
|
return fastrand.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
var UnsafeUUIDGenerator = uuid.NewGenWithOptions(uuid.WithRandomReader(fastRandReader{}))
|
2022-05-07 12:35:14 +08:00
|
|
|
|
2023-03-15 10:10:03 +08:00
|
|
|
func NewUUIDV1() uuid.UUID {
|
|
|
|
u, _ := UnsafeUUIDGenerator.NewV1() // fastrand.Read wouldn't cause error, so ignore err is safe
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUUIDV3(ns uuid.UUID, name string) uuid.UUID {
|
|
|
|
return UnsafeUUIDGenerator.NewV3(ns, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUUIDV4() uuid.UUID {
|
|
|
|
u, _ := UnsafeUUIDGenerator.NewV4() // fastrand.Read wouldn't cause error, so ignore err is safe
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUUIDV5(ns uuid.UUID, name string) uuid.UUID {
|
|
|
|
return UnsafeUUIDGenerator.NewV5(ns, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUUIDV6() uuid.UUID {
|
|
|
|
u, _ := UnsafeUUIDGenerator.NewV6() // fastrand.Read wouldn't cause error, so ignore err is safe
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUUIDV7() uuid.UUID {
|
|
|
|
u, _ := UnsafeUUIDGenerator.NewV7() // fastrand.Read wouldn't cause error, so ignore err is safe
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
2022-05-06 13:28:09 +08:00
|
|
|
// UUIDMap https://github.com/XTLS/Xray-core/issues/158#issue-783294090
|
|
|
|
func UUIDMap(str string) (uuid.UUID, error) {
|
|
|
|
u, err := uuid.FromString(str)
|
|
|
|
if err != nil {
|
2023-03-15 10:10:03 +08:00
|
|
|
return NewUUIDV5(uuid.Nil, str), nil
|
2022-05-06 13:28:09 +08:00
|
|
|
}
|
|
|
|
return u, nil
|
|
|
|
}
|