pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/uuid/uuid.go (about)

     1  // Package uuid contains methods for generating version 4 and 5 UUID's
     2  package uuid
     3  
     4  // ////////////////////////////////////////////////////////////////////////////////// //
     5  //                                                                                    //
     6  //                         Copyright (c) 2022 ESSENTIAL KAOS                          //
     7  //      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
     8  //                                                                                    //
     9  // ////////////////////////////////////////////////////////////////////////////////// //
    10  
    11  import (
    12  	"crypto/rand"
    13  	"crypto/sha1"
    14  	"encoding/hex"
    15  )
    16  
    17  // ////////////////////////////////////////////////////////////////////////////////// //
    18  
    19  // Predefined namespace UUID's
    20  var (
    21  	NsDNS  = []byte{107, 167, 184, 16, 157, 173, 17, 209, 128, 180, 0, 192, 79, 212, 48, 200}
    22  	NsURL  = []byte{107, 167, 184, 17, 157, 173, 17, 209, 128, 180, 0, 192, 79, 212, 48, 200}
    23  	NsOID  = []byte{107, 167, 184, 18, 157, 173, 17, 209, 128, 180, 0, 192, 79, 212, 48, 200}
    24  	NsX500 = []byte{107, 167, 184, 20, 157, 173, 17, 209, 128, 180, 0, 192, 79, 212, 48, 200}
    25  )
    26  
    27  // ////////////////////////////////////////////////////////////////////////////////// //
    28  
    29  // GenUUID generates v4 UUID (Universally Unique Identifier)
    30  func GenUUID() string {
    31  	return GenUUID4()
    32  }
    33  
    34  // GenUUID4 generates random generated UUID
    35  func GenUUID4() string {
    36  	uuid := make([]byte, 16)
    37  
    38  	rand.Read(uuid)
    39  
    40  	uuid[6] = (uuid[6] & 0X0F) | 0X40
    41  	uuid[8] = (uuid[8] & 0X3F) | 0X80
    42  
    43  	return toString(uuid)
    44  }
    45  
    46  // GenUUID5 generates UUID based on SHA-1 hash of namespace UUID and name
    47  func GenUUID5(ns []byte, name string) string {
    48  	uuid := make([]byte, 16)
    49  
    50  	hash := sha1.New()
    51  	hash.Write(ns)
    52  	hash.Write([]byte(name))
    53  
    54  	copy(uuid, hash.Sum(nil))
    55  
    56  	uuid[6] = (uuid[6] & 0X0F) | 0x50
    57  	uuid[8] = (uuid[8] & 0X3F) | 0x80
    58  
    59  	return toString(uuid)
    60  }
    61  
    62  // ////////////////////////////////////////////////////////////////////////////////// //
    63  
    64  func toString(uuid []byte) string {
    65  	buf := make([]byte, 36)
    66  
    67  	hex.Encode(buf[0:8], uuid[0:4])
    68  	buf[8] = '-'
    69  	hex.Encode(buf[9:13], uuid[4:6])
    70  	buf[13] = '-'
    71  	hex.Encode(buf[14:18], uuid[6:8])
    72  	buf[18] = '-'
    73  	hex.Encode(buf[19:23], uuid[8:10])
    74  	buf[23] = '-'
    75  	hex.Encode(buf[24:], uuid[10:])
    76  
    77  	return string(buf)
    78  }