version: 1.10

package hash

import "hash"

Overview

Package hash provides interfaces for hash functions.


Example:

  1. const (
  2. input1 = "The tunneling gopher digs downwards, "
  3. input2 = "unaware of what he will find."
  4. )
  5. first := sha256.New()
  6. first.Write([]byte(input1))
  7. marshaler, ok := first.(encoding.BinaryMarshaler)
  8. if !ok {
  9. log.Fatal("first does not implement encoding.BinaryMarshaler")
  10. }
  11. state, err := marshaler.MarshalBinary()
  12. if err != nil {
  13. log.Fatal("unable to marshal hash:", err)
  14. }
  15. second := sha256.New()
  16. unmarshaler, ok := second.(encoding.BinaryUnmarshaler)
  17. if !ok {
  18. log.Fatal("second does not implement encoding.BinaryUnmarshaler")
  19. }
  20. if err := unmarshaler.UnmarshalBinary(state); err != nil {
  21. log.Fatal("unable to unmarshal hash:", err)
  22. }
  23. first.Write([]byte(input2))
  24. second.Write([]byte(input2))
  25. fmt.Printf("%x\n", first.Sum(nil))
  26. fmt.Println(bytes.Equal(first.Sum(nil), second.Sum(nil)))
  27. // Output:
  28. // 57d51a066f3a39942649cd9a76c77e97ceab246756ff3888659e6aa5a07f4a52
  29. // true

Index

Examples

Package files

hash.go

type Hash

  1. type Hash interface {
  2. // Write (via the embedded io.Writer interface) adds more data to the running hash.
  3. // It never returns an error.
  4. io.Writer
  5.  
  6. // Sum appends the current hash to b and returns the resulting slice.
  7. // It does not change the underlying hash state.
  8. Sum(b []byte) []byte
  9.  
  10. // Reset resets the Hash to its initial state.
  11. Reset()
  12.  
  13. // Size returns the number of bytes Sum will return.
  14. Size() int
  15.  
  16. // BlockSize returns the hash's underlying block size.
  17. // The Write method must be able to accept any amount
  18. // of data, but it may operate more efficiently if all writes
  19. // are a multiple of the block size.
  20. BlockSize() int
  21. }

Hash is the common interface implemented by all hash functions.

Hash implementations in the standard library (e.g. hash/crc32 and crypto/sha256)
implement the encoding.BinaryMarshaler and encoding.BinaryUnmarshaler
interfaces. Marshaling a hash implementation allows its internal state to be
saved and used for additional processing later, without having to re-write the
data previously written to the hash. The hash state may contain portions of the
input in its original form, which users are expected to handle for any possible
security implications.

Compatibility: Any future changes to hash or crypto packages will endeavor to
maintain compatibility with state encoded using previous versions. That is, any
released versions of the packages should be able to decode data written with any
previously released version, subject to issues such as security fixes. See the
Go compatibility document for background: https://golang.org/doc/go1compat

type Hash32

  1. type Hash32 interface {
  2. Hash
  3. Sum32() uint32
  4. }

Hash32 is the common interface implemented by all 32-bit hash functions.

type Hash64

  1. type Hash64 interface {
  2. Hash
  3. Sum64() uint64
  4. }

Hash64 is the common interface implemented by all 64-bit hash functions.

Subdirectories