raw_sha1_ostream.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //==- raw_sha1_ostream.h - raw_ostream that compute SHA1 --*- 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. // This file defines the raw_sha1_ostream class.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_SUPPORT_RAW_SHA1_OSTREAM_H
  18. #define LLVM_SUPPORT_RAW_SHA1_OSTREAM_H
  19. #include "llvm/ADT/ArrayRef.h"
  20. #include "llvm/Support/SHA1.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. namespace llvm {
  23. /// A raw_ostream that hash the content using the sha1 algorithm.
  24. class raw_sha1_ostream : public raw_ostream {
  25. SHA1 State;
  26. /// See raw_ostream::write_impl.
  27. void write_impl(const char *Ptr, size_t Size) override {
  28. State.update(ArrayRef<uint8_t>((const uint8_t *)Ptr, Size));
  29. }
  30. public:
  31. /// Return the current SHA1 hash for the content of the stream
  32. std::array<uint8_t, 20> sha1() {
  33. flush();
  34. return State.result();
  35. }
  36. /// Reset the internal state to start over from scratch.
  37. void resetHash() { State.init(); }
  38. uint64_t current_pos() const override { return 0; }
  39. };
  40. } // end llvm namespace
  41. #endif
  42. #ifdef __GNUC__
  43. #pragma GCC diagnostic pop
  44. #endif