friendlyhash

package module
v0.0.0-...-1ca64b3 Latest Latest
Warning

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

Go to latest
Published: May 22, 2019 License: MIT Imports: 5 Imported by: 1

README ΒΆ

Friendly hash GoDoc Build Status codecov

Friendly hash is a Go library which implements human-readable and reversible representation of known-length byte slices. It can be used to represent hashes in a human-readable way.

The core idea

This library aims to make hashes friendlier to humans. As an example the following hash:

9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08

Can be instead represented in the following way to make it more visually recognizable:

swiss.laboratory.mostly.parks.inches.therapy.homes.preferred.victory.applicant.making.leading.documentation.ownership.every.models.expense.targets.picture.series.return.signature

Or even displayed using emoji:

🏘 πŸ“  πŸ₯“ 😢 😑 🀢 πŸ“‹ πŸšΆβ€β™€οΈ ⌨ πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦ 🍷 πŸ˜† πŸ‘ πŸ€ 🍣 🌈 🚍 🍏 🐬 πŸ’Ί 🚝 🏯 πŸ‘ πŸ§€ 🍸 πŸš‡ πŸ™ 😝 πŸ—

Length of the output

The table below presents the number of dictionary elements which have to be used to represent a hash of a specific size using the dictionary containing the given number of elements.

Example: 11 elements are needed to encode a hash if it is 128 bits (16 bytes) long and a dictionary of 5000 elements is used.

Dictionary size 128 bits 256 bits 512 bits
5000 11 21 42
10000 10 20 39
15000 10 19 37
20000 9 18 36
30000 9 18 35
40000 9 17 34
50000 9 17 33
60000 9 17 33
70000 8 16 32
80000 8 16 32
90000 8 16 32
100000 8 16 31

Word list

This package doesn't provide a dictionary of words used for encoding. I can recommend using one of the following lists available on Github:

If you prefer to use emoji the following library has a list of those:

Code example

dictionary := []string{"word1", "word2", "word3", "word4", "word5", "word6"}
hashSize := 2

// Create
h, err := New(dictionary, hashSize)
if err != nil {
    panic(err)
}

// Humanize
humanized, err := h.Humanize([]byte{'a', 'b'})
if err != nil {
    panic(err)
}
fmt.Println(strings.Join(humanized, "-"))

// Dehumanize
dehumanized, err := h.Dehumanize(humanized)
if err != nil {
    panic(err)
}
fmt.Printf("%q\n", dehumanized)

// Output:
// word2-word3-word1-word2-word2-word3-word1-word3
// "ab"

Documentation ΒΆ

Overview ΒΆ

Package friendlyhash implements human-readable and reversible representation of known-length byte slices.

Index ΒΆ

Examples ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

This section is empty.

Types ΒΆ

type FriendlyHash ΒΆ

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

FriendlyHash creates a human-friendly representation of byte slices.

func New ΒΆ

func New(dictionary []string, hashLength int) (*FriendlyHash, error)

New creates a struct responsible for encoding and decoding the data using the provided dictionary and hash length. The dictionary should be a list of at least two unique strings, usually words. To decode once encoded data an identical dictionary has to be used. Hash length is required to avoid including the length of the data in the resulting representation or using padding characters.

Example ΒΆ
// Create
dictionary := []string{
	"word1",
	"word2",
	"word3",
	"word4",
	"word5",
	"word6",
}

h, err := New(dictionary, 2)
if err != nil {
	panic(err)
}

// Humanize
humanized, err := h.Humanize([]byte{'a', 'b'})
if err != nil {
	panic(err)
}
fmt.Println(strings.Join(humanized, "-"))

// Dehumanize
dehumanized, err := h.Dehumanize(humanized)
if err != nil {
	panic(err)
}
fmt.Printf("%q\n", dehumanized)
Output:

word2-word3-word1-word2-word2-word3-word1-word3
"ab"

func (*FriendlyHash) Dehumanize ΒΆ

func (h *FriendlyHash) Dehumanize(words []string) ([]byte, error)

Dehumanize converts the provided list of words previously created using the humanize function back to its byte slice equivalent.

func (*FriendlyHash) Humanize ΒΆ

func (h *FriendlyHash) Humanize(hash []byte) ([]string, error)

Humanize encodes the provided byte slice and returns its representation as a list of words from the dictionary.

func (*FriendlyHash) NumberOfBytes ΒΆ

func (h *FriendlyHash) NumberOfBytes() int

NumberOfBytes returns the hash length returned by the dehumanize function.

func (*FriendlyHash) NumberOfWords ΒΆ

func (h *FriendlyHash) NumberOfWords() int

NumberOfWords returns the number of words returned by the humanize function.

Jump to

Keyboard shortcuts

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