hash

package
v0.0.0-...-71969ea Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Epoch is set to the twitter snowflake epoch of Nov 04 2010 01:42:54 UTC in milliseconds
	// You may customize this to set a different epoch for your application.
	Epoch int64 = 1288834974657

	// NodeBits holds the number of bits to use for SnowNode
	// Remember, you have a total 22 bits to share between SnowNode/Step
	NodeBits uint8 = 10

	// StepBits holds the number of bits to use for Step
	// Remember, you have a total 22 bits to share between SnowNode/Step
	StepBits uint8 = 12
)
View Source
var ErrInvalidBase32 = errors.New("invalid base32")

ErrInvalidBase32 is returned by ParseBase32 when given an invalid []byte

View Source
var ErrInvalidBase58 = errors.New("invalid base58")

ErrInvalidBase58 is returned by ParseBase58 when given an invalid []byte

Functions

func AppendUint32

func AppendUint32(b []byte, v uint32) []byte

func NewMurmur

func NewMurmur(seed uint32) hash.Hash32

NewMurmur returns a new 32-bit FNV-1 hash.Hash. Its Sum method will lay the value out in big-endian byte order.

Types

type Fnv64

type Fnv64 struct {
}

func NewFnv64

func NewFnv64() *Fnv64

func (Fnv64) Sum64

func (f Fnv64) Sum64(key string) uint64

Sum64 gets the string and returns its uint64 hash value. hash值只和传入的字符串有关,因此能保证多线程安全

type Hasher

type Hasher interface {
	Sum64(string) uint64
}

Hasher is responsible for generating unsigned, 64 bit hash of provided string. Hasher should minimize collisions (generating same hash for different strings) and while performance is also important fast functions are preferable (i.e. you can use FarmHash family).

type JSONSyntaxError

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

A JSONSyntaxError is returned from UnmarshalJSON if an invalid ID is provided.

func (JSONSyntaxError) Error

func (j JSONSyntaxError) Error() string

type Murmur

type Murmur uint32

func (*Murmur) BlockSize

func (s *Murmur) BlockSize() int

func (*Murmur) Reset

func (s *Murmur) Reset()

func (*Murmur) Size

func (s *Murmur) Size() int

func (*Murmur) Sum

func (s *Murmur) Sum(b []byte) []byte

func (*Murmur) Sum32

func (s *Murmur) Sum32() uint32

Sum32 计算给定数据的哈希值。

func (*Murmur) Write

func (s *Murmur) Write(data []byte) (int, error)

type SnowID

type SnowID int64

An SnowID is a custom type used for a snowflake SnowID. This is used so we can attach methods onto the SnowID.

func ParseBase2

func ParseBase2(id string) (SnowID, error)

ParseBase2 converts a Base2 string into a snowflake SnowID

func ParseBase32

func ParseBase32(b []byte) (SnowID, error)

ParseBase32 parses a base32 []byte into a snowflake SnowID NOTE: There are many different base32 implementations so becareful when doing any interoperation.

func ParseBase36

func ParseBase36(id string) (SnowID, error)

ParseBase36 converts a Base36 string into a snowflake SnowID

func ParseBase58

func ParseBase58(b []byte) (SnowID, error)

ParseBase58 parses a base58 []byte into a snowflake SnowID

func ParseBase64

func ParseBase64(id string) (SnowID, error)

func ParseBytes

func ParseBytes(id []byte) (SnowID, error)

func ParseInt64

func ParseInt64(id int64) SnowID

ParseInt64 converts an int64 into a snowflake SnowID

func ParseIntBytes

func ParseIntBytes(id [8]byte) SnowID

ParseIntBytes converts an array of bytes encoded as big endian integer as a snowflake SnowID

func ParseString

func ParseString(id string) (SnowID, error)

ParseString converts a string into a snowflake SnowID

func (SnowID) Base2

func (f SnowID) Base2() string

Base2 returns a string base2 of the snowflake SnowID

func (SnowID) Base32

func (f SnowID) Base32() string

Base32 uses the z-base-32 character set but encodes and decodes similar to base58, allowing it to create an even smaller result string. NOTE: There are many different base32 implementations so becareful when doing any interoperation.

func (SnowID) Base36

func (f SnowID) Base36() string

Base36 returns a base36 string of the snowflake SnowID

func (SnowID) Base58

func (f SnowID) Base58() string

Base58 returns a base58 string of the snowflake SnowID

func (SnowID) Base64

func (f SnowID) Base64() string

func (SnowID) Bytes

func (f SnowID) Bytes() []byte

func (SnowID) Int64

func (f SnowID) Int64() int64

Int64 returns an int64 of the snowflake SnowID

func (SnowID) IntBytes

func (f SnowID) IntBytes() [8]byte

IntBytes returns an array of bytes of the snowflake SnowID, encoded as a big endian integer.

func (SnowID) MarshalJSON

func (f SnowID) MarshalJSON() ([]byte, error)

MarshalJSON returns a json byte array string of the snowflake SnowID.

func (SnowID) Node

func (f SnowID) Node() int64

Node SnowNode returns an int64 of the snowflake SnowID node number DEPRECATED: the below function will be removed in a future release.

func (SnowID) Step

func (f SnowID) Step() int64

Step returns an int64 of the snowflake step (or sequence) number DEPRECATED: the below function will be removed in a future release.

func (SnowID) String

func (f SnowID) String() string

func (SnowID) Time

func (f SnowID) Time() int64

Time returns an int64 unix timestamp in milliseconds of the snowflake SnowID time DEPRECATED: the below function will be removed in a future release.

func (*SnowID) UnmarshalJSON

func (f *SnowID) UnmarshalJSON(b []byte) error

UnmarshalJSON converts a json byte array of a snowflake SnowID into an SnowID type.

type SnowNode

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

A SnowNode struct holds the basic information needed for a snowflake generator node

func NewSnowNode

func NewSnowNode(node int64) (*SnowNode, error)

NewSnowNode returns a new snowflake node that can be used to generate snowflake IDs

func (*SnowNode) Generate

func (n *SnowNode) Generate() SnowID

Generate creates and returns a unique snowflake SnowID To help guarantee uniqueness - Make sure your system is keeping accurate system time - Make sure you never have multiple nodes running with the same node SnowID

Jump to

Keyboard shortcuts

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