hashkey

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2024 License: MIT Imports: 2 Imported by: 0

README

A2Y-D5L / hashkey

Go Reference Go Report Card

hashkey provides a simple, concurrency-safe way to generate deterministic hash keys from strings. Hashes are produced using the FNV-1a algorithm implemented in the Go standard library at hash/fnv.

NOTE:

The FNV hash was designed for fast hash table and checksum use, not cryptography.

Fowler–Noll–Vo hash function - Wikipedia

Installation

To use the hashkey library, first ensure you have Go installed on your machine. Then, import the library into your Go project:

import "github.com/A2Y-D5L/hashkey"

Usage

The library provides two functions:

  • String(s string) (string, error): Returns a hexadecimal string representation of the hash of the input string.
  • Uint64(s string) (uint64, error): Returns the raw uint64 hash of the input string.
Examples
Generate a string
package main

import (
    "fmt"
    "github.com/A2Y-D5L/hashkey"
)

func main() {
    hash, err := hashkey.String("Hello there.")
    if err != nil {
        fmt.Println("Error generating hashkey:", err)
        return
    }
    fmt.Printf("String hash key: \"%s\"\n", hash) // -> String hash key: "892362dd056bbf55"
Generate a uint64
package main

import (
    "fmt"
    "github.com/A2Y-D5L/hashkey"
)

func main() {
    hash, err := hashkey.Uint64("General Kenobi! You are a bold one.")
    if err != nil {
        fmt.Println("Error generating hashkey:", err)
        return
    }
    fmt.Printf("Uint64 hash key: %x\n", hash) // -> Uint64 hash key: 0x58d72f7088dae07
}

Concurrency-Safety

The hashkey library is designed to be safe for concurrent use. Each call to String or Uint64 operates independently, using its instance of the FNV-1a hash function, ensuring no shared state between calls.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func String

func String(s string) (string, error)

String returns a hashkey for the given string

Example
h, err := String("Hello there.")
if err != nil {
	panic(err)
}
fmt.Println(h)
Output:

892362dd056bbf55

func Uint64

func Uint64(s string) (uint64, error)
Example
h, err := Uint64("General Kenobi! You are a bold one.")
if err != nil {
	panic(err)
}
fmt.Println(h)
Output:

400102347231833607

Types

This section is empty.

Jump to

Keyboard shortcuts

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