Top Related Projects
Go package for UUIDs based on RFC 4122 and DCE 1.1: Authentication and Security Services.
Universally Unique Lexicographically Sortable Identifier (ULID) in Go
xid is a globally unique id generator thought for the web
K-Sortable Globally Unique IDs
:mushroom: A generator library for concise, unambiguous and URL-safe UUIDs
Quick Overview
The go-hashids
library is a Go implementation of the Hashids algorithm, which is a way to obfuscate numerical IDs into short, unique, non-sequential, URL-friendly strings. This library can be useful for generating short, unique identifiers for resources in web applications or APIs.
Pros
- Concise and Readable Hashes: The generated hashes are short, unique, and easy to read and remember.
- Customizable: The library allows for customization of the alphabet used to generate the hashes, as well as the minimum and maximum length of the hashes.
- Reversible: The hashes can be easily decoded back to their original numerical values.
- Efficient: The library is written in Go, which is known for its performance and efficiency.
Cons
- Limited to Integers: The library can only encode and decode integer values, and not arbitrary data.
- Potential for Collisions: While the hashes are designed to be unique, there is still a possibility of collisions, especially for large datasets.
- Dependency on External Library: The library depends on the
speps/go-hashids
package, which may not be suitable for all projects. - Limited Documentation: The library's documentation could be more comprehensive, especially for advanced use cases.
Code Examples
Here are a few examples of how to use the go-hashids
library:
// Encoding an integer
h := hashids.NewData()
h.Salt = "this is my salt"
h.MinLength = 4
hid, _ := hashids.Encode(h, []int{123, 456, 789})
fmt.Println(hid) // Output: "aBcd9eF0"
// Decoding a hash
h := hashids.NewData()
h.Salt = "this is my salt"
h.MinLength = 4
numbers, _ := hashids.Decode(h, "aBcd9eF0")
fmt.Println(numbers) // Output: [123 456 789]
// Encoding multiple integers with custom alphabet
h := hashids.NewData()
h.Salt = "this is my salt"
h.MinLength = 4
h.Alphabet = "abcdefghijklmnopqrstuvwxyz"
hid, _ := hashids.Encode(h, []int{123, 456, 789})
fmt.Println(hid) // Output: "abcd9efg"
// Decoding a hash with custom alphabet
h := hashids.NewData()
h.Salt = "this is my salt"
h.MinLength = 4
h.Alphabet = "abcdefghijklmnopqrstuvwxyz"
numbers, _ := hashids.Decode(h, "abcd9efg")
fmt.Println(numbers) // Output: [123 456 789]
Getting Started
To use the go-hashids
library in your Go project, follow these steps:
- Install the library using the Go package manager:
go get github.com/speps/go-hashids
- Import the library in your Go file:
import "github.com/speps/go-hashids"
- Create a new
HashData
instance and configure it with your desired settings:
h := hashids.NewData()
h.Salt = "this is my salt"
h.MinLength = 4
- Use the
Encode
andDecode
functions to generate and decode hashes:
hid, _ := hashids.Encode(h, []int{123, 456, 789})
fmt.Println(hid) // Output: "aBcd9eF0"
numbers, _ := hashids.Decode(h, "aBcd9eF0")
fmt.Println(numbers) // Output: [123 456 789]
That's the basic usage of the go-hashids
library. You can further customize the library by setting the Alphabet
and other properties of the HashData
instance.
Competitor Comparisons
Go package for UUIDs based on RFC 4122 and DCE 1.1: Authentication and Security Services.
Pros of uuid
- Generates truly unique identifiers with extremely low collision probability
- Widely recognized and adopted standard for unique identifiers
- Supports multiple UUID versions (v1, v3, v4, v5)
Cons of uuid
- Generates longer, less human-readable identifiers
- Not designed for encoding custom data or creating short URLs
Code Comparison
uuid:
import "github.com/google/uuid"
id := uuid.New()
fmt.Println(id.String())
go-hashids:
import "github.com/speps/go-hashids"
hd := hashids.NewData()
h, _ := hashids.NewWithData(hd)
id, _ := h.Encode([]int{45, 434, 1313})
fmt.Println(id)
Key Differences
- uuid generates random, unique identifiers, while go-hashids encodes and decodes custom data
- uuid produces longer, alphanumeric strings, whereas go-hashids creates shorter, customizable identifiers
- uuid is better suited for generating unique database keys, while go-hashids is ideal for creating short URLs or obfuscating sequential IDs
Use Cases
uuid:
- Generating unique identifiers for database records
- Creating globally unique identifiers for distributed systems
go-hashids:
- Creating short, shareable URLs
- Obfuscating sequential IDs in public-facing applications
- Encoding and decoding custom data in a reversible manner
Universally Unique Lexicographically Sortable Identifier (ULID) in Go
Pros of ulid
- Generates sortable, time-based identifiers
- Provides built-in monotonicity support
- Offers better performance and lower memory allocation
Cons of ulid
- Less flexible encoding options compared to go-hashids
- Not designed for obfuscation or short URL generation
- Limited customization of output format
Code Comparison
ulid:
id := ulid.MustNew(ulid.Timestamp(time.Now()), entropy)
fmt.Printf("ULID: %s\n", id)
go-hashids:
hd := hashids.NewData()
h, _ := hashids.NewWithData(hd)
e, _ := h.Encode([]int{45, 434, 1313, 99})
fmt.Printf("Hashid: %s\n", e)
Key Differences
- ulid focuses on generating unique, sortable identifiers
- go-hashids emphasizes obfuscation and reversible encoding
- ulid is more suitable for distributed systems and time-based sorting
- go-hashids offers more flexibility in output format and encoding alphabet
Use Cases
ulid:
- Distributed systems requiring sortable IDs
- Time-based event logging and ordering
go-hashids:
- URL shortening services
- Obfuscating database IDs in public-facing applications
Both libraries have their strengths, and the choice depends on specific project requirements and use cases.
xid is a globally unique id generator thought for the web
Pros of xid
- Generates globally unique IDs, unlike hashids which focuses on obfuscation
- Sortable by creation time, providing a chronological order
- Compact 20-character string representation, efficient for storage and transmission
Cons of xid
- Less customizable than hashids, which allows for custom alphabets and salt
- Not designed for obfuscation or encoding of existing IDs, unlike hashids
- Fixed format, while hashids can generate variable-length IDs
Code Comparison
xid:
id := xid.New()
fmt.Printf("Generated ID: %s\n", id.String())
hashids:
hd := hashids.NewData()
h, _ := hashids.NewWithData(hd)
e, _ := h.Encode([]int{45, 434, 1313})
fmt.Printf("Encoded: %s\n", e)
xid generates unique IDs, while hashids encodes existing numbers. xid's usage is simpler, requiring no configuration, whereas hashids needs initialization with optional parameters. xid produces fixed-length strings, while hashids can create variable-length encodings based on input.
K-Sortable Globally Unique IDs
Pros of ksuid
- Generates sortable, time-ordered unique identifiers
- Provides better performance and scalability for distributed systems
- Includes a command-line tool for generating and inspecting KSUIDs
Cons of ksuid
- Less flexibility in customizing the output format
- Not designed for creating short, URL-friendly IDs
- May be overkill for simple use cases where a basic unique identifier is sufficient
Code Comparison
ksuid:
id := ksuid.New()
fmt.Printf("ID: %s\n", id.String())
go-hashids:
hd := hashids.NewData()
h, _ := hashids.NewWithData(hd)
e, _ := h.Encode([]int{45, 434, 1313})
fmt.Printf("Encoded: %s\n", e)
Key Differences
- ksuid focuses on generating unique, sortable identifiers for distributed systems
- go-hashids is designed for creating short, URL-friendly IDs from integers
- ksuid includes timestamp information, while go-hashids does not
- go-hashids offers more customization options for the output format
- ksuid provides better performance for high-volume ID generation
Use Cases
- ksuid: Ideal for distributed systems, time-series data, and scenarios requiring sortable IDs
- go-hashids: Better suited for creating short, obfuscated IDs for URLs or public-facing identifiers
:mushroom: A generator library for concise, unambiguous and URL-safe UUIDs
Pros of shortuuid
- Generates UUIDs that are shorter and more URL-friendly
- Provides flexibility in choosing encoding alphabets
- Supports custom random number generators
Cons of shortuuid
- Does not offer built-in collision resistance like Hashids
- Limited customization options compared to Hashids
- May not be suitable for applications requiring reversible IDs
Code Comparison
shortuuid:
id := shortuuid.New()
fmt.Println(id) // Output: something like "3k4NJN6tJrqk9XRkgXWB2w"
go-hashids:
hd := hashids.NewData()
h, _ := hashids.NewWithData(hd)
e, _ := h.Encode([]int{45, 434, 1313, 99})
fmt.Println(e) // Output: something like "7nnhzEsDkiJqrZRCByXiR2"
Summary
shortuuid focuses on generating short, unique identifiers based on UUIDs, while go-hashids specializes in creating reversible, collision-resistant hash IDs. shortuuid is simpler to use for basic unique ID generation, whereas go-hashids offers more customization and the ability to encode and decode integer arrays. Choose shortuuid for straightforward, URL-friendly unique IDs, and go-hashids for applications requiring reversible IDs or encoding multiple values.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
go-hashids
Go (golang) v1 implementation of http://www.hashids.org under MIT License (same as the original implementations)
Original implementations by Ivan Akimov
Setup
go get github.com/speps/go-hashids/v2
CLI tool :
go get github.com/speps/go-hashids/v2/cmd/hashid
Example
package main
import "fmt"
import "github.com/speps/go-hashids/v2"
func main() {
hd := hashids.NewData()
hd.Salt = "this is my salt"
hd.MinLength = 30
h, _ := hashids.NewWithData(hd)
e, _ := h.Encode([]int{45, 434, 1313, 99})
fmt.Println(e)
d, _ := h.DecodeWithError(e)
fmt.Println(d)
}
Thanks to all the contributors
- Harm Aarts
- Christoffer G. Thomsen
- Peter Hellberg
- Rémy Oudompheng
- Mart Roosmaa
- Jakub Kramarz
- Zou Xifeng
- Per Persson
- Baiju Muthukadan
- Pablo de la Concepción Sanz
- Olivier Mengué
- Matthew Valimaki
- Cody Maloney
Let me know if I forgot anyone of course.
Changelog
2021/05/04
- v2.0.1 - Added module support with /v2 suffix
2017/05/09
- Changed API
New
methods now return errors- Added sanity check in
Decode
that makes sure that the salt is consistent
2014/09/13
- Updated to Hashids v1.0.0 (should be compatible with other implementations, let me know if not, was checked against the Javascript version)
- Changed API
- Encrypt/Decrypt are now Encode/Decode
- HashID is now constructed from HashIDData containing alphabet, salt and minimum length
Top Related Projects
Go package for UUIDs based on RFC 4122 and DCE 1.1: Authentication and Security Services.
Universally Unique Lexicographically Sortable Identifier (ULID) in Go
xid is a globally unique id generator thought for the web
K-Sortable Globally Unique IDs
:mushroom: A generator library for concise, unambiguous and URL-safe UUIDs
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot