hasher

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package hasher implements hash functions. It is used to generate raw IDs.

Example (Xor8)

Example of _xor8

Example of the use of the private function _xor8. This function is implemented internally, but its use is currently on hold due to speed and accuracy issues.

input := "this is a sample data"
r := strings.NewReader(input)

byteR, err := _xor8(r)
if err != nil {
	log.Fatal(err)
}

fmt.Printf("%x\n", byteR)
fmt.Printf("%08b\n", byteR)
fmt.Printf("%04d\n", byteR)
Output:

6f
[01101111]
[0111]
Example (Xxhash)

Example of the use of the private function _xxhash. To use it from public see the CheckSum (Xxhash) example.

input := "a"
r := strings.NewReader(input)

// lenOut=0 returns 64 bit/8 Byte length output
byteR, err := _xxhash(r, 0)
if err != nil {
	log.Fatal(err)
}

fmt.Printf("0x%x\n", byteR)
Output:

0xd24ec4f1a98c6e5b

Index

Examples

Constants

This section is empty.

Variables

View Source
var CRC32Poly = crc32PolyDefault

CRC32Poly is the polynomial used in the CRC32 algorithm.

By default it uses Castagnoli polynomial. Overwrite this value to use a different polynomial.

Example:
  genrawid.CRC32Poly = crc32.Koopman
  genrawid.CRC32Poly = crc32.IEEE

Note that this won't affect if ChkSumAlgo is other than CRC32.

View Source
var ChkSumAlgo = chksumAlgoDefault

ChkSumAlgo is the checksum algorithm to use.

One of TChkSumAlgo type must be set. By default it is ChkSumCRC32(=CRC32). Currently, we only support CRC32.

View Source
var HashAlgo = hashAlgoDefault

HashAlgo is the hash algorithm to use. By default it uses BLAKE3. Overwrite this value to use a different hash algorithm.

View Source
var HashLen = hashLenDefault

HashLen is the hash digest length. By default it is 64 byte length.

View Source
var IoSeekStart = io.SeekStart

IoSeekStart is a copy of io.SeekStart to ease testing.

Functions

func CheckSum

func CheckSum(input io.Reader) (rawid.ID, error)

CheckSum returns the CRC-32 checksum of input.

The CRC32Poly variable is used as a polynomial to create the table. By default it uses Castagnoli polynomial. A.k.a. CRC32C or CRC32-Castagnoli.

Example
package main

import (
	"fmt"
	"log"
	"strings"

	"github.com/KEINOS/go-genrawid/pkg/hasher"
)

func main() {
	input := "1234567890"
	r := strings.NewReader(input)

	sumByte, err := hasher.CheckSum(r)
	if err != nil {
		log.Fatal(err)
	}

	// By default, the checksum algorithm is CRC32C with 4 bytes of length.
	fmt.Printf("Checksum algorithm: %v\n", hasher.ChkSumAlgo.String())
	fmt.Printf("Length of checksum: %v bytes\n", len(sumByte))
	fmt.Printf("Checksum value    : %x\n", sumByte)

}
Output:

Checksum algorithm: crc32
Length of checksum: 4 bytes
Checksum value    : f3dbd4fe
Example (Xxhash)
package main

import (
	"fmt"
	"strings"

	"github.com/KEINOS/go-genrawid/pkg/hasher"
)

func main() {
	// backup and defer restore checksum algorithm
	oldChkSumAlgo := hasher.ChkSumAlgo
	defer func() {
		hasher.ChkSumAlgo = oldChkSumAlgo
	}()

	// Change checksum algorithm from CRC32C to xxHash
	hasher.ChkSumAlgo = hasher.ChkSumXXHash

	input := "1234567890"
	r := strings.NewReader(input)

	sumByte, err := hasher.CheckSum(r)
	if err != nil {
		fmt.Println(err.Error())

		return
	}

	// By default, the checksum algorithm is CRC32C with 4 bytes of length.
	fmt.Printf("Checksum algorithm: %v\n", hasher.ChkSumAlgo.String())
	fmt.Printf("Length of checksum: %v bytes\n", len(sumByte))
	fmt.Printf("Checksum value    : %x\n", sumByte)

}
Output:

Checksum algorithm: xxhash
Length of checksum: 4 bytes
Checksum value    : a9d4d413

func Hash

func Hash(input io.Reader) (rawid.ID, error)

Hash returns the hash/digest of input. By default the returned digest length is 64 bytes.

Example
package main

import (
	"fmt"
	"log"
	"strings"

	"github.com/KEINOS/go-genrawid/pkg/hasher"
)

func main() {
	input := "This is a string"
	r := strings.NewReader(input)

	// By default, the hash algorithm is BLAKE3 with 64 bytes of length.
	hashByte, err := hasher.Hash(r)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("Hash algorithm(BLAKE3): %v\n", hasher.HashAlgo.String())
	fmt.Printf("Length of full hash   : %v bytes\n", len(hashByte))
	fmt.Printf("First 32 bytes of hash: %x\n", hashByte[:32])

}
Output:

Hash algorithm(BLAKE3): blake3
Length of full hash   : 64 bytes
First 32 bytes of hash: 718b749f12a61257438b2ea6643555fd995001c9d9ff84764f93f82610a780f2
Example (Change_hash_algorithm)
package main

import (
	"fmt"
	"os"
	"strings"

	"github.com/KEINOS/go-genrawid/pkg/hasher"
)

func main() {
	input := "This is a string"
	tmpReader := strings.NewReader(input)

	// To change the hash algorithm set the hasher.HashAlgo global variable.
	// Here, we temporary set SHA3-512 as the hashing algorithm.
	hasher.HashAlgo = hasher.HashAlgoSHA3_512
	defer func() {
		// Restore to default after the test
		hasher.HashAlgo = hasher.HashAlgoBLAKE3
	}()

	// SHA3-512 returns 64 bytes of length hash as well but slower than BLAKE3
	hashByte, err := hasher.Hash(tmpReader)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)

		return
	}

	fmt.Printf("Hash algorithm(SHA3)  : %v\n", hasher.HashAlgo.String())
	fmt.Printf("Length of full hash   : %v bytes\n", len(hashByte))
	fmt.Printf("First 32 bytes of hash: %x\n", hashByte[:32])

}
Output:

Hash algorithm(SHA3)  : sha3-512
Length of full hash   : 64 bytes
First 32 bytes of hash: bcffce0fa80f0bbaaa7c65725df4c474d298f2b69459e09284c8d9bc22c19201

Types

type TChkSumAlgo

type TChkSumAlgo int

TChkSumAlgo is an enum type that represents the checksum algorithm. It also implements the fmt.Stringer interface.

const (
	// ChkSumUnknown is the enum of unknown checksum algorithm.
	ChkSumUnknown TChkSumAlgo = iota
	// ChkSumCRC32 is the enum of CRC32 checksum algorithm. This needs "CRC32Poly"
	// variagle, a polynomial to use, to be set as well.
	ChkSumCRC32
	// ChkSumXXHash is the enum of xxHash algorithm as a checksum.
	ChkSumXXHash
)

func (TChkSumAlgo) String

func (h TChkSumAlgo) String() string

String returns the string representation of the hash algorithm. This is an implementation of fmt.Stringer interface.

Example
package main

import (
	"fmt"

	"github.com/KEINOS/go-genrawid/pkg/hasher"
)

func main() {
	for _, chkSumAlgo := range []hasher.TChkSumAlgo{
		hasher.ChkSumUnknown,
		hasher.ChkSumCRC32,
		hasher.ChkSumXXHash,
	} {
		fmt.Printf("Value: %#v, String: %s\n", chkSumAlgo, chkSumAlgo)
	}

}
Output:

Value: 0, String: unknown
Value: 1, String: crc32
Value: 2, String: xxhash

type THashAlgo

type THashAlgo int

THashAlgo is an enum type that represents the hash algorithm. It also implements the fmt.Stringer interface.

const (
	// HashAlgoUnknown is the enum of unknown hash algorithm.
	HashAlgoUnknown THashAlgo = iota
	// HashAlgoBLAKE3 is the enum of BLAKE3 hash algorithm. This is the default
	// set to "hasher.HashAlgo".
	HashAlgoBLAKE3
	// HashAlgoSHA3_512 is the enum of SHA3-512 hash algorithm. Set this to
	// "hasher.HashAlgo" to use this algorithm.
	HashAlgoSHA3_512
)

func (THashAlgo) String

func (h THashAlgo) String() string

String returns the string representation of the hash algorithm. This is an implementation of fmt.Stringer interface.

Example
package main

import (
	"fmt"

	"github.com/KEINOS/go-genrawid/pkg/hasher"
)

func main() {
	for _, chkSumAlgo := range []hasher.THashAlgo{
		hasher.HashAlgoUnknown,
		hasher.HashAlgoBLAKE3,
		hasher.HashAlgoSHA3_512,
	} {
		fmt.Printf("Value: %#v, String: %s\n", chkSumAlgo, chkSumAlgo)
	}

}
Output:

Value: 0, String: unknown
Value: 1, String: blake3
Value: 2, String: sha3-512

Jump to

Keyboard shortcuts

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