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