OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 #pragma once | |
11 | |
12 #include <string> | |
13 | |
14 #include "base/base_api.h" | |
15 #include "base/basictypes.h" | |
16 #include "base/memory/scoped_ptr.h" | |
17 | |
18 namespace base { | |
19 | |
20 // Simplify the interface and reduce includes by abstracting out the internals. | |
21 struct HMACPlatformData; | |
22 | |
23 class BASE_API HMAC { | |
24 public: | |
25 // The set of supported hash functions. Extend as required. | |
26 enum HashAlgorithm { | |
27 SHA1, | |
28 SHA256, | |
29 }; | |
30 | |
31 explicit HMAC(HashAlgorithm hash_alg); | |
32 ~HMAC(); | |
33 | |
34 // Initializes this instance using |key| of the length |key_length|. Call Init | |
35 // only once. It returns false on the second or later calls. | |
36 bool Init(const unsigned char* key, int key_length); | |
37 | |
38 // Initializes this instance using |key|. Call Init only once. It returns | |
39 // false on the second or later calls. | |
40 bool Init(const std::string& key) { | |
41 return Init(reinterpret_cast<const unsigned char*>(key.data()), | |
42 static_cast<int>(key.size())); | |
43 } | |
44 | |
45 // Calculates the HMAC for the message in |data| using the algorithm supplied | |
46 // to the constructor and the key supplied to the Init method. The HMAC is | |
47 // returned in |digest|, which has |digest_length| bytes of storage available. | |
48 bool Sign(const std::string& data, unsigned char* digest, int digest_length); | |
49 | |
50 // TODO(albertb): Add a Verify method. | |
51 | |
52 private: | |
53 HashAlgorithm hash_alg_; | |
54 scoped_ptr<HMACPlatformData> plat_; | |
55 | |
56 DISALLOW_COPY_AND_ASSIGN(HMAC); | |
57 }; | |
58 | |
59 } // namespace base | |
60 | |
61 #endif // BASE_HMAC_H_ | |
OLD | NEW |