shortid

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2023 License: BSD-3-Clause Imports: 2 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 uses standard Go libraries to generate random bytes and to encode these bytes into a base64 URL-friendly format.

The primary function in this package is `Generate`, which 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.

It is important to note that the length of the final ID is not directly related to the entropy of the random bytes generated. The `Generate` function calculates the number of random bytes to generate based on the desired length of the encoded output, taking into account the base64 encoding expansion.

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)

The `Generate` function is designed to be simple to use and does not require any setup or initialization. It can be called as needed to generate unique identifiers for various components of an application, such as URL shortening services, database keys, or any other system that requires random, hard-to-guess identifiers.

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 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.

Types

This section is empty.

Jump to

Keyboard shortcuts

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