hasher

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 13, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

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

Example (Xor8)
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)
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"
	"log"
	"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 {
		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: 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])

	// 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(r)
	if err != nil {
		log.Fatal(err)
	}

	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(BLAKE3): blake3
Length of full hash   : 64 bytes
First 32 bytes of hash: 718b749f12a61257438b2ea6643555fd995001c9d9ff84764f93f82610a780f2
Hash algorithm(SHA3)  : sha3-512
Length of full hash   : 64 bytes
First 32 bytes of hash: a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a6

Types

type TChkSumAlgo

type TChkSumAlgo int

TChkSumAlgo is an enum type that represents the checksum algorithm.

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.

type THashAlgo

type THashAlgo int

THashAlgo is an enum type that represents the hash algorithm.

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.

Jump to

Keyboard shortcuts

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