OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 |
5 // Utility class for calculating the HMAC for a given message. We currently | 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. | 6 // only support SHA1 for the hash algorithm, but this can be extended easily. |
7 | 7 |
8 #ifndef BASE_HMAC_H__ | 8 #ifndef BASE_HMAC_H_ |
9 #define BASE_HMAC_H__ | 9 #define BASE_HMAC_H_ |
10 | 10 |
| 11 #include "build/build_config.h" |
| 12 |
| 13 #if defined(OS_WIN) |
11 #include <windows.h> | 14 #include <windows.h> |
12 #include <wincrypt.h> | 15 #include <wincrypt.h> |
| 16 #endif |
13 | 17 |
14 #include <string> | 18 #include <string> |
15 | 19 |
16 #include "base/basictypes.h" | 20 #include "base/basictypes.h" |
17 | 21 |
| 22 namespace base { |
| 23 |
18 class HMAC { | 24 class HMAC { |
19 public: | 25 public: |
20 // The set of supported hash functions. Extend as required. | 26 // The set of supported hash functions. Extend as required. |
21 enum HashAlgorithm { | 27 enum HashAlgorithm { |
22 SHA1 | 28 SHA1 |
23 }; | 29 }; |
24 | 30 |
25 HMAC(HashAlgorithm hash_alg, const unsigned char* key, int key_length); | 31 HMAC(HashAlgorithm hash_alg, const unsigned char* key, int key_length); |
26 ~HMAC(); | 32 ~HMAC(); |
27 | 33 |
28 // Returns the HMAC in 'digest' for the message in 'data' and the key | 34 // Calculates the HMAC for the message in |data| using the algorithm and key |
29 // specified in the contructor. | 35 // supplied to the constructor. The HMAC is returned in |digest|, which |
| 36 // has |digest_length| bytes of storage available. |
30 bool Sign(const std::string& data, unsigned char* digest, int digest_length); | 37 bool Sign(const std::string& data, unsigned char* digest, int digest_length); |
31 | 38 |
32 private: | 39 private: |
| 40 #if defined(OS_POSIX) |
| 41 HashAlgorithm hash_alg_; |
| 42 std::string key_; |
| 43 #elif defined(OS_WIN) |
33 // Import the key so that we don't have to store it ourself. | 44 // Import the key so that we don't have to store it ourself. |
34 // TODO(paulg): Bug: http://b/1084719, 'ImportKey' will not currently work on | |
35 // Windows 2000 since it requires special handling for importing | |
36 // keys. See this link for details: | |
37 // http://www.derkeiler.com/Newsgroups/microsoft.public.platformsdk.security/2
004-06/0270.html | |
38 void ImportKey(const unsigned char* key, int key_length); | 45 void ImportKey(const unsigned char* key, int key_length); |
39 | 46 |
40 // Returns the SHA1 hash of 'data' and 'key' in 'digest'. If there was any | 47 // Returns the SHA1 hash of 'data' and 'key' in 'digest'. If there was any |
41 // error in the calculation, this method returns false, otherwise true. | 48 // error in the calculation, this method returns false, otherwise true. |
42 bool SignWithSHA1(const std::string& data, | 49 bool SignWithSHA1(const std::string& data, |
43 unsigned char* digest, | 50 unsigned char* digest, |
44 int digest_length); | 51 int digest_length); |
45 | 52 |
46 // Required for the SHA1 key_blob struct. We limit this to 16 bytes since | |
47 // Windows 2000 doesn't support keys larger than that. | |
48 static const int kMaxKeySize = 16; | |
49 | |
50 // The hash algorithm to use. | 53 // The hash algorithm to use. |
51 HashAlgorithm hash_alg_; | 54 HashAlgorithm hash_alg_; |
52 | 55 |
53 // Windows Crypt API resources. | 56 // Windows Crypt API resources. |
54 HCRYPTPROV provider_; | 57 HCRYPTPROV provider_; |
55 HCRYPTHASH hash_; | 58 HCRYPTHASH hash_; |
56 HCRYPTKEY hkey_; | 59 HCRYPTKEY hkey_; |
| 60 #endif // OS_WIN |
57 | 61 |
58 DISALLOW_EVIL_CONSTRUCTORS(HMAC); | 62 DISALLOW_COPY_AND_ASSIGN(HMAC); |
59 }; | 63 }; |
60 | 64 |
| 65 } // namespace base |
61 | 66 |
62 #endif // BASE_HMAC_H__ | 67 #endif // BASE_HMAC_H_ |
63 | |
OLD | NEW |