SHA1.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //==- SHA1.h - SHA1 implementation for LLVM --*- 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. // This code is taken from public domain
  14. // (http://oauth.googlecode.com/svn/code/c/liboauth/src/sha1.c)
  15. // and modified by wrapping it in a C++ interface for LLVM,
  16. // and removing unnecessary code.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_SUPPORT_SHA1_H
  20. #define LLVM_SUPPORT_SHA1_H
  21. #include <array>
  22. #include <cstdint>
  23. namespace llvm {
  24. template <typename T> class ArrayRef;
  25. class StringRef;
  26. /// A class that wrap the SHA1 algorithm.
  27. class SHA1 {
  28. public:
  29. SHA1() { init(); }
  30. /// Reinitialize the internal state
  31. void init();
  32. /// Digest more data.
  33. void update(ArrayRef<uint8_t> Data);
  34. /// Digest more data.
  35. void update(StringRef Str);
  36. /// Return a reference to the current raw 160-bits SHA1 for the digested data
  37. /// since the last call to init(). This call will add data to the internal
  38. /// state and as such is not suited for getting an intermediate result
  39. /// (see result()).
  40. StringRef final();
  41. /// Return a reference to the current raw 160-bits SHA1 for the digested data
  42. /// since the last call to init(). This is suitable for getting the SHA1 at
  43. /// any time without invalidating the internal state so that more calls can be
  44. /// made into update.
  45. StringRef result();
  46. /// Returns a raw 160-bit SHA1 hash for the given data.
  47. static std::array<uint8_t, 20> hash(ArrayRef<uint8_t> Data);
  48. private:
  49. /// Define some constants.
  50. /// "static constexpr" would be cleaner but MSVC does not support it yet.
  51. enum { BLOCK_LENGTH = 64 };
  52. enum { HASH_LENGTH = 20 };
  53. // Internal State
  54. struct {
  55. union {
  56. uint8_t C[BLOCK_LENGTH];
  57. uint32_t L[BLOCK_LENGTH / 4];
  58. } Buffer;
  59. uint32_t State[HASH_LENGTH / 4];
  60. uint32_t ByteCount;
  61. uint8_t BufferOffset;
  62. } InternalState;
  63. // Internal copy of the hash, populated and accessed on calls to result()
  64. uint32_t HashResult[HASH_LENGTH / 4];
  65. // Helper
  66. void writebyte(uint8_t data);
  67. void hashBlock();
  68. void addUncounted(uint8_t data);
  69. void pad();
  70. };
  71. } // end llvm namespace
  72. #endif
  73. #ifdef __GNUC__
  74. #pragma GCC diagnostic pop
  75. #endif