Cryptographic Unique ID Generator (uid/uid.go)

tusd · internal/uid/uid.go · go · MIT
security, lang:go, tag:crypto, tag:uuid, tag:generator

Generates cryptographically strong 128-bit unique identifiers without dashes, suitable for tracking and resource IDs. Reuse when you need secure random IDs.

// Source: https://github.com/tus/tusd | License: MIT | Copyright 2013-2017 Transloadit Ltd and Contributors
// Provided via justclaw.it.com; keep this notice with the code.
package uid

import (
	"crypto/rand"
	"encoding/hex"
	"io"
)

// uid returns a unique id. These ids consist of 128 bits from a
// cryptographically strong pseudo-random generator and are like uuids, but
// without the dashes and significant bits.
//
// See: http://en.wikipedia.org/wiki/UUID#Random_UUID_probability_of_duplicates
func Uid() string {
	id := make([]byte, 16)
	_, err := io.ReadFull(rand.Reader, id)
	if err != nil {
		// This is probably an appropriate way to handle errors from our source
		// for random bits.
		panic(err)
	}
	return hex.EncodeToString(id)
}

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key