From d91f84376fe8dd249770ac19b7c08f8fcc20f446 Mon Sep 17 00:00:00 2001 From: "wtc@chromium.org" Date: Tue, 5 May 2009 23:55:59 +0000 Subject: Separate the key setting code in the constructor of HMAC class into the Init method. Overload the Init method for char* and std::string. Add DCHECKs to the destruction methods in ~HMAC in hmac_win.cc. The patch is written by Takeshi Yoshino . Original code review: http://codereview.chromium.org/88062 R=wtc http://crbug.com/2297 TEST=base_unittests should pass. Safe browsing should continue to work. Review URL: http://codereview.chromium.org/113001 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@15353 0039d316-1c4b-4281-b951-d872f2087c98 --- base/hmac.h | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'base/hmac.h') diff --git a/base/hmac.h b/base/hmac.h index bbfe855..bdd23a9 100644 --- a/base/hmac.h +++ b/base/hmac.h @@ -25,12 +25,23 @@ class HMAC { SHA1 }; - HMAC(HashAlgorithm hash_alg, const unsigned char* key, int key_length); + explicit HMAC(HashAlgorithm hash_alg); ~HMAC(); - // Calculates the HMAC for the message in |data| using the algorithm and key - // supplied to the constructor. The HMAC is returned in |digest|, which - // has |digest_length| bytes of storage available. + // Initializes this instance using |key| of the length |key_length|. Call Init + // only once. It returns false on the second or later calls. + bool Init(const unsigned char* key, int key_length); + + // Initializes this instance using |key|. Call Init only once. It returns + // false on the second or later calls. + bool Init(const std::string& key) { + return Init(reinterpret_cast(key.data()), + static_cast(key.size())); + } + + // Calculates the HMAC for the message in |data| using the algorithm supplied + // to the constructor and the key supplied to the Init method. The HMAC is + // returned in |digest|, which has |digest_length| bytes of storage available. bool Sign(const std::string& data, unsigned char* digest, int digest_length); private: -- cgit v1.1