stringkey

package
v1.89.0 Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package stringkey provides the ability to create a simple unique key from multiple strings.

This package uses the farmhash64 algorithm to create a 64 bit hash from the concatenation of input strings. The input strings are tab-concatenated, unicode-normalized, converted to lowercase and stripped of multiple, leading and trailing spaces.

This package only works with few small strings and it is not intended to be used for cryptographic purposes. The total number of input bytes should be reasonably small to be compatible with a 64 bit hash.

According to the birthday problem, with a 64 bit hash, the hash space is 1.8x10^19 and the probability of a collision is 1% with 6.1×10^8 keys and 50% with 5.1×10^9 keys.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type StringKey

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

StringKey stores the encoded key.

func New

func New(fields ...string) *StringKey

New encode (hash) input strings into a uint64 key.

Example
package main

import (
	"fmt"

	"github.com/Vonage/gosrvlib/pkg/stringkey"
)

func main() {
	// input strings
	args := []string{
		"0123456789",
		"abcdefghijklmnopqrstuvwxyz",
		"Lorem ipsum dolor sit amet",
	}

	// generate a new key
	sk := stringkey.New(args...)

	fmt.Println(sk)

}
Output:

2p8dmari397l8

func (*StringKey) Hex

func (sk *StringKey) Hex() string

Hex returns a fixed-length 16 digits hexadecimal string key.

Example
package main

import (
	"fmt"

	"github.com/Vonage/gosrvlib/pkg/stringkey"
)

func main() {
	// generate a new key as fixed-length 16 digits hexadecimal string key.
	k := stringkey.New(
		"0123456789",
		"abcdefghijklmnopqrstuvwxyz",
		"Lorem ipsum dolor sit amet",
	).Hex()

	fmt.Println(k)

}
Output:

b19b688e8e3229ac

func (*StringKey) Key

func (sk *StringKey) Key() uint64

Key returns a uint64 key.

Example
package main

import (
	"fmt"

	"github.com/Vonage/gosrvlib/pkg/stringkey"
)

func main() {
	// generate a new key as uint64
	k := stringkey.New(
		"0123456789",
		"abcdefghijklmnopqrstuvwxyz",
		"Lorem ipsum dolor sit amet",
	).Key()

	fmt.Println(k)

}
Output:

12797937727583693228

func (*StringKey) String

func (sk *StringKey) String() string

String returns a variable-length string key.

Example
package main

import (
	"fmt"

	"github.com/Vonage/gosrvlib/pkg/stringkey"
)

func main() {
	// generate a new key as 36-char encoded string
	k := stringkey.New(
		"0123456789",
		"abcdefghijklmnopqrstuvwxyz",
		"Lorem ipsum dolor sit amet",
	).String()

	fmt.Println(k)

}
Output:

2p8dmari397l8

Jump to

Keyboard shortcuts

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