michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* Simple class for computing SHA1. */ michael@0: michael@0: #ifndef mozilla_SHA1_h michael@0: #define mozilla_SHA1_h michael@0: michael@0: #include "mozilla/Types.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: namespace mozilla { michael@0: michael@0: /** michael@0: * This class computes the SHA1 hash of a byte sequence, or of the concatenation michael@0: * of multiple sequences. For example, computing the SHA1 of two sequences of michael@0: * bytes could be done as follows: michael@0: * michael@0: * void SHA1(const uint8_t* buf1, uint32_t size1, michael@0: * const uint8_t* buf2, uint32_t size2, michael@0: * SHA1Sum::Hash& hash) michael@0: * { michael@0: * SHA1Sum s; michael@0: * s.update(buf1, size1); michael@0: * s.update(buf2, size2); michael@0: * s.finish(hash); michael@0: * } michael@0: * michael@0: * The finish method may only be called once and cannot be followed by calls michael@0: * to update. michael@0: */ michael@0: class SHA1Sum michael@0: { michael@0: union { michael@0: uint32_t w[16]; /* input buffer */ michael@0: uint8_t b[64]; michael@0: } u; michael@0: uint64_t size; /* count of hashed bytes. */ michael@0: unsigned H[22]; /* 5 state variables, 16 tmp values, 1 extra */ michael@0: bool mDone; michael@0: michael@0: public: michael@0: MFBT_API SHA1Sum(); michael@0: michael@0: static const size_t HashSize = 20; michael@0: typedef uint8_t Hash[HashSize]; michael@0: michael@0: /* Add len bytes of dataIn to the data sequence being hashed. */ michael@0: MFBT_API void update(const void* dataIn, uint32_t len); michael@0: michael@0: /* Compute the final hash of all data into hashOut. */ michael@0: MFBT_API void finish(SHA1Sum::Hash& hashOut); michael@0: }; michael@0: michael@0: } /* namespace mozilla */ michael@0: michael@0: #endif /* mozilla_SHA1_h */