SHA1.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim: set ts=8 sts=2 et sw=2 tw=80: */
  3. /* This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. /* Simple class for computing SHA1. */
  7. #ifndef mozilla_SHA1_h
  8. #define mozilla_SHA1_h
  9. #include "mozilla/Types.h"
  10. #include <stddef.h>
  11. #include <stdint.h>
  12. namespace mozilla {
  13. /**
  14. * This class computes the SHA1 hash of a byte sequence, or of the concatenation
  15. * of multiple sequences. For example, computing the SHA1 of two sequences of
  16. * bytes could be done as follows:
  17. *
  18. * void SHA1(const uint8_t* buf1, uint32_t size1,
  19. * const uint8_t* buf2, uint32_t size2,
  20. * SHA1Sum::Hash& hash)
  21. * {
  22. * SHA1Sum s;
  23. * s.update(buf1, size1);
  24. * s.update(buf2, size2);
  25. * s.finish(hash);
  26. * }
  27. *
  28. * The finish method may only be called once and cannot be followed by calls
  29. * to update.
  30. */
  31. class SHA1Sum
  32. {
  33. union
  34. {
  35. uint32_t mW[16]; /* input buffer */
  36. uint8_t mB[64];
  37. } mU;
  38. uint64_t mSize; /* count of hashed bytes. */
  39. unsigned mH[22]; /* 5 state variables, 16 tmp values, 1 extra */
  40. bool mDone;
  41. public:
  42. MFBT_API SHA1Sum();
  43. static const size_t kHashSize = 20;
  44. typedef uint8_t Hash[kHashSize];
  45. /* Add len bytes of dataIn to the data sequence being hashed. */
  46. MFBT_API void update(const void* aData, uint32_t aLength);
  47. /* Compute the final hash of all data into hashOut. */
  48. MFBT_API void finish(SHA1Sum::Hash& aHashOut);
  49. };
  50. } /* namespace mozilla */
  51. #endif /* mozilla_SHA1_h */