summaryrefslogtreecommitdiffstats
path: root/base/hmac_mac.cc
diff options
context:
space:
mode:
authormmentovai@google.com <mmentovai@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-04 19:00:37 +0000
committermmentovai@google.com <mmentovai@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-09-04 19:00:37 +0000
commit301415ea99531af12cf34568c908fdbdd128bcda (patch)
treebeb76fa0ee445fa569e212dcfbe8222528fe739c /base/hmac_mac.cc
parent261d17188f9c63f82ea326f510c2fddb477c9c97 (diff)
downloadchromium_src-301415ea99531af12cf34568c908fdbdd128bcda.zip
chromium_src-301415ea99531af12cf34568c908fdbdd128bcda.tar.gz
chromium_src-301415ea99531af12cf34568c908fdbdd128bcda.tar.bz2
HMAC-SHA1 implementation for Mac based on CommonCrypto;
allow Windows HMAC-SHA1 to use keys longer than 16 bytes. Review URL: http://codereview.chromium.org/218 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1724 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/hmac_mac.cc')
-rw-r--r--base/hmac_mac.cc52
1 files changed, 52 insertions, 0 deletions
diff --git a/base/hmac_mac.cc b/base/hmac_mac.cc
new file mode 100644
index 0000000..0542a70
--- /dev/null
+++ b/base/hmac_mac.cc
@@ -0,0 +1,52 @@
+// Copyright (c) 2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/hmac.h"
+
+#include <CommonCrypto/CommonHMAC.h>
+
+#include "base/logging.h"
+
+namespace base {
+
+HMAC::HMAC(HashAlgorithm hash_alg, const unsigned char* key, int key_length)
+ : hash_alg_(hash_alg),
+ key_(reinterpret_cast<const char*>(key), key_length) {
+}
+
+HMAC::~HMAC() {
+ // Zero out key copy.
+ key_.assign(key_.length(), std::string::value_type());
+ key_.clear();
+ key_.reserve(0);
+}
+
+bool HMAC::Sign(const std::string& data,
+ unsigned char* digest,
+ int digest_length) {
+ CCHmacAlgorithm algorithm;
+ int algorithm_digest_length;
+ switch (hash_alg_) {
+ case SHA1:
+ algorithm = kCCHmacAlgSHA1;
+ algorithm_digest_length = CC_SHA1_DIGEST_LENGTH;
+ break;
+ default:
+ NOTREACHED();
+ return false;
+ }
+
+ if (digest_length < algorithm_digest_length) {
+ NOTREACHED();
+ return false;
+ }
+
+ CCHmac(algorithm,
+ key_.data(), key_.length(), data.data(), data.length(),
+ digest);
+
+ return true;
+}
+
+} // namespace base