SHA256.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //====- SHA256.cpp - SHA256 implementation ---*- C++ -* ======//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. /*
  14. * The SHA-256 Secure Hash Standard was published by NIST in 2002.
  15. *
  16. * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
  17. *
  18. * The implementation is based on nacl's sha256 implementation [0] and LLVM's
  19. * pre-exsiting SHA1 code [1].
  20. *
  21. * [0] https://hyperelliptic.org/nacl/nacl-20110221.tar.bz2 (public domain
  22. * code)
  23. * [1] llvm/lib/Support/SHA1.{h,cpp}
  24. */
  25. //===----------------------------------------------------------------------===//
  26. #ifndef LLVM_SUPPORT_SHA256_H
  27. #define LLVM_SUPPORT_SHA256_H
  28. #include <array>
  29. #include <cstdint>
  30. namespace llvm {
  31. template <typename T> class ArrayRef;
  32. class StringRef;
  33. class SHA256 {
  34. public:
  35. explicit SHA256() { init(); }
  36. /// Reinitialize the internal state
  37. void init();
  38. /// Digest more data.
  39. void update(ArrayRef<uint8_t> Data);
  40. /// Digest more data.
  41. void update(StringRef Str);
  42. /// Return a reference to the current raw 256-bits SHA256 for the digested
  43. /// data since the last call to init(). This call will add data to the
  44. /// internal state and as such is not suited for getting an intermediate
  45. /// result (see result()).
  46. StringRef final();
  47. /// Return a reference to the current raw 256-bits SHA256 for the digested
  48. /// data since the last call to init(). This is suitable for getting the
  49. /// SHA256 at any time without invalidating the internal state so that more
  50. /// calls can be made into update.
  51. StringRef result();
  52. /// Returns a raw 256-bit SHA256 hash for the given data.
  53. static std::array<uint8_t, 32> hash(ArrayRef<uint8_t> Data);
  54. private:
  55. /// Define some constants.
  56. /// "static constexpr" would be cleaner but MSVC does not support it yet.
  57. enum { BLOCK_LENGTH = 64 };
  58. enum { HASH_LENGTH = 32 };
  59. // Internal State
  60. struct {
  61. union {
  62. uint8_t C[BLOCK_LENGTH];
  63. uint32_t L[BLOCK_LENGTH / 4];
  64. } Buffer;
  65. uint32_t State[HASH_LENGTH / 4];
  66. uint32_t ByteCount;
  67. uint8_t BufferOffset;
  68. } InternalState;
  69. // Internal copy of the hash, populated and accessed on calls to result()
  70. uint32_t HashResult[HASH_LENGTH / 4];
  71. // Helper
  72. void writebyte(uint8_t data);
  73. void hashBlock();
  74. void addUncounted(uint8_t data);
  75. void pad();
  76. };
  77. } // namespace llvm
  78. #endif // LLVM_SUPPORT_SHA256_H
  79. #ifdef __GNUC__
  80. #pragma GCC diagnostic pop
  81. #endif