shortid

package
v0.4.10 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2023 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package shortid provides functionality to generate cryptographically secure, URL-friendly unique identifiers. These identifiers can be used as part of URLs without encoding issues. The package leverages standard Go libraries to generate random bytes and to encode these bytes into a base64 URL-friendly format without padding.

Functions:

  • Generate(length int): Generates a short ID of the specified length.
  • GenerateUnique(ctx context.Context, client *datastore.Client, length int): Generates a unique short ID.

Internal Functions:

  • generateRandomString(length int): Generates a random, URL-friendly string of the specified length.

The Generate function produces a short ID with a specified length. This length refers to the number of characters in the encoded string, which is URL-safe and does not include base64 padding characters. The function ensures that the output is of the desired length, even if this requires generating and encoding additional random bytes. The actual entropy and uniqueness of the generated IDs depend on the specified length. Shorter IDs have a higher chance of collision, so it is important to choose an appropriate length based on the use case and the expected number of IDs to be generated.

The GenerateUnique function ensures the uniqueness of the ID by checking against a datastore, using the provided context and client. It retries up to a maximum number of times defined by maxRetries.

Example usage:

id, err := shortid.Generate(10)
if err != nil {
    log.Fatalf("Failed to generate short ID: %v", err)
}
fmt.Println("Generated short ID:", id)

id, err = shortid.GenerateUnique(context.Background(), datastoreClient, 10)
if err != nil {
    log.Fatalf("Failed to generate unique short ID: %v", err)
}
fmt.Println("Generated unique short ID:", id)

The generated IDs are cryptographically secure as they are based on random bytes generated by the crypto/rand package, which is suitable for security-sensitive applications.

Note: The actual entropy and uniqueness of the generated IDs depend on the specified length. Shorter IDs have a higher chance of collision, so it is important to choose an appropriate length based on the use case and the expected number of IDs to be generated.

Copyright (c) 2023 by H0llyW00dzZ

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(length int) (string, error)

Generate creates a cryptographically secure, URL-friendly short ID of a specified length.

func GenerateUniqueDataStore added in v0.4.5

func GenerateUniqueDataStore(ctx context.Context, client *datastore.Client, length int) (string, error)

GenerateUniqueDataStore creates a unique, cryptographically secure, URL-friendly short ID.

Note: This function has been renamed to GenerateUniqueDataStore to avoid confusion with similarly named 'Generate' functions for other databases in the future.

Types

This section is empty.

Jump to

Keyboard shortcuts

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