github.com/metacubex/mihomo@v1.18.5/common/utils/uuid.go (about)

     1  package utils
     2  
     3  import (
     4  	"github.com/gofrs/uuid/v5"
     5  	"github.com/zhangyunhao116/fastrand"
     6  )
     7  
     8  type fastRandReader struct{}
     9  
    10  func (r fastRandReader) Read(p []byte) (int, error) {
    11  	return fastrand.Read(p)
    12  }
    13  
    14  var UnsafeUUIDGenerator = uuid.NewGenWithOptions(uuid.WithRandomReader(fastRandReader{}))
    15  
    16  func NewUUIDV1() uuid.UUID {
    17  	u, _ := UnsafeUUIDGenerator.NewV1() // fastrand.Read wouldn't cause error, so ignore err is safe
    18  	return u
    19  }
    20  
    21  func NewUUIDV3(ns uuid.UUID, name string) uuid.UUID {
    22  	return UnsafeUUIDGenerator.NewV3(ns, name)
    23  }
    24  
    25  func NewUUIDV4() uuid.UUID {
    26  	u, _ := UnsafeUUIDGenerator.NewV4() // fastrand.Read wouldn't cause error, so ignore err is safe
    27  	return u
    28  }
    29  
    30  func NewUUIDV5(ns uuid.UUID, name string) uuid.UUID {
    31  	return UnsafeUUIDGenerator.NewV5(ns, name)
    32  }
    33  
    34  func NewUUIDV6() uuid.UUID {
    35  	u, _ := UnsafeUUIDGenerator.NewV6() // fastrand.Read wouldn't cause error, so ignore err is safe
    36  	return u
    37  }
    38  
    39  func NewUUIDV7() uuid.UUID {
    40  	u, _ := UnsafeUUIDGenerator.NewV7() // fastrand.Read wouldn't cause error, so ignore err is safe
    41  	return u
    42  }
    43  
    44  // UUIDMap https://github.com/XTLS/Xray-core/issues/158#issue-783294090
    45  func UUIDMap(str string) (uuid.UUID, error) {
    46  	u, err := uuid.FromString(str)
    47  	if err != nil {
    48  		return NewUUIDV5(uuid.Nil, str), nil
    49  	}
    50  	return u, nil
    51  }