2017-10-23 15:21:01 +03:00
|
|
|
// Copyright (C) 2013-2018 by Maxim Bublis <b@codemonkey.ru>
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
// a copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
// permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
// the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be
|
|
|
|
// included in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
package uuid
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/binary"
|
2018-01-03 20:27:13 +03:00
|
|
|
"fmt"
|
2017-10-23 15:21:01 +03:00
|
|
|
"hash"
|
2018-01-03 19:02:28 +03:00
|
|
|
"io"
|
2017-10-23 15:21:01 +03:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Difference in 100-nanosecond intervals between
|
|
|
|
// UUID epoch (October 15, 1582) and Unix epoch (January 1, 1970).
|
|
|
|
const epochStart = 122192928000000000
|
|
|
|
|
2018-01-03 19:02:28 +03:00
|
|
|
type epochFunc func() time.Time
|
2018-01-03 20:27:13 +03:00
|
|
|
type hwAddrFunc func() (net.HardwareAddr, error)
|
2018-01-03 19:02:28 +03:00
|
|
|
|
2017-10-23 15:21:01 +03:00
|
|
|
var (
|
2018-01-03 19:02:28 +03:00
|
|
|
global = newRFC4122Generator()
|
2017-10-23 15:21:01 +03:00
|
|
|
|
2018-01-03 19:02:28 +03:00
|
|
|
posixUID = uint32(os.Getuid())
|
|
|
|
posixGID = uint32(os.Getgid())
|
2017-10-23 15:21:01 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewV1 returns UUID based on current timestamp and MAC address.
|
2018-01-03 19:02:28 +03:00
|
|
|
func NewV1() (UUID, error) {
|
2017-10-23 15:21:01 +03:00
|
|
|
return global.NewV1()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewV2 returns DCE Security UUID based on POSIX UID/GID.
|
2018-01-03 19:02:28 +03:00
|
|
|
func NewV2(domain byte) (UUID, error) {
|
2017-10-23 15:21:01 +03:00
|
|
|
return global.NewV2(domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewV3 returns UUID based on MD5 hash of namespace UUID and name.
|
|
|
|
func NewV3(ns UUID, name string) UUID {
|
|
|
|
return global.NewV3(ns, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewV4 returns random generated UUID.
|
2018-01-03 19:02:28 +03:00
|
|
|
func NewV4() (UUID, error) {
|
2017-10-23 15:21:01 +03:00
|
|
|
return global.NewV4()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewV5 returns UUID based on SHA-1 hash of namespace UUID and name.
|
|
|
|
func NewV5(ns UUID, name string) UUID {
|
|
|
|
return global.NewV5(ns, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generator provides interface for generating UUIDs.
|
|
|
|
type Generator interface {
|
2018-01-03 19:02:28 +03:00
|
|
|
NewV1() (UUID, error)
|
|
|
|
NewV2(domain byte) (UUID, error)
|
2017-10-23 15:21:01 +03:00
|
|
|
NewV3(ns UUID, name string) UUID
|
2018-01-03 19:02:28 +03:00
|
|
|
NewV4() (UUID, error)
|
2017-10-23 15:21:01 +03:00
|
|
|
NewV5(ns UUID, name string) UUID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default generator implementation.
|
2018-01-03 19:02:28 +03:00
|
|
|
type rfc4122Generator struct {
|
|
|
|
clockSequenceOnce sync.Once
|
|
|
|
hardwareAddrOnce sync.Once
|
|
|
|
storageMutex sync.Mutex
|
|
|
|
|
|
|
|
rand io.Reader
|
2017-10-23 15:21:01 +03:00
|
|
|
|
2018-01-03 19:02:28 +03:00
|
|
|
epochFunc epochFunc
|
2018-01-03 20:27:13 +03:00
|
|
|
hwAddrFunc hwAddrFunc
|
2017-10-23 15:21:01 +03:00
|
|
|
lastTime uint64
|
|
|
|
clockSequence uint16
|
|
|
|
hardwareAddr [6]byte
|
|
|
|
}
|
|
|
|
|
2018-01-03 19:02:28 +03:00
|
|
|
func newRFC4122Generator() Generator {
|
|
|
|
return &rfc4122Generator{
|
2018-01-03 20:27:13 +03:00
|
|
|
epochFunc: time.Now,
|
|
|
|
hwAddrFunc: defaultHWAddrFunc,
|
|
|
|
rand: rand.Reader,
|
2018-01-03 19:02:28 +03:00
|
|
|
}
|
2017-10-23 15:21:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewV1 returns UUID based on current timestamp and MAC address.
|
2018-01-03 19:02:28 +03:00
|
|
|
func (g *rfc4122Generator) NewV1() (UUID, error) {
|
2017-10-23 15:21:01 +03:00
|
|
|
u := UUID{}
|
|
|
|
|
2018-01-03 19:02:28 +03:00
|
|
|
timeNow, clockSeq, err := g.getClockSequence()
|
|
|
|
if err != nil {
|
|
|
|
return Nil, err
|
|
|
|
}
|
2017-10-23 15:21:01 +03:00
|
|
|
binary.BigEndian.PutUint32(u[0:], uint32(timeNow))
|
|
|
|
binary.BigEndian.PutUint16(u[4:], uint16(timeNow>>32))
|
|
|
|
binary.BigEndian.PutUint16(u[6:], uint16(timeNow>>48))
|
|
|
|
binary.BigEndian.PutUint16(u[8:], clockSeq)
|
|
|
|
|
2018-01-03 19:02:28 +03:00
|
|
|
hardwareAddr, err := g.getHardwareAddr()
|
|
|
|
if err != nil {
|
|
|
|
return Nil, err
|
|
|
|
}
|
2017-10-23 15:21:01 +03:00
|
|
|
copy(u[10:], hardwareAddr)
|
|
|
|
|
2018-01-03 03:26:12 +03:00
|
|
|
u.SetVersion(V1)
|
2018-01-03 06:42:45 +03:00
|
|
|
u.SetVariant(VariantRFC4122)
|
2017-10-23 15:21:01 +03:00
|
|
|
|
2018-01-03 19:02:28 +03:00
|
|
|
return u, nil
|
2017-10-23 15:21:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewV2 returns DCE Security UUID based on POSIX UID/GID.
|
2018-01-03 19:02:28 +03:00
|
|
|
func (g *rfc4122Generator) NewV2(domain byte) (UUID, error) {
|
2018-01-03 20:27:13 +03:00
|
|
|
u, err := g.NewV1()
|
2018-01-03 19:02:28 +03:00
|
|
|
if err != nil {
|
|
|
|
return Nil, err
|
|
|
|
}
|
2017-10-23 15:21:01 +03:00
|
|
|
|
|
|
|
switch domain {
|
|
|
|
case DomainPerson:
|
2018-01-03 19:02:28 +03:00
|
|
|
binary.BigEndian.PutUint32(u[:], posixUID)
|
2017-10-23 15:21:01 +03:00
|
|
|
case DomainGroup:
|
2018-01-03 19:02:28 +03:00
|
|
|
binary.BigEndian.PutUint32(u[:], posixGID)
|
2017-10-23 15:21:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
u[9] = domain
|
|
|
|
|
2018-01-03 03:26:12 +03:00
|
|
|
u.SetVersion(V2)
|
2018-01-03 06:42:45 +03:00
|
|
|
u.SetVariant(VariantRFC4122)
|
2017-10-23 15:21:01 +03:00
|
|
|
|
2018-01-03 19:02:28 +03:00
|
|
|
return u, nil
|
2017-10-23 15:21:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewV3 returns UUID based on MD5 hash of namespace UUID and name.
|
2018-01-03 19:02:28 +03:00
|
|
|
func (g *rfc4122Generator) NewV3(ns UUID, name string) UUID {
|
2017-10-23 15:21:01 +03:00
|
|
|
u := newFromHash(md5.New(), ns, name)
|
2018-01-03 03:26:12 +03:00
|
|
|
u.SetVersion(V3)
|
2018-01-03 06:42:45 +03:00
|
|
|
u.SetVariant(VariantRFC4122)
|
2017-10-23 15:21:01 +03:00
|
|
|
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewV4 returns random generated UUID.
|
2018-01-03 19:02:28 +03:00
|
|
|
func (g *rfc4122Generator) NewV4() (UUID, error) {
|
2017-10-23 15:21:01 +03:00
|
|
|
u := UUID{}
|
2018-04-04 19:55:56 +03:00
|
|
|
if _, err := io.ReadFull(g.rand, u[:]); err != nil {
|
2018-01-03 19:02:28 +03:00
|
|
|
return Nil, err
|
|
|
|
}
|
2018-01-03 03:26:12 +03:00
|
|
|
u.SetVersion(V4)
|
2018-01-03 06:42:45 +03:00
|
|
|
u.SetVariant(VariantRFC4122)
|
2017-10-23 15:21:01 +03:00
|
|
|
|
2018-01-03 19:02:28 +03:00
|
|
|
return u, nil
|
2017-10-23 15:21:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewV5 returns UUID based on SHA-1 hash of namespace UUID and name.
|
2018-01-03 19:02:28 +03:00
|
|
|
func (g *rfc4122Generator) NewV5(ns UUID, name string) UUID {
|
2017-10-23 15:21:01 +03:00
|
|
|
u := newFromHash(sha1.New(), ns, name)
|
2018-01-03 03:26:12 +03:00
|
|
|
u.SetVersion(V5)
|
2018-01-03 06:42:45 +03:00
|
|
|
u.SetVariant(VariantRFC4122)
|
2017-10-23 15:21:01 +03:00
|
|
|
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
2018-01-03 19:02:28 +03:00
|
|
|
// Returns epoch and clock sequence.
|
|
|
|
func (g *rfc4122Generator) getClockSequence() (uint64, uint16, error) {
|
|
|
|
var err error
|
|
|
|
g.clockSequenceOnce.Do(func() {
|
|
|
|
buf := make([]byte, 2)
|
2018-04-04 19:55:56 +03:00
|
|
|
if _, err = io.ReadFull(g.rand, buf); err != nil {
|
2018-01-03 19:02:28 +03:00
|
|
|
return
|
2017-10-23 15:21:01 +03:00
|
|
|
}
|
2018-01-03 19:02:28 +03:00
|
|
|
g.clockSequence = binary.BigEndian.Uint16(buf)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
2017-10-23 15:21:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
g.storageMutex.Lock()
|
|
|
|
defer g.storageMutex.Unlock()
|
|
|
|
|
2018-01-03 19:02:28 +03:00
|
|
|
timeNow := g.getEpoch()
|
|
|
|
// Clock didn't change since last UUID generation.
|
2017-10-23 15:21:01 +03:00
|
|
|
// Should increase clock sequence.
|
|
|
|
if timeNow <= g.lastTime {
|
|
|
|
g.clockSequence++
|
|
|
|
}
|
|
|
|
g.lastTime = timeNow
|
|
|
|
|
2018-01-03 19:02:28 +03:00
|
|
|
return timeNow, g.clockSequence, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns hardware address.
|
|
|
|
func (g *rfc4122Generator) getHardwareAddr() ([]byte, error) {
|
|
|
|
var err error
|
|
|
|
g.hardwareAddrOnce.Do(func() {
|
2018-01-03 20:27:13 +03:00
|
|
|
if hwAddr, err := g.hwAddrFunc(); err == nil {
|
|
|
|
copy(g.hardwareAddr[:], hwAddr)
|
|
|
|
return
|
2018-01-03 19:02:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize hardwareAddr randomly in case
|
|
|
|
// of real network interfaces absence.
|
2018-04-04 19:55:56 +03:00
|
|
|
if _, err = io.ReadFull(g.rand, g.hardwareAddr[:]); err != nil {
|
2018-01-03 19:02:28 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Set multicast bit as recommended by RFC 4122
|
|
|
|
g.hardwareAddr[0] |= 0x01
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
return g.hardwareAddr[:], nil
|
2017-10-23 15:21:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns difference in 100-nanosecond intervals between
|
|
|
|
// UUID epoch (October 15, 1582) and current time.
|
2018-01-03 19:02:28 +03:00
|
|
|
func (g *rfc4122Generator) getEpoch() uint64 {
|
|
|
|
return epochStart + uint64(g.epochFunc().UnixNano()/100)
|
2017-10-23 15:21:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns UUID based on hashing of namespace UUID and name.
|
|
|
|
func newFromHash(h hash.Hash, ns UUID, name string) UUID {
|
|
|
|
u := UUID{}
|
|
|
|
h.Write(ns[:])
|
|
|
|
h.Write([]byte(name))
|
|
|
|
copy(u[:], h.Sum(nil))
|
|
|
|
|
|
|
|
return u
|
|
|
|
}
|
2018-01-03 20:27:13 +03:00
|
|
|
|
|
|
|
// Returns hardware address.
|
|
|
|
func defaultHWAddrFunc() (net.HardwareAddr, error) {
|
|
|
|
ifaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
for _, iface := range ifaces {
|
|
|
|
if len(iface.HardwareAddr) >= 6 {
|
|
|
|
return iface.HardwareAddr, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return []byte{}, fmt.Errorf("uuid: no HW address found")
|
|
|
|
}
|