hashids

package module
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2018 License: MIT Imports: 4 Imported by: 285

README

go-hashids Build Status GoDoc

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

CLI tool :

go get github.com/speps/go-hashids/cmd/hashid
Example
package main

import "fmt"
import "github.com/speps/go-hashids"

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

Let me know if I forgot anyone of course.

Changelog

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

Documentation

Index

Constants

View Source
const (
	// Version is the version number of the library
	Version string = "1.0.0"

	// DefaultAlphabet is the default alphabet used by go-hashids
	DefaultAlphabet string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type HashID

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

HashID contains everything needed to encode/decode hashids

func New

func New() (*HashID, error)

New creates a new HashID

func NewWithData

func NewWithData(data *HashIDData) (*HashID, error)

NewWithData creates a new HashID with the provided HashIDData

func (*HashID) Decode

func (h *HashID) Decode(hash string) []int

DEPRECATED: Use DecodeWithError instead Decode unhashes the string passed to an array of int. It is symmetric with Encode if the Alphabet and Salt are the same ones which were used to hash. MinLength has no effect on Decode.

func (*HashID) DecodeHex

func (h *HashID) DecodeHex(hash string) (string, error)

DecodeHex unhashes the string passed to a hexadecimal string. It is symmetric with EncodeHex if the Alphabet and Salt are the same ones which were used to hash.

Each hex nibble is decoded from an integer in range [16, 31].

func (*HashID) DecodeInt64

func (h *HashID) DecodeInt64(hash string) []int64

DEPRECATED: Use DecodeInt64WithError instead DecodeInt64 unhashes the string passed to an array of int64. It is symmetric with EncodeInt64 if the Alphabet and Salt are the same ones which were used to hash. MinLength has no effect on DecodeInt64.

func (*HashID) DecodeInt64WithError

func (h *HashID) DecodeInt64WithError(hash string) ([]int64, error)

DecodeInt64 unhashes the string passed to an array of int64. It is symmetric with EncodeInt64 if the Alphabet and Salt are the same ones which were used to hash. MinLength has no effect on DecodeInt64.

func (*HashID) DecodeWithError

func (h *HashID) DecodeWithError(hash string) ([]int, error)

Decode unhashes the string passed to an array of int. It is symmetric with Encode if the Alphabet and Salt are the same ones which were used to hash. MinLength has no effect on Decode.

func (*HashID) Encode

func (h *HashID) Encode(numbers []int) (string, error)

Encode hashes an array of int to a string containing at least MinLength characters taken from the Alphabet. Use Decode using the same Alphabet and Salt to get back the array of int.

func (*HashID) EncodeHex

func (h *HashID) EncodeHex(hex string) (string, error)

EncodeHex hashes a hexadecimal string to a string containing at least MinLength characters taken from the Alphabet. A hexadecimal string should not contain the 0x prefix. Use DecodeHex using the same Alphabet and Salt to get back the hexadecimal string.

Each hex nibble is encoded as an integer in range [16, 31].

func (*HashID) EncodeInt64

func (h *HashID) EncodeInt64(numbers []int64) (string, error)

EncodeInt64 hashes an array of int64 to a string containing at least MinLength characters taken from the Alphabet. Use DecodeInt64 using the same Alphabet and Salt to get back the array of int64.

type HashIDData

type HashIDData struct {
	// Alphabet is the alphabet used to generate new ids
	Alphabet string

	// MinLength is the minimum length of a generated id
	MinLength int

	// Salt is the secret used to make the generated id harder to guess
	Salt string
}

HashIDData contains the information needed to generate hashids

func NewData

func NewData() *HashIDData

NewData creates a new HashIDData with the DefaultAlphabet already set.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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