File: uuid.go

package info (click to toggle)
golang-github-rogpeppe-fastuuid 0.0~git20150106.0.6724a57-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 60 kB
  • ctags: 9
  • sloc: makefile: 2
file content (66 lines) | stat: -rw-r--r-- 1,703 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Package fastuuid provides fast UUID generation of 192 bit
// universally unique identifiers. It does not provide
// formatting or parsing of the identifiers (it is assumed
// that a simple hexadecimal or base64 representation
// is sufficient, for which adequate functionality exists elsewhere).
//
// Note that the generated UUIDs are not unguessable - each
// UUID generated from a Generator is adjacent to the
// previously generated UUID.
//
// It ignores RFC 4122.
package fastuuid

import (
	"crypto/rand"
	"encoding/binary"
	"errors"
	"sync/atomic"
)

// Generator represents a UUID generator that
// generates UUIDs in sequence from a random starting
// point.
type Generator struct {
	seed    [24]byte
	counter uint64
}

// NewGenerator returns a new Generator.
// It can fail if the crypto/rand read fails.
func NewGenerator() (*Generator, error) {
	var g Generator
	_, err := rand.Read(g.seed[:])
	if err != nil {
		return nil, errors.New("cannot generate random seed: " + err.Error())
	}
	return &g, nil
}

// MustNewGenerator is like NewGenerator
// but panics on failure.
func MustNewGenerator() *Generator {
	g, err := NewGenerator()
	if err != nil {
		panic(err)
	}
	return g
}

// Next returns the next UUID from the generator.
// Only the first 8 bytes can differ from the previous
// UUID, so taking a slice of the first 16 bytes
// is sufficient to provide a somewhat less secure 128 bit UUID.
//
// It is OK to call this method concurrently.
func (g *Generator) Next() [24]byte {
	x := atomic.AddUint64(&g.counter, 1)
	var counterBytes [8]byte
	binary.LittleEndian.PutUint64(counterBytes[:], x)

	uuid := g.seed
	for i, b := range counterBytes {
		uuid[i] ^= b
	}
	return uuid
}