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