uuid

package
v0.0.0-...-9649b88 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2020 License: Apache-2.0 Imports: 4 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// NIL is defined in RFC 4122 section 4.1.7.
	// The nil UUID is special form of UUID that is specified to have all 128 bits set to zero.
	NIL = &UUID{
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	}
)

Functions

func GetUUID8Byte

func GetUUID8Byte(s string) string

func NewUUID

func NewUUID() string
func TestNewV5(t *testing.T) {
	namespace := NewNamespaceUUID("test")
	uuid := NewV5(namespace, []byte("test name"))
	if uuid.Version() != 5 {
		t.Errorf("invalid version %d - expected 5", uuid.Version())
	}
	t.Logf("UUID V5: %s", uuid)
}

// NewV4 creates a new UUID with variant 4 as described in RFC 4122. // Variant 4 based on pure random bytes.

func NewV4() *UUID {
	buf := make([]byte, 16)
	rand.Read(buf)
	buf[6] = (buf[6] & 0x0f) | 0x40
	var uuid UUID
	copy(uuid[:], buf[:])
	uuid.variantRFC4122()
	return &uuid
}

/*

func TestNewV4(t *testing.T) {
	uuid := NewV4()
	if uuid.Version() != 4 {
		t.Errorf("invalid version %d - expected 4", uuid.Version())
	}
	t.Logf("UUID V4: %s", uuid)
}

// NewV3 creates a new UUID with variant 3 as described in RFC 4122. // Variant 3 based namespace-uuid and name and MD-5 hash calculation.

func NewV3(namespace *UUID, name []byte) *UUID {
	uuid := newByHash(md5.New(), namespace, name)
	uuid[6] = (uuid[6] & 0x0f) | 0x30
	return uuid
}

/*

func TestNewV3(t *testing.T) {
	namespace := NewNamespaceUUID("test")
	uuid := NewV3(namespace, []byte("test name"))
	if uuid.Version() != 3 {
		t.Errorf("invalid version %d - expected 3", uuid.Version())
	}
	t.Logf("UUID V3: %s", uuid)
}

NewNamespaceUUID creates a namespace UUID by using the namespace name in the NIL name space. This is a different approach as the 4 "standard" namespace UUIDs which are timebased UUIDs (V1).

func NewNamespaceUUID(namespace string) string {
	namespace = time.Now().String()
	return NewV5(NIL, []byte(namespace)).String()
}

Types

type UUID

type UUID [16]byte

func NewV5

func NewV5(namespaceUUID *UUID, name []byte) *UUID

NewV5 creates a new UUID with variant 5 as described in RFC 4122. Variant 5 based namespace-uuid and name and SHA-1 hash calculation.

func (*UUID) String

func (u *UUID) String() string

String returns the human readable form of the UUID.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL