Path: blob/main/vendor/github.com/google/uuid/dce.go
2875 views
// Copyright 2016 Google Inc. All rights reserved.1// Use of this source code is governed by a BSD-style2// license that can be found in the LICENSE file.34package uuid56import (7"encoding/binary"8"fmt"9"os"10)1112// A Domain represents a Version 2 domain13type Domain byte1415// Domain constants for DCE Security (Version 2) UUIDs.16const (17Person = Domain(0)18Group = Domain(1)19Org = Domain(2)20)2122// NewDCESecurity returns a DCE Security (Version 2) UUID.23//24// The domain should be one of Person, Group or Org.25// On a POSIX system the id should be the users UID for the Person26// domain and the users GID for the Group. The meaning of id for27// the domain Org or on non-POSIX systems is site defined.28//29// For a given domain/id pair the same token may be returned for up to30// 7 minutes and 10 seconds.31func NewDCESecurity(domain Domain, id uint32) (UUID, error) {32uuid, err := NewUUID()33if err == nil {34uuid[6] = (uuid[6] & 0x0f) | 0x20 // Version 235uuid[9] = byte(domain)36binary.BigEndian.PutUint32(uuid[0:], id)37}38return uuid, err39}4041// NewDCEPerson returns a DCE Security (Version 2) UUID in the person42// domain with the id returned by os.Getuid.43//44// NewDCESecurity(Person, uint32(os.Getuid()))45func NewDCEPerson() (UUID, error) {46return NewDCESecurity(Person, uint32(os.Getuid()))47}4849// NewDCEGroup returns a DCE Security (Version 2) UUID in the group50// domain with the id returned by os.Getgid.51//52// NewDCESecurity(Group, uint32(os.Getgid()))53func NewDCEGroup() (UUID, error) {54return NewDCESecurity(Group, uint32(os.Getgid()))55}5657// Domain returns the domain for a Version 2 UUID. Domains are only defined58// for Version 2 UUIDs.59func (uuid UUID) Domain() Domain {60return Domain(uuid[9])61}6263// ID returns the id for a Version 2 UUID. IDs are only defined for Version 264// UUIDs.65func (uuid UUID) ID() uint32 {66return binary.BigEndian.Uint32(uuid[0:4])67}6869func (d Domain) String() string {70switch d {71case Person:72return "Person"73case Group:74return "Group"75case Org:76return "Org"77}78return fmt.Sprintf("Domain%d", int(d))79}808182