cuelang.org/go@v0.13.0/pkg/uuid/uuid.go (about) 1 // Copyright 2021 CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package uuid defines functionality for creating UUIDs as defined in RFC 4122. 16 // 17 // Currently only Version 5 (SHA1) and Version 3 (MD5) are supported. 18 package uuid 19 20 import ( 21 "math/big" 22 23 "github.com/google/uuid" 24 ) 25 26 // Valid ensures that s is a valid UUID which would be accepted by Parse. 27 func Valid(s string) error { 28 return uuid.Validate(s) 29 } 30 31 // Parse decodes s into a UUID or returns an error. Both the standard UUID forms 32 // of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and 33 // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx are decoded as well as the 34 // Microsoft encoding {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} and the raw hex 35 // encoding: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx. 36 func Parse(s string) (string, error) { 37 x, err := uuid.Parse(s) 38 return x.String(), err 39 } 40 41 // URN reports the canonical URN of a UUID. 42 func URN(x string) (string, error) { 43 u, err := uuid.Parse(x) 44 if err != nil { 45 return "", err 46 } 47 return u.URN(), nil 48 } 49 50 // FromInt creates a UUID from an integer. 51 // 52 // DNS: uuid.FromInt(0x6ba7b810_9dad_11d1_80b4_00c04fd430c8) 53 func FromInt(i *big.Int) (string, error) { 54 // must be uint128 55 var buf [16]byte 56 b := i.Bytes() 57 if len(b) < 16 { 58 copy(buf[16-len(b):], b) 59 b = buf[:] 60 } 61 u, err := uuid.FromBytes(b) 62 return u.String(), err 63 } 64 65 // ToInt represents a UUID string as a 128-bit value. 66 func ToInt(x string) *big.Int { 67 var i big.Int 68 i.SetBytes([]byte(x[:])) 69 return &i 70 } 71 72 // Variant reports the UUID variant. 73 func Variant(x string) (int, error) { 74 u, err := uuid.Parse(x) 75 if err != nil { 76 return 0, err 77 } 78 return int(u.Variant()), nil 79 } 80 81 // Version reports the UUID version. 82 func Version(x string) (int, error) { 83 u, err := uuid.Parse(x) 84 if err != nil { 85 return 0, err 86 } 87 return int(u.Version()), nil 88 } 89 90 // SHA1 generates a version 5 UUID based on the supplied name space and data. 91 func SHA1(space string, data []byte) (string, error) { 92 u, err := uuid.Parse(space) 93 if err != nil { 94 return "", err 95 } 96 return uuid.NewSHA1(u, data).String(), nil 97 } 98 99 // MD5 generates a version 3 UUID based on the supplied name space and data. 100 // Use SHA1 instead if you can. 101 func MD5(space string, data []byte) (string, error) { 102 u, err := uuid.Parse(space) 103 if err != nil { 104 return "", err 105 } 106 return uuid.NewMD5(u, data).String(), nil 107 }