go.uuid/uuid.go

241 lines
5.4 KiB
Go
Raw Normal View History

2013-06-18 17:23:53 +04:00
/*
This package provides implementation of Universally Unique Identifier (UUID).
Supported versions are 1, 3, 4 and 5 (as specified in RFC 4122) and
version 2 (as specified in DCE 1.1).
2013-06-18 17:23:53 +04:00
*/
2013-06-18 13:50:18 +04:00
package uuid
import (
2013-06-18 20:17:07 +04:00
"bytes"
2013-06-18 17:23:53 +04:00
"crypto/md5"
2013-06-18 13:50:18 +04:00
"crypto/rand"
2013-06-18 17:23:53 +04:00
"crypto/sha1"
2013-06-18 18:58:10 +04:00
"encoding/binary"
"errors"
2013-06-18 14:53:23 +04:00
"fmt"
2013-06-18 17:23:53 +04:00
"hash"
2013-06-18 18:58:10 +04:00
"net"
"os"
2013-06-18 18:58:10 +04:00
"time"
2013-06-18 13:50:18 +04:00
)
2013-06-18 14:51:58 +04:00
// UUID layout variants.
const (
VariantNCS = iota
VariantRFC4122
VariantMicrosoft
VariantFuture
)
// UUID DCE domains.
const (
DomainPerson = iota
DomainGroup
DomainOrg
)
2013-06-18 18:58:10 +04:00
// Difference in 100-nanosecond intervals between
2013-06-18 19:08:28 +04:00
// UUID epoch (October 15, 1582) and Unix epoch (January 1, 1970).
2013-06-18 18:58:10 +04:00
const epochStart = 122192928000000000
// UUID v1/v2 storage.
var (
clockSequence uint16
lastTime uint64
hardwareAddr [6]byte
posixUID = uint32(os.Getuid())
posixGID = uint32(os.Getgid())
)
// Epoch calculation function
var epochFunc func() uint64
// Initialize storage
func init() {
buf := make([]byte, 2)
2013-07-09 17:02:40 +04:00
rand.Read(buf)
clockSequence = binary.BigEndian.Uint16(buf)
// Initialize hardwareAddr randomly in case
// of real network interfaces absence
rand.Read(hardwareAddr[:])
interfaces, err := net.Interfaces()
if err == nil {
for _, iface := range interfaces {
if len(iface.HardwareAddr) >= 6 {
copy(hardwareAddr[:], iface.HardwareAddr)
break
}
}
}
epochFunc = unixTimeFunc
}
// Returns difference in 100-nanosecond intervals between
// UUID epoch (October 15, 1582) and current time.
// This is default epoch calculation function.
func unixTimeFunc() uint64 {
2013-07-17 16:00:24 +04:00
return epochStart + uint64(time.Now().UnixNano()/100)
}
// Returns current epoch calculation function.
2013-07-17 16:00:24 +04:00
func GetEpochFunc() func() uint64 {
return epochFunc
}
// Sets epoch calculation function.
func SetEpochFunc(newEpochFunc func() uint64) {
epochFunc = newEpochFunc
}
2013-06-18 18:58:10 +04:00
2013-06-18 14:53:23 +04:00
// UUID representation compliant with specification
// described in RFC 4122.
2013-06-18 13:50:18 +04:00
type UUID [16]byte
2013-06-18 19:08:28 +04:00
// Predefined namespace UUIDs.
2013-06-18 17:23:53 +04:00
var (
2013-06-18 20:16:16 +04:00
NamespaceDNS = &UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
NamespaceURL = &UUID{0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
NamespaceOID = &UUID{0x6b, 0xa7, 0xb8, 0x12, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
NamespaceX500 = &UUID{0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
2013-06-18 17:23:53 +04:00
)
2013-06-18 20:17:07 +04:00
// Returns true if u1 and u2 equals, otherwise returns false.
func Equal(u1 *UUID, u2 *UUID) bool {
return bytes.Equal(u1[:], u2[:])
}
2013-06-18 17:24:02 +04:00
// Returns algorithm version used to generate UUID.
2013-06-18 13:50:18 +04:00
func (u *UUID) Version() uint {
return uint(u[6] >> 4)
}
2013-06-18 14:51:58 +04:00
// Returns UUID layout variant.
func (u *UUID) Variant() uint {
switch {
2013-06-18 14:53:23 +04:00
case (u[8] & 0x80) == 0x00:
return VariantNCS
case (u[8]&0xc0)|0x80 == 0x80:
return VariantRFC4122
case (u[8]&0xe0)|0xc0 == 0xc0:
return VariantMicrosoft
2013-06-18 14:51:58 +04:00
}
return VariantFuture
}
2013-06-18 13:50:18 +04:00
// Returns canonical string representation of UUID:
2013-06-18 14:53:23 +04:00
// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
2013-06-18 13:50:18 +04:00
func (u *UUID) String() string {
return fmt.Sprintf("%x-%x-%x-%x-%x",
u[:4], u[4:6], u[6:8], u[8:10], u[10:])
}
2013-06-18 17:24:02 +04:00
// Sets version bits.
2013-07-09 14:35:21 +04:00
func (u *UUID) SetVersion(v byte) {
2013-06-18 14:53:23 +04:00
u[6] = (u[6] & 0x0f) | (v << 4)
2013-06-18 13:50:18 +04:00
}
2013-06-18 17:24:02 +04:00
// Sets variant bits as described in RFC 4122.
2013-07-09 14:35:21 +04:00
func (u *UUID) SetVariant() {
2013-06-18 14:53:23 +04:00
u[8] = (u[8] & 0xbf) | 0x80
2013-06-18 13:50:18 +04:00
}
// Returns UUID epoch timestamp
func getTimestamp() uint64 {
timeNow := epochStart + epochFunc()
// Clock changed backwards since last UUID generation.
// Should increase clock sequence.
if timeNow <= lastTime {
clockSequence++
}
lastTime = timeNow
return timeNow
}
2013-06-18 18:58:10 +04:00
// Returns UUID based on current timestamp and MAC address.
func NewV1() (*UUID, error) {
u := new(UUID)
2013-06-18 18:58:10 +04:00
timeNow := getTimestamp()
2013-06-18 18:58:10 +04:00
binary.BigEndian.PutUint32(u[0:], uint32(timeNow))
binary.BigEndian.PutUint16(u[4:], uint16(timeNow>>32))
binary.BigEndian.PutUint16(u[6:], uint16(timeNow>>48))
2013-06-18 18:58:10 +04:00
binary.BigEndian.PutUint16(u[8:], clockSequence)
copy(u[10:], hardwareAddr[:])
2013-06-18 18:58:10 +04:00
2013-07-09 14:35:21 +04:00
u.SetVersion(1)
u.SetVariant()
return u, nil
2013-06-18 18:58:10 +04:00
}
// Returns DCE Security UUID based on POSIX UID/GID.
func NewV2(domain byte) (*UUID, error) {
u := new(UUID)
switch domain {
case DomainPerson:
binary.BigEndian.PutUint32(u[0:], posixUID)
case DomainGroup:
binary.BigEndian.PutUint32(u[0:], posixGID)
default:
return nil, errors.New("Unsupported domain")
}
timeNow := getTimestamp()
binary.BigEndian.PutUint16(u[4:], uint16(timeNow>>32))
binary.BigEndian.PutUint16(u[6:], uint16(timeNow>>48))
binary.BigEndian.PutUint16(u[8:], clockSequence)
u[9] = domain
copy(u[10:], hardwareAddr[:])
2013-07-09 14:35:21 +04:00
u.SetVersion(2)
u.SetVariant()
return u, nil
}
2013-06-18 17:23:53 +04:00
// Returns UUID based on MD5 hash of namespace UUID and name.
func NewV3(ns *UUID, name string) (*UUID, error) {
u := newFromHash(md5.New(), ns, name)
2013-07-09 14:35:21 +04:00
u.SetVersion(3)
u.SetVariant()
return u, nil
2013-06-18 17:23:53 +04:00
}
2013-06-18 17:24:02 +04:00
// Returns random generated UUID.
func NewV4() (*UUID, error) {
u := new(UUID)
_, err := rand.Read(u[:])
if err != nil {
return nil, err
}
2013-07-09 14:35:21 +04:00
u.SetVersion(4)
u.SetVariant()
return u, nil
2013-06-18 13:50:18 +04:00
}
2013-06-18 17:23:53 +04:00
// Returns UUID based on SHA-1 hash of namespace UUID and name.
func NewV5(ns *UUID, name string) (*UUID, error) {
u := newFromHash(sha1.New(), ns, name)
2013-07-09 14:35:21 +04:00
u.SetVersion(5)
u.SetVariant()
return u, nil
2013-06-18 17:23:53 +04:00
}
2013-06-18 19:08:28 +04:00
// Returns UUID based on hashing of namespace UUID and name.
func newFromHash(h hash.Hash, ns *UUID, name string) *UUID {
u := new(UUID)
2013-06-18 17:23:53 +04:00
h.Write(ns[:])
h.Write([]byte(name))
copy(u[:], h.Sum(nil))
return u
2013-06-18 17:23:53 +04:00
}