github.com/solo-io/cue@v0.4.7/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 "fmt" 22 "math/big" 23 "regexp" 24 25 "github.com/google/uuid" 26 ) 27 28 var valid = regexp.MustCompile( 29 "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$") 30 31 // Valid can be used to define a valid Valid. 32 func Valid(s string) error { 33 if !valid.MatchString(string(s)) { 34 return fmt.Errorf("invalid UUID %q", s) 35 } 36 return nil 37 } 38 39 // Parse decodes s into a UUID or returns an error. Both the standard UUID forms 40 // of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and 41 // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx are decoded as well as the 42 // Microsoft encoding {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} and the raw hex 43 // encoding: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx. 44 func Parse(s string) (string, error) { 45 x, err := uuid.Parse(s) 46 return string(x.String()), err 47 } 48 49 // String represents a 128-bit UUID value as a string. 50 func ToString(x string) string { 51 return string(x) 52 } 53 54 // URN reports the canonical URN of a UUID. 55 func URN(x string) (string, error) { 56 u, err := uuid.Parse(string(x)) 57 if err != nil { 58 return "", err 59 } 60 return u.URN(), nil 61 } 62 63 // FromInt creates a UUID from an integer. 64 // 65 // DNS: uuid.FromInt(0x6ba7b810_9dad_11d1_80b4_00c04fd430c8) 66 // 67 func FromInt(i *big.Int) (string, error) { 68 // must be uint128 69 var buf [16]byte 70 b := i.Bytes() 71 if len(b) < 16 { 72 copy(buf[16-len(b):], b) 73 b = buf[:] 74 } 75 u, err := uuid.FromBytes(b) 76 return string(u.String()), err 77 } 78 79 // ToInt represents a UUID string as a 128-bit value. 80 func ToInt(x string) *big.Int { 81 var i big.Int 82 i.SetBytes([]byte(x[:])) 83 return &i 84 } 85 86 // Variant reports the UUID variant. 87 func Variant(x string) (int, error) { 88 u, err := uuid.Parse(string(x)) 89 if err != nil { 90 return 0, err 91 } 92 return int(u.Variant()), nil 93 } 94 95 // Version reports the UUID version. 96 func Version(x string) (int, error) { 97 u, err := uuid.Parse(string(x)) 98 if err != nil { 99 return 0, err 100 } 101 return int(u.Version()), nil 102 } 103 104 // SHA1 generates a version 5 UUID based on the supplied name space and data. 105 func SHA1(space string, data []byte) (string, error) { 106 u, err := uuid.Parse(string(space)) 107 if err != nil { 108 return "", err 109 } 110 return string(uuid.NewSHA1(u, data).String()), nil 111 } 112 113 // MD5 generates a version 3 UUID based on the supplied name space and data. 114 // Use SHA1 instead if you can. 115 func MD5(space string, data []byte) (string, error) { 116 u, err := uuid.Parse(string(space)) 117 if err != nil { 118 return "", err 119 } 120 return string(uuid.NewMD5(u, data).String()), nil 121 }