Skip to content
/ hasher Public

hasher - generate & compare hash using multiple algorithms.

License

Notifications You must be signed in to change notification settings

nao1215/hasher

Repository files navigation

Hasher

All Contributors

Coverage Multi OS Unit Test reviewdog Go Reference Go Report Card

The hasher package operates on different hash algorithms through a unified interface. The interfaces it provides include "hash generation" and "comparison of hash values with files (or strings)."

The hasher is cross-platform and has been tested on Windows, macOS, and Linux.

Supported Hash Algorithms

  • MD5
  • CRC32
  • SHA1
  • SHA256
  • SHA512
  • 32-bit FNV-1, FNV-1a
  • 64-bit FNV-1, FNV-1a
  • 128-bit FNV-1, FNV-1a
  • Blake3(64bit)
  • MurmurHash v3
  • Whirlpool
  • xxHash
  • Perceptual Hash (only for images)
  • User-defined algorithms

Usage

Use default algorithm: MD5

package main

import (
	"fmt"
	"github.com/nao1215/hasher"
	"log"
	"os"
)

func main() {
	// Create a new Hash instance with default options.
	h := hasher.NewHash()

	// Generate a hash from a string.
	hash, err := h.Generate("example")
	if err != nil {
	    log.Fatal(err)
	}

	// Compare a hash with a string.
	err := h.Compare(hash, "example")
	if err != nil {
	    log.Fatal(err)
	}

	// Generate a hash from an io.Reader.
	file, err := os.Open("example.txt")
	if err != nil {
	    log.Fatal(err)
	}
	defer file.Close()
	hash, err = h.Generate(file)
	if err != nil {
	    log.Fatal(err)
	}

	// Compare a hash with an io.Reader.
	file, err = os.Open("example.txt")
	if err != nil {
	    log.Fatal(err)
	}
	defer file.Close()

	err = h.Compare(hash, file)
	if err != nil {
	    log.Fatal(err)
	}
}

Use another algorithm: SHA256

If you use another algorithm, you can specify algorithm option when creating a new Hash instance. If you use SHA256, you can do as follows:

    h := hasher.NewHash(hasher.WithSha256sum())

Use user-defined algorithm

If you use a user-defined algorithm, you must implement the Hasher interface.

// Hasher is an interface that contains the methods to generate and compare hashes.
type Hasher interface {
	// GenHashFromString generates a hash from a string.
	GenHashFromString(string) ([]byte, error)
	// GenHashFromIOReader generates a hash from an io.Reader.
	GenHashFromIOReader(io.Reader) ([]byte, error)
	// CmpHashAndString compares a hash and a string.
	// If the hash and the string are the same, nil is returned.
	CmpHashAndString([]byte, string) error
	// CmpHashAndIOReader compares a hash and an io.Reader.
	// If the hash and the io.Reader are the same, nil is returned.
	CmpHashAndIOReader([]byte, io.Reader) error
}
	// YourOriginalHashAlgorithm implements the Hasher interface.
	h := hasher.NewHash(hasher.WithUserDifinedAlgorithm(YourOriginalHashAlgorithm))

LICENSE

MIT License

Contribution

First off, thanks for taking the time to contribute! Contributions are not only related to development. For example, GitHub Star motivates me to develop! Please feel free to contribute to this project.

Thanks goes to these wonderful people (emoji key):

CHIKAMATSU Naohiro
CHIKAMATSU Naohiro

💻
Add your contributions

This project follows the all-contributors specification. Contributions of any kind welcome!

About

hasher - generate & compare hash using multiple algorithms.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published