Documentation ¶
Overview ¶
Package stringkey provides the ability to create a simple unique hash 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 leading, trailing, and multiple spaces.
This package is designed for working with a small number of strings and is not intended 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 ¶
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 ¶
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 ¶
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 ¶
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