hashfuncs.mx raw

   1  // Copyright (c) 2015 The Decred developers
   2  // Copyright (c) 2016-2017 The btcsuite developers
   3  // Use of this source code is governed by an ISC
   4  // license that can be found in the LICENSE file.
   5  
   6  package chainhash
   7  
   8  import (
   9  	"crypto/sha256"
  10  )
  11  
  12  // HashB calculates hash(b) and returns the resulting bytes.
  13  func HashB(b []byte) []byte {
  14  	hash := sha256.Sum256(b)
  15  	return hash[:]
  16  }
  17  
  18  // HashH calculates hash(b) and returns the resulting bytes as a Hash.
  19  func HashH(b []byte) Hash { return Hash(sha256.Sum256(b)) }
  20  
  21  // DoubleHashB calculates hash(hash(b)) and returns the resulting bytes.
  22  func DoubleHashB(b []byte) []byte {
  23  	first := sha256.Sum256(b)
  24  	second := sha256.Sum256(first[:])
  25  	return second[:]
  26  }
  27  
  28  // DoubleHashH calculates hash(hash(b)) and returns the resulting bytes as a
  29  // Hash.
  30  func DoubleHashH(b []byte) Hash {
  31  	first := sha256.Sum256(b)
  32  	return sha256.Sum256(first[:])
  33  }
  34