...
1
2
3
4
5 package uuid
6
7 import (
8 "encoding/binary"
9 "fmt"
10 "os"
11 )
12
13
14 type Domain byte
15
16
17 const (
18 Person = Domain(0)
19 Group = Domain(1)
20 Org = Domain(2)
21 )
22
23
24
25
26
27
28
29
30
31
32 func NewDCESecurity(domain Domain, id uint32) UUID {
33 uuid := NewUUID()
34 if uuid != nil {
35 uuid[6] = (uuid[6] & 0x0f) | 0x20
36 uuid[9] = byte(domain)
37 binary.BigEndian.PutUint32(uuid[0:], id)
38 }
39 return uuid
40 }
41
42
43
44
45
46 func NewDCEPerson() UUID {
47 return NewDCESecurity(Person, uint32(os.Getuid()))
48 }
49
50
51
52
53
54 func NewDCEGroup() UUID {
55 return NewDCESecurity(Group, uint32(os.Getgid()))
56 }
57
58
59 func (uuid UUID) Domain() (Domain, bool) {
60 if v, _ := uuid.Version(); v != 2 {
61 return 0, false
62 }
63 return Domain(uuid[9]), true
64 }
65
66
67 func (uuid UUID) Id() (uint32, bool) {
68 if v, _ := uuid.Version(); v != 2 {
69 return 0, false
70 }
71 return binary.BigEndian.Uint32(uuid[0:4]), true
72 }
73
74 func (d Domain) String() string {
75 switch d {
76 case Person:
77 return "Person"
78 case Group:
79 return "Group"
80 case Org:
81 return "Org"
82 }
83 return fmt.Sprintf("Domain%d", int(d))
84 }
85
View as plain text