sha256.h raw

   1  // Copyright (c) 2014-present The Bitcoin Core developers
   2  // Distributed under the MIT software license, see the accompanying
   3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   4  
   5  #ifndef BITCOIN_CRYPTO_SHA256_H
   6  #define BITCOIN_CRYPTO_SHA256_H
   7  
   8  #include <cstdint>
   9  #include <cstdlib>
  10  #include <string>
  11  
  12  /** A hasher class for SHA-256. */
  13  class CSHA256
  14  {
  15  private:
  16      uint32_t s[8];
  17      unsigned char buf[64];
  18      uint64_t bytes{0};
  19  
  20  public:
  21      static const size_t OUTPUT_SIZE = 32;
  22  
  23      CSHA256();
  24      CSHA256& Write(const unsigned char* data, size_t len);
  25      void Finalize(unsigned char hash[OUTPUT_SIZE]);
  26      CSHA256& Reset();
  27  };
  28  
  29  namespace sha256_implementation {
  30  enum UseImplementation : uint8_t {
  31      STANDARD = 0,
  32      USE_SSE4 = 1 << 0,
  33      USE_AVX2 = 1 << 1,
  34      USE_SHANI = 1 << 2,
  35      USE_SSE4_AND_AVX2 = USE_SSE4 | USE_AVX2,
  36      USE_SSE4_AND_SHANI = USE_SSE4 | USE_SHANI,
  37      USE_ALL = USE_SSE4 | USE_AVX2 | USE_SHANI,
  38  };
  39  }
  40  
  41  /** Autodetect the best available SHA256 implementation.
  42   *  Returns the name of the implementation.
  43   */
  44  std::string SHA256AutoDetect(sha256_implementation::UseImplementation use_implementation = sha256_implementation::USE_ALL);
  45  
  46  /** Compute multiple double-SHA256's of 64-byte blobs.
  47   *  output:  pointer to a blocks*32 byte output buffer
  48   *  input:   pointer to a blocks*64 byte input buffer
  49   *  blocks:  the number of hashes to compute.
  50   */
  51  void SHA256D64(unsigned char* output, const unsigned char* input, size_t blocks);
  52  
  53  #endif // BITCOIN_CRYPTO_SHA256_H
  54