|
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. |
|
4 |
|
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. |
|
7 |
|
8 #ifndef BASE_HMAC_H_ |
|
9 #define BASE_HMAC_H_ |
|
10 |
|
11 #include <string> |
|
12 |
|
13 #include "base/basictypes.h" |
|
14 #include "base/scoped_ptr.h" |
|
15 |
|
16 namespace base { |
|
17 |
|
18 // Simplify the interface and reduce includes by abstracting out the internals. |
|
19 struct HMACPlatformData; |
|
20 |
|
21 class HMAC { |
|
22 public: |
|
23 // The set of supported hash functions. Extend as required. |
|
24 enum HashAlgorithm { |
|
25 SHA1 |
|
26 }; |
|
27 |
|
28 explicit HMAC(HashAlgorithm hash_alg); |
|
29 ~HMAC(); |
|
30 |
|
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); |
|
34 |
|
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 } |
|
41 |
|
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); |
|
46 |
|
47 private: |
|
48 HashAlgorithm hash_alg_; |
|
49 scoped_ptr<HMACPlatformData> plat_; |
|
50 |
|
51 DISALLOW_COPY_AND_ASSIGN(HMAC); |
|
52 }; |
|
53 |
|
54 } // namespace base |
|
55 |
|
56 #endif // BASE_HMAC_H_ |