randomhash

package module
v0.0.0-...-37600aa Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2018 License: MIT Imports: 5 Imported by: 0

README ΒΆ

random-hash

Generate random hashes with a fully customizable charset and configurable length

It is a GO port of https://github.com/PabloSichert/random-hash

Usage

    //default charset abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_

    randomHash := randomhash.New("")
	result, err := randomHash.GenerateHash(10) //setting hash length
	if err != nil {
		panic(err)
	}
    fmt.Printf("random hash %s", result) //Dgc2iUZLeR
    
    
    charset := "πŸ˜πŸ˜ŽπŸ˜πŸ˜‡πŸ€“πŸ€”πŸ˜΄πŸ˜πŸ€‘πŸ€’πŸ˜­πŸ˜ˆπŸ‘»πŸ‘½πŸ€–πŸ’©πŸŽ…πŸ’ͺπŸ‘ˆπŸ‘‰πŸ‘†πŸ‘‡βœŒβœ‹πŸ‘ŒπŸ‘πŸ‘ŽπŸ‘πŸ‘‚πŸ‘ƒπŸ‘£πŸ‘"
    randomHash := randomhash.New(charset)
	result, err := randomHash.GenerateHash(5) //setting hash length
	if err != nil {
		panic(err)
	}
    fmt.Printf("random hash %s", result) //πŸ€‘βœ‹πŸ€’πŸ˜πŸ€‘

Algorithm for generateHash

|----------- a1 ----------| ... |------------------------- a(n) ------------------------|
| b1 b2 b3 b4 b5 b6 b7 b8 | ... | b(n-7) b(n-6) b(n-5) b(n-4) b(n-3) b(n-2) b(n-1) b(n) |
|--- c1 ---|                               ...                               |-- c(n) --|

a(n): binary with 8 digits
b(n): binary from RNG
c(n): binary with log2(charset.length) digits

a1 ... a(ceil(hashLength * log2(charset.length) / 8))
b1 ... b(ceil(hashLength * log2(charset.length) / 8) * 8)
c1 ... c(hashLength)
charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
length = 4

|------- a1 ------|------- b2 ------|------- c3 ------|
| 0 1 1 0 0 0 1 1 | 0 1 0 1 0 1 0 0 | 0 0 0 0 0 0 0 1 | random bytes
|---- c1 ----|----- c2 ----|----- c3 ----|---- c4 ----|
|----- y ----|----- 1 -----|----- q -----|----- b ----| = "y1qb"
Example

charset = "πŸ˜πŸ˜ŽπŸ˜πŸ˜‡πŸ€“πŸ€”πŸ˜΄πŸ˜πŸ€‘πŸ€’πŸ˜­πŸ˜ˆπŸ‘»πŸ‘½πŸ€–πŸ’©πŸŽ…πŸ’ͺπŸ‘ˆπŸ‘‰πŸ‘†πŸ‘‡βœŒβœ‹πŸ‘ŒπŸ‘πŸ‘ŽπŸ‘πŸ‘‚πŸ‘ƒπŸ‘£πŸ‘"

length = 3

|------- a1 ------|------- b2 ------|
| 0 0 0 1 1 0 1 1 | 0 0 1 0 1 1 1 0 | random bytes
|--- c1 ---|---- c2 ---|--- c3 --|
|--- πŸ˜‡ ---|---- πŸ‘» ----|--- βœ‹ ---| = "πŸ˜‡πŸ‘»βœ‹"

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

This section is empty.

Types ΒΆ

type RandomHash ΒΆ

type RandomHash struct {
	Charset []rune
	// contains filtered or unexported fields
}

RandomHash RandomHash

func New ΒΆ

func New(charset string) *RandomHash

New get new RandomHash

func (*RandomHash) GenerateHash ΒΆ

func (randomHash *RandomHash) GenerateHash(length int) (string, error)

GenerateHash generate the length hash string

Jump to

Keyboard shortcuts

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