This code defines a Go package called `randid` for generating...

September 2, 2025 at 12:57 AM

package randid import ( "math/rand/v2" "unsafe" ) // Size is the length in bytes of the ID const Size = 16 // StringLen is the length of the string representation of ID const StringLen = 22 // ID is our 128-bit random value type ID [2]uint64 func (id ID) Bytes() [Size]byte { var out [Size]byte out[0] = byte(id[0]) out[1] = byte(id[0] >> 8) out[2] = byte(id[0] >> 16) out[3] = byte(id[0] >> 24) out[4] = byte(id[0] >> 32) out[5] = byte(id[0] >> 40) out[6] = byte(id[0] >> 48) out[7] = byte(id[0] >> 56) out[8] = byte(id[1]) out[9] = byte(id[1] >> 8) out[10] = byte(id[1] >> 16) out[11] = byte(id[1] >> 24) out[12] = byte(id[1] >> 32) out[13] = byte(id[1] >> 40) out[14] = byte(id[1] >> 48) out[15] = byte(id[1] >> 56) return out } // String returns base64 encoding of our ID func (id ID) String() string { var buf [StringLen]byte encodeUnrolled(&buf, id) // Directly convert the array to a string without an // intermediary slice return unsafe.String((*byte)(unsafe.Pointer(&buf)), StringLen) } // New generates a new random ID func New() ID { return ID{ rand.Uint64(), rand.Uint64(), } } // vendoring in a bit simpler variant of base64 url encoding // that removes some extra branches and removes the concept of padding // We're working with a fixed size and fixed character set. var encodeMap = [64]byte{ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_', } func encodeUnrolled(dst *[StringLen]byte, src ID) { // XXX: this is manually unrolled and performance is // a few percentage faster, likely the only optimization // that can be accomplished here is through ASM and utilizing // vectors and probably encode this entire thing in one // vector CPU instruction. var val uint32 // Convert 3x 8bit source bytes into 4 bytes val = (uint32(byte(src[(0+0)/8]>>((0+0)%8*8)))<<16 | uint32(byte(src[(0+1)/8]>>((0+1)%8*8)))<<8 | uint32(byte(src[(0+2)/8]>>((0+2)%8*8)))<<0) dst[0+0] = encodeMap[val>>18&0x3F] dst[0+1] = encodeMap[val>>12&0x3F] dst[0+2] = encodeMap[val>>6&0x3F] dst[0+3] = encodeMap[val&0x3F] // Convert 3x 8bit source bytes into 4 bytes val = (uint32(byte(src[(3+0)/8]>>((3+0)%8*8)))<<16 | uint32(byte(src[(3+1)/8]>>((3+1)%8*8)))<<8 | uint32(byte(src[(3+2)/8]>>((3+2)%8*8)))<<0) dst[4+0] = encodeMap[val>>18&0x3F] dst[4+1] = encodeMap[val>>12&0x3F] dst[4+2] = encodeMap[val>>6&0x3F] dst[4+3] = encodeMap[val&0x3F] // Convert 3x 8bit source bytes into 4 bytes val = (uint32(byte(src[(6+0)/8]>>((6+0)%8*8)))<<16 | uint32(byte(src[(6+1)/8]>>((6+1)%8*8)))<<8 | uint32(byte(src[(6+2)/8]>>((6+2)%8*8)))<<0) dst[8+0] = encodeMap[val>>18&0x3F] dst[8+1] = encodeMap[val>>12&0x3F] dst[8+2] = encodeMap[val>>6&0x3F] dst[8+3] = encodeMap[val&0x3F] // Convert 3x 8bit source bytes into 4 bytes val = (uint32(byte(src[(9+0)/8]>>((9+0)%8*8)))<<16 | uint32(byte(src[(9+1)/8]>>((9+1)%8*8)))<<8 | uint32(byte(src[(9+2)/8]>>((9+2)%8*8)))<<0) dst[12+0] = encodeMap[val>>18&0x3F] dst[12+1] = encodeMap[val>>12&0x3F] dst[12+2] = encodeMap[val>>6&0x3F] dst[12+3] = encodeMap[val&0x3F] // Convert 3x 8bit source bytes into 4 bytes val = (uint32(byte(src[(12+0)/8]>>((12+0)%8*8)))<<16 | uint32(byte(src[(12+1)/8]>>((12+1)%8*8)))<<8 | uint32(byte(src[(12+2)/8]>>((12+2)%8*8)))<<0) dst[16+0] = encodeMap[val>>18&0x3F] dst[16+1] = encodeMap[val>>12&0x3F] dst[16+2] = encodeMap[val>>6&0x3F] dst[16+3] = encodeMap[val&0x3F] // last byte val = uint32(src[1]>>56) << 16 dst[20+0] = encodeMap[val>>18&0x3F] dst[20+1] = encodeMap[val>>12&0x3F] }

This code defines a Go package called randid for generating and working with 128-bit random IDs, along with a base64-like encoding mechanism for their string representation.

Detailed Breakdown:

  1. ID Definition (type ID [2]uint64):

    • ID is defined as a 128-bit random value, represented using an array of two uint64 values ([2]uint64).
  2. Constants:

    • Size: Represents the length in bytes of an ID (128 bits = 16 bytes).
    • StringLen: Represents the length of the base64-encoded string representation of an ID (22 characters without padding).
  3. Converting ID to Bytes (func (id ID) Bytes() [Size]byte):

    • Converts the 128-bit ID into an array of 16 bytes.
    • The function extracts each byte from the two uint64 values in ID by applying bit-shifting and masking.
  4. Converting ID to Base64-like String (func (id ID) String() string):

    • Converts the ID into a string representation using a custom unpadded base64-like encoding.
    • The function uses the encodeUnrolled method to encode the ID into 22 characters using a manually optimized approach.
    • It uses the unsafe package to efficiently convert the encoded byte array into a string.
  5. Generating New IDs (func New() ID):

    • Generates a new 128-bit random ID using two calls to rand.Uint64() to populate the two uint64 values in the ID.
  6. Custom Base64-like Encoding (func encodeUnrolled(dst *[StringLen]byte, src ID)):

    • Encodes the ID into a fixed-length custom base64-like string without padding characters (=).
    • Utilizes a map of 64 characters (encodeMap) to represent 6-bit chunks of the input ID (base64 encoding works with 6-bit blocks).
    • The implementation is manually unrolled for performance optimization, processing chunks of 3 bytes from the ID at a time and converting them into 4 encoded characters.
  7. Custom Encoding Map (var encodeMap):

    • Defines the 64-character map used for encoding. It includes uppercase letters (A-Z), lowercase letters (a-z), digits (0-9), and two special characters (- and _)—a "URL-safe" base64-like encoding.

Key Points:

  • The main functionality of this package is to generate random 128-bit identifiers (ID), convert them to a byte representation (Bytes()), and encode them into a compact and URL-safe string representation (String()).
  • The encoding implemented here is a custom take on base64 encoding, optimized for fixed-size input (16 bytes) and omitting the need for padding.
  • The code relies on the Go math/rand package (specifically rand.Uint64()) for generating the random numbers used in the ID.

Example Usage:

To generate and work with an ID:

package main

import (
	"fmt"
	"randid"
)

func main() {
	// Generate a new random ID
	id := randid.New()
	
	// Get the byte representation
	bytes := id.Bytes()
	fmt.Printf("Bytes: %x\n", bytes)

	// Get the string representation
	str := id.String()
	fmt.Printf("String: %s\n", str)
}

This program would:

  1. Generate a random 128-bit ID.
  2. Print the ID as a 16-byte hexadecimal array.
  3. Print the URL-safe string representation (22 characters).

Summary:

This package provides a streamlined and compact way to generate, encode, and represent 128-bit random identifiers, useful for purposes like unique session tokens, object IDs, or other cases requiring collision-resistant random identifiers. The encoding used is a tailored and efficient variation of base64.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node