hashids

package module
v0.0.0-...-640f3d1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2023 License: MIT Imports: 4 Imported by: 0

README

Hashids

Go Report Card Workflow codecov Maintainability Quality Gate Status Go Reference

Hashids is a package to convert ID into a random string to obfuscate the real ID from user. In the implementation itself, the ID will still be an integer. But, when it is shown to the user, it becomes a random string. The generated random string can be decoded back to the original ID. This project uses https://github.com/speps/go-hashids as the backend.

Installation

go get github.com/indrasaputra/hashids

Example

Let there be a struct:

type Product struct {
    ID      hashids.ID  `json:"id"`
    Name    string      `json:"name"`
}

Then we have an instance of Product like this:

product := &Product{
    ID: hashids.ID(66),
    Name: "Product's name",
}

When the product is marshalled into a JSON, the ID will not be a plain integer. It will become a random string like this:

{
    "id": "kmzwa8awaa",
    "name": "product's name"
}

Upon decoding the ID, Hashids will decode back the random string to the original ID.

var product Product
b := []byte(`{"id": "kmzwa8awaa","name": "product's name"}`)
json.Unmarshal(b, &product)

The code above will fill the product's attributes like this:

{66 product's name}

Limitation

For now, this package only support encoding to JSON, decoding from JSON, and encode as a string.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EncodeID

func EncodeID(id ID) ([]byte, error)

EncodeID encodes ID into a random string (hash).

func SetHasher

func SetHasher(hash *HashID)

SetHasher sets the hasher. If this method is never called, the package will use default hasher. The default hasher is NewHashID(10, "common-salt").

Types

type Hash

type Hash interface {
	// Encode encodes the ID into a slice of byte.
	// The slice of byte generated is the result of Hashids algorithm.
	Encode(ID) ([]byte, error)
	// Decode decodes the slice of byte into an ID.
	Decode([]byte) (ID, error)
}

Hash defines the contract to encode and decode the ID.

type HashID

type HashID struct {
	// contains filtered or unexported fields
}

HashID can be used to encode and decode hashids. It implements the Hash interface.

func NewHashID

func NewHashID(minLength uint, salt string) (*HashID, error)

NewHashID creates an instance of HashID. It needs two parameters. The minimum length is used to define the mininum length of generated string. The salt is used to add the uniqueness of the generated hash.

func (*HashID) Decode

func (h *HashID) Decode(hash []byte) (ID, error)

Decode decodes the slice of byte into an ID.

func (*HashID) Encode

func (h *HashID) Encode(id ID) ([]byte, error)

Encode encodes the ID into a slice of byte.

type ID

type ID uint64

ID represents a unique identifier. It means to replace the old int64 as unique ID. Using this type allows the int64 to be obfuscated into a random string using the Hashids algorithm. Read more about hashids in https://hashids.org/.

func DecodeHash

func DecodeHash(hash []byte) (ID, error)

DecodeHash decodes hash into an ID.

func (ID) EncodeString

func (id ID) EncodeString() string

EncodeString encodes ID to hashsids format and returns as a string. It ignores the error coming from encoding process. Thus, if there is any error during the process, it returns empty string.

func (ID) MarshalJSON

func (id ID) MarshalJSON() ([]byte, error)

MarshalJSON marshals the ID to JSON.

func (*ID) UnmarshalJSON

func (id *ID) UnmarshalJSON(hash []byte) error

UnmarshalJSON unmarshals the JSON back to ID.

Jump to

Keyboard shortcuts

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