hash

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2025 License: MIT Imports: 9 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateTarget

func CalculateTarget(difficulty uint64, hashSize int) []byte

CalculateTarget calculates the target hash value from a difficulty Higher difficulty means lower target value

func CompareHash

func CompareHash(a, b []byte) bool

CompareHash compares two hashes and returns true if they are equal

func CountLeadingZeros

func CountLeadingZeros(hash []byte) int

CountLeadingZeros counts the number of leading zero bits in a hash

func DoubleHash

func DoubleHash(algorithm Algorithm, data []byte) ([]byte, error)

DoubleHash performs a double hash (hash of hash) - useful for some consensus algorithms

func GetDifficultyFromHash

func GetDifficultyFromHash(hash []byte) uint64

GetDifficultyFromHash calculates the effective difficulty of a hash based on the number of leading zero bits

func HashToHex

func HashToHex(hash []byte) string

HashToHex converts a hash to a hexadecimal string

func HashWithSHA256

func HashWithSHA256(data []byte) []byte

HashWithSHA256 is a convenience function for SHA-256 hashing

func HashWithSHA512

func HashWithSHA512(data []byte) []byte

HashWithSHA512 is a convenience function for SHA-512 hashing

func HexToHash

func HexToHash(hex string) ([]byte, error)

HexToHash converts a hexadecimal string to a hash

func IsValidHash

func IsValidHash(hashValue, target []byte) bool

IsValidHash checks if a hash meets a specific difficulty target The hash must be less than or equal to the target

func MeetsTargetDifficulty

func MeetsTargetDifficulty(hash []byte, difficulty uint64) bool

MeetsTargetDifficulty checks if a hash meets the required difficulty

func NewHasher

func NewHasher(algorithm Algorithm) (interfaces.Hasher, error)

NewHasher creates a new hasher for the specified algorithm

func QuickHash

func QuickHash(algorithm Algorithm, data []byte) ([]byte, error)

QuickHash provides a quick way to hash data with a specific algorithm

Types

type Algorithm

type Algorithm string

Algorithm represents a hash algorithm type

const (
	// AlgorithmSHA256 represents the SHA-256 hash algorithm
	AlgorithmSHA256 Algorithm = "SHA256"

	// AlgorithmSHA512 represents the SHA-512 hash algorithm
	AlgorithmSHA512 Algorithm = "SHA512"

	// AlgorithmSHA3_256 represents the SHA3-256 hash algorithm
	AlgorithmSHA3_256 Algorithm = "SHA3-256"

	// AlgorithmSHA3_512 represents the SHA3-512 hash algorithm
	AlgorithmSHA3_512 Algorithm = "SHA3-512"

	// AlgorithmBlake2b256 represents the Blake2b-256 hash algorithm
	AlgorithmBlake2b256 Algorithm = "BLAKE2b-256"

	// AlgorithmBlake2b512 represents the Blake2b-512 hash algorithm
	AlgorithmBlake2b512 Algorithm = "BLAKE2b-512"

	// AlgorithmBlake2s256 represents the Blake2s-256 hash algorithm
	AlgorithmBlake2s256 Algorithm = "BLAKE2s-256"

	// AlgorithmKeccak256 represents the Keccak-256 hash algorithm (Ethereum style)
	AlgorithmKeccak256 Algorithm = "Keccak256"
)

type Factory

type Factory struct{}

Factory provides a hasher factory pattern

func NewFactory

func NewFactory() *Factory

NewFactory creates a new hasher factory

func (*Factory) CreateBlake2b

func (f *Factory) CreateBlake2b() (interfaces.Hasher, error)

CreateBlake2b creates a Blake2b-256 hasher

func (*Factory) CreateHasher

func (f *Factory) CreateHasher(algorithm Algorithm) (interfaces.Hasher, error)

CreateHasher creates a hasher for the specified algorithm

func (*Factory) CreateKeccak256

func (f *Factory) CreateKeccak256() (interfaces.Hasher, error)

CreateKeccak256 creates a Keccak-256 hasher

func (*Factory) CreateSHA3

func (f *Factory) CreateSHA3() (interfaces.Hasher, error)

CreateSHA3 creates a SHA3-256 hasher

func (*Factory) CreateSHA256

func (f *Factory) CreateSHA256() (interfaces.Hasher, error)

CreateSHA256 creates a SHA-256 hasher

Jump to

Keyboard shortcuts

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