github.com/lbryio/lbcd@v0.22.119/chaincfg/chainhash/hashfuncs.go (about)

     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  	"crypto/sha512"
    11  
    12  	"golang.org/x/crypto/ripemd160"
    13  )
    14  
    15  // HashB calculates hash(b) and returns the resulting bytes.
    16  func HashB(b []byte) []byte {
    17  	hash := sha256.Sum256(b)
    18  	return hash[:]
    19  }
    20  
    21  // HashH calculates hash(b) and returns the resulting bytes as a Hash.
    22  func HashH(b []byte) Hash {
    23  	return Hash(sha256.Sum256(b))
    24  }
    25  
    26  // DoubleHashB calculates hash(hash(b)) and returns the resulting bytes.
    27  func DoubleHashB(b []byte) []byte {
    28  	first := sha256.Sum256(b)
    29  	second := sha256.Sum256(first[:])
    30  	return second[:]
    31  }
    32  
    33  // DoubleHashH calculates hash(hash(b)) and returns the resulting bytes as a
    34  // Hash.
    35  func DoubleHashH(b []byte) Hash {
    36  	first := sha256.Sum256(b)
    37  	return Hash(sha256.Sum256(first[:]))
    38  }
    39  
    40  // LbryPoWHashH calculates returns the PoW Hash.
    41  //
    42  //	doubled  := SHA256(SHA256(b))
    43  //	expanded := SHA512(doubled)
    44  //	left     := RIPEMD160(expanded[0:32])
    45  //	right    := RIPEMD160(expanded[32:64])
    46  //	result   := SHA256(SHA256(left||right))
    47  func LbryPoWHashH(b []byte) Hash {
    48  	doubled := DoubleHashB(b)
    49  	expanded := sha512.Sum512(doubled)
    50  
    51  	r := ripemd160.New()
    52  	r.Reset()
    53  	r.Write(expanded[:sha256.Size])
    54  	left := r.Sum(nil)
    55  
    56  	r.Reset()
    57  	r.Write(expanded[sha256.Size:])
    58  
    59  	combined := r.Sum(left)
    60  	return DoubleHashH(combined)
    61  }