...

Source file src/cuelang.org/go/pkg/uuid/uuid.go

Documentation: cuelang.org/go/pkg/uuid

     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  func FromInt(i *big.Int) (string, error) {
    67  	// must be uint128
    68  	var buf [16]byte
    69  	b := i.Bytes()
    70  	if len(b) < 16 {
    71  		copy(buf[16-len(b):], b)
    72  		b = buf[:]
    73  	}
    74  	u, err := uuid.FromBytes(b)
    75  	return string(u.String()), err
    76  }
    77  
    78  // ToInt represents a UUID string as a 128-bit value.
    79  func ToInt(x string) *big.Int {
    80  	var i big.Int
    81  	i.SetBytes([]byte(x[:]))
    82  	return &i
    83  }
    84  
    85  // Variant reports the UUID variant.
    86  func Variant(x string) (int, error) {
    87  	u, err := uuid.Parse(string(x))
    88  	if err != nil {
    89  		return 0, err
    90  	}
    91  	return int(u.Variant()), nil
    92  }
    93  
    94  // Version reports the UUID version.
    95  func Version(x string) (int, error) {
    96  	u, err := uuid.Parse(string(x))
    97  	if err != nil {
    98  		return 0, err
    99  	}
   100  	return int(u.Version()), nil
   101  }
   102  
   103  // SHA1 generates a version 5 UUID based on the supplied name space and data.
   104  func SHA1(space string, data []byte) (string, error) {
   105  	u, err := uuid.Parse(string(space))
   106  	if err != nil {
   107  		return "", err
   108  	}
   109  	return string(uuid.NewSHA1(u, data).String()), nil
   110  }
   111  
   112  // MD5 generates a version 3 UUID based on the supplied name space and data.
   113  // Use SHA1 instead if you can.
   114  func MD5(space string, data []byte) (string, error) {
   115  	u, err := uuid.Parse(string(space))
   116  	if err != nil {
   117  		return "", err
   118  	}
   119  	return string(uuid.NewMD5(u, data).String()), nil
   120  }
   121  

View as plain text