Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:34

0001 //====- SHA256.cpp - SHA256 implementation ---*- C++ -* ======//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 /*
0009  *  The SHA-256 Secure Hash Standard was published by NIST in 2002.
0010  *
0011  *  http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
0012  *
0013  *   The implementation is based on nacl's sha256 implementation [0] and LLVM's
0014  *  pre-exsiting SHA1 code [1].
0015  *
0016  *   [0] https://hyperelliptic.org/nacl/nacl-20110221.tar.bz2 (public domain
0017  *       code)
0018  *   [1] llvm/lib/Support/SHA1.{h,cpp}
0019  */
0020 //===----------------------------------------------------------------------===//
0021 
0022 #ifndef LLVM_SUPPORT_SHA256_H
0023 #define LLVM_SUPPORT_SHA256_H
0024 
0025 #include <array>
0026 #include <cstdint>
0027 
0028 namespace llvm {
0029 
0030 template <typename T> class ArrayRef;
0031 class StringRef;
0032 
0033 class SHA256 {
0034 public:
0035   explicit SHA256() { init(); }
0036 
0037   /// Reinitialize the internal state
0038   void init();
0039 
0040   /// Digest more data.
0041   void update(ArrayRef<uint8_t> Data);
0042 
0043   /// Digest more data.
0044   void update(StringRef Str);
0045 
0046   /// Return the current raw 256-bits SHA256 for the digested
0047   /// data since the last call to init(). This call will add data to the
0048   /// internal state and as such is not suited for getting an intermediate
0049   /// result (see result()).
0050   std::array<uint8_t, 32> final();
0051 
0052   /// Return the current raw 256-bits SHA256 for the digested
0053   /// data since the last call to init(). This is suitable for getting the
0054   /// SHA256 at any time without invalidating the internal state so that more
0055   /// calls can be made into update.
0056   std::array<uint8_t, 32> result();
0057 
0058   /// Returns a raw 256-bit SHA256 hash for the given data.
0059   static std::array<uint8_t, 32> hash(ArrayRef<uint8_t> Data);
0060 
0061 private:
0062   /// Define some constants.
0063   /// "static constexpr" would be cleaner but MSVC does not support it yet.
0064   enum { BLOCK_LENGTH = 64 };
0065   enum { HASH_LENGTH = 32 };
0066 
0067   // Internal State
0068   struct {
0069     union {
0070       uint8_t C[BLOCK_LENGTH];
0071       uint32_t L[BLOCK_LENGTH / 4];
0072     } Buffer;
0073     uint32_t State[HASH_LENGTH / 4];
0074     uint32_t ByteCount;
0075     uint8_t BufferOffset;
0076   } InternalState;
0077 
0078   // Helper
0079   void writebyte(uint8_t data);
0080   void hashBlock();
0081   void addUncounted(uint8_t data);
0082   void pad();
0083 
0084   void final(std::array<uint32_t, HASH_LENGTH / 4> &HashResult);
0085 };
0086 
0087 } // namespace llvm
0088 
0089 #endif // LLVM_SUPPORT_SHA256_H