"Make Chrome your default browser" should now appear as a checkbox at the bottom...
[chromium-blink-merge.git] / base / hmac_mac.cc
blobb2d0a33ec33e02a961006b22a2eb81db6125327a
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/hmac.h"
7 #include <CommonCrypto/CommonHMAC.h>
9 #include "base/logging.h"
11 namespace base {
13 struct HMACPlatformData {
14 std::string key_;
17 HMAC::HMAC(HashAlgorithm hash_alg, const unsigned char* key, int key_length)
18 : hash_alg_(hash_alg), plat_(new HMACPlatformData()) {
19 plat_->key_.assign(reinterpret_cast<const char*>(key), key_length);
22 HMAC::~HMAC() {
23 // Zero out key copy.
24 plat_->key_.assign(plat_->key_.length(), std::string::value_type());
25 plat_->key_.clear();
26 plat_->key_.reserve(0);
28 delete plat_;
31 bool HMAC::Sign(const std::string& data,
32 unsigned char* digest,
33 int digest_length) {
34 CCHmacAlgorithm algorithm;
35 int algorithm_digest_length;
36 switch (hash_alg_) {
37 case SHA1:
38 algorithm = kCCHmacAlgSHA1;
39 algorithm_digest_length = CC_SHA1_DIGEST_LENGTH;
40 break;
41 default:
42 NOTREACHED();
43 return false;
46 if (digest_length < algorithm_digest_length) {
47 NOTREACHED();
48 return false;
51 CCHmac(algorithm,
52 plat_->key_.data(), plat_->key_.length(), data.data(), data.length(),
53 digest);
55 return true;
58 } // namespace base