Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | // Utility class for calculating the HMAC for a given message. We currently |
michael@0 | 6 | // only support SHA1 for the hash algorithm, but this can be extended easily. |
michael@0 | 7 | |
michael@0 | 8 | #ifndef BASE_HMAC_H_ |
michael@0 | 9 | #define BASE_HMAC_H_ |
michael@0 | 10 | |
michael@0 | 11 | #include <string> |
michael@0 | 12 | |
michael@0 | 13 | #include "base/basictypes.h" |
michael@0 | 14 | #include "base/scoped_ptr.h" |
michael@0 | 15 | |
michael@0 | 16 | namespace base { |
michael@0 | 17 | |
michael@0 | 18 | // Simplify the interface and reduce includes by abstracting out the internals. |
michael@0 | 19 | struct HMACPlatformData; |
michael@0 | 20 | |
michael@0 | 21 | class HMAC { |
michael@0 | 22 | public: |
michael@0 | 23 | // The set of supported hash functions. Extend as required. |
michael@0 | 24 | enum HashAlgorithm { |
michael@0 | 25 | SHA1 |
michael@0 | 26 | }; |
michael@0 | 27 | |
michael@0 | 28 | explicit HMAC(HashAlgorithm hash_alg); |
michael@0 | 29 | ~HMAC(); |
michael@0 | 30 | |
michael@0 | 31 | // Initializes this instance using |key| of the length |key_length|. Call Init |
michael@0 | 32 | // only once. It returns false on the second or later calls. |
michael@0 | 33 | bool Init(const unsigned char* key, int key_length); |
michael@0 | 34 | |
michael@0 | 35 | // Initializes this instance using |key|. Call Init only once. It returns |
michael@0 | 36 | // false on the second or later calls. |
michael@0 | 37 | bool Init(const std::string& key) { |
michael@0 | 38 | return Init(reinterpret_cast<const unsigned char*>(key.data()), |
michael@0 | 39 | static_cast<int>(key.size())); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | // Calculates the HMAC for the message in |data| using the algorithm supplied |
michael@0 | 43 | // to the constructor and the key supplied to the Init method. The HMAC is |
michael@0 | 44 | // returned in |digest|, which has |digest_length| bytes of storage available. |
michael@0 | 45 | bool Sign(const std::string& data, unsigned char* digest, int digest_length); |
michael@0 | 46 | |
michael@0 | 47 | private: |
michael@0 | 48 | HashAlgorithm hash_alg_; |
michael@0 | 49 | scoped_ptr<HMACPlatformData> plat_; |
michael@0 | 50 | |
michael@0 | 51 | DISALLOW_COPY_AND_ASSIGN(HMAC); |
michael@0 | 52 | }; |
michael@0 | 53 | |
michael@0 | 54 | } // namespace base |
michael@0 | 55 | |
michael@0 | 56 | #endif // BASE_HMAC_H_ |