github.com/sandwich-go/boost@v1.3.29/xhash/nhash/nhash.go (about)

     1  // Copyright © 2014 Lawrence E. Bakst. All rights reserved.
     2  
     3  // This package contains a new set of interfacs for hash functions.
     4  // It also implements the Go streaming hash interface as HashStream.
     5  // It is an experiment.
     6  
     7  package nhash
     8  
     9  import (
    10  	"io"
    11  )
    12  
    13  // Interface HashFunction requires 4 methods that return the
    14  // size of the hasg function in bytes and bits. Probably wiil
    15  // flush bits. Also the maximum number of bytes of seed needed.
    16  type HashFunction interface {
    17  	// Size returns the number of bytes Sum will return.
    18  	Size() int
    19  
    20  	// BlockSize returns the hash's underlying block size.
    21  	// The Write method must be able to accept any amount
    22  	// of data, but it may operate more efficiently if all writes
    23  	// are a multiple of the block size.
    24  	BlockSize() int
    25  
    26  	// maximum number of seeds in bytes (should this be in bits?)
    27  	NumSeedBytes() int
    28  
    29  	// retunrs the number of bits the hash function outputs
    30  	//HashSizeInBits() int
    31  }
    32  
    33  // HashStream is a streaming interface for hash functions.
    34  type HashStream interface {
    35  	HashFunction
    36  
    37  	// Write (via the embedded io.Writer interface) adds more data to the running hash.
    38  	// It never returns an error.
    39  	io.Writer
    40  
    41  	// Sum appends the current hash to b and returns the resulting slice.
    42  	// It does not change the underlying hash state.
    43  	Sum(b []byte) []byte
    44  
    45  	// Reset resets the Hash to its initial state.
    46  	Reset()
    47  }
    48  
    49  // Hash32 is a common interface implemented by the streaming 32-bit hash functions.
    50  type Hash32 interface {
    51  	HashStream
    52  	Sum32() uint32
    53  }
    54  
    55  // Hash64 is a common interface implemented by the streaming 32-bit hash functions.
    56  type Hash64 interface {
    57  	HashStream
    58  	Write64(h uint64) error
    59  	Sum64() uint64
    60  }
    61  
    62  // *** Everything below here will be removed or chnaged as vargs was way too expensive. ***
    63  
    64  // HashF32 is the interface that all non-streaming 32 bit hash functions implement.
    65  type HashF32 interface {
    66  	HashFunction
    67  	Hash32(b []byte, seeds ...uint32) uint32
    68  }
    69  
    70  // HashF64 is the interface that all non-streaming 64 bit hash functions implement.
    71  type HashF64 interface {
    72  	HashFunction
    73  	Hash64(b []byte, seeds ...uint64) uint64
    74  	Hash64S(b []byte, seed uint64) uint64
    75  }
    76  
    77  // HashF128 is the interface that all non-streaming 128 bit hash functions implement.
    78  type HashF128 interface {
    79  	HashFunction
    80  	Hash128(b []byte, seeds ...uint64) (uint64, uint64)
    81  }
    82  
    83  // HashGeneric is generic interface that non-streaming, typicall crytpo hash functions implement.
    84  type HashGeneric interface {
    85  	HashFunction
    86  
    87  	// Hash takes "in" bytes of input, the hash is returned into byte slice "out"
    88  	// change seeds to bytes ???
    89  	Hash(in []byte, out []byte, seeds ...uint64) []byte
    90  }