github.com/Microsoft/azure-vhd-utils@v0.0.0-20230613175315-7c30a3748a1b/vhdcore/common/uuid.go (about)

     1  package common
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  )
     7  
     8  // UUID represents a Universally Unique Identifier.
     9  //
    10  type UUID struct {
    11  	uuid [16]byte
    12  }
    13  
    14  // NewUUID creates a new UUID, it uses the given 128-bit (16 byte) value as the uuid.
    15  //
    16  func NewUUID(b []byte) (*UUID, error) {
    17  	if len(b) != 16 {
    18  		return nil, errors.New("NewUUID: buffer requires to be 16 bytes")
    19  	}
    20  
    21  	u := &UUID{}
    22  	copy(u.uuid[:], b)
    23  	return u, nil
    24  }
    25  
    26  // String returns the string representation of the UUID which is 16 hex digits separated by hyphens
    27  // int form xxxx-xx-xx-xx-xxxxxx
    28  //
    29  func (u *UUID) String() string {
    30  	a := uint32(u.uuid[3])<<24 | uint32(u.uuid[2])<<16 | uint32(u.uuid[1])<<8 | uint32(u.uuid[0])
    31  	// a := b.order.Uint32(b.buffer[:4])
    32  	b1 := int16(int32(u.uuid[5])<<8 | int32(u.uuid[4]))
    33  	// b1 := b.order.Uint16(b.buffer[4:6])
    34  	c := int16(int32(u.uuid[7])<<8 | int32(u.uuid[6]))
    35  	// c := b.order.Uint16(b.buffer[6:8])
    36  	d := u.uuid[8]
    37  	e := u.uuid[9]
    38  	f := u.uuid[10]
    39  	g := u.uuid[11]
    40  	h := u.uuid[12]
    41  	i := u.uuid[13]
    42  	j := u.uuid[14]
    43  	k := u.uuid[15]
    44  	return fmt.Sprintf("%x-%x-%x-%x%x-%x%x%x%x%x%x", a, b1, c, d, e, f, g, h, i, j, k)
    45  }
    46  
    47  // ToByteSlice returns the UUID as byte slice.
    48  //
    49  func (u *UUID) ToByteSlice() []byte {
    50  	b := make([]byte, 16)
    51  	copy(b, u.uuid[:])
    52  	return b
    53  }