michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: // Utility class for calculating the HMAC for a given message. We currently michael@0: // only support SHA1 for the hash algorithm, but this can be extended easily. michael@0: michael@0: #ifndef BASE_HMAC_H_ michael@0: #define BASE_HMAC_H_ michael@0: michael@0: #include michael@0: michael@0: #include "base/basictypes.h" michael@0: #include "base/scoped_ptr.h" michael@0: michael@0: namespace base { michael@0: michael@0: // Simplify the interface and reduce includes by abstracting out the internals. michael@0: struct HMACPlatformData; michael@0: michael@0: class HMAC { michael@0: public: michael@0: // The set of supported hash functions. Extend as required. michael@0: enum HashAlgorithm { michael@0: SHA1 michael@0: }; michael@0: michael@0: explicit HMAC(HashAlgorithm hash_alg); michael@0: ~HMAC(); michael@0: michael@0: // Initializes this instance using |key| of the length |key_length|. Call Init michael@0: // only once. It returns false on the second or later calls. michael@0: bool Init(const unsigned char* key, int key_length); michael@0: michael@0: // Initializes this instance using |key|. Call Init only once. It returns michael@0: // false on the second or later calls. michael@0: bool Init(const std::string& key) { michael@0: return Init(reinterpret_cast(key.data()), michael@0: static_cast(key.size())); michael@0: } michael@0: michael@0: // Calculates the HMAC for the message in |data| using the algorithm supplied michael@0: // to the constructor and the key supplied to the Init method. The HMAC is michael@0: // returned in |digest|, which has |digest_length| bytes of storage available. michael@0: bool Sign(const std::string& data, unsigned char* digest, int digest_length); michael@0: michael@0: private: michael@0: HashAlgorithm hash_alg_; michael@0: scoped_ptr plat_; michael@0: michael@0: DISALLOW_COPY_AND_ASSIGN(HMAC); michael@0: }; michael@0: michael@0: } // namespace base michael@0: michael@0: #endif // BASE_HMAC_H_