Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: crypto/hmac.cc

Issue 2095523002: Make //crypto factories return std::unique_ptr<>s (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: I'm blind Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « crypto/encryptor.cc ('k') | crypto/hmac_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "crypto/hmac.h" 5 #include "crypto/hmac.h"
6 6
7 #include <openssl/hmac.h> 7 #include <openssl/hmac.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 std::fill(raw_key.begin(), raw_key.end(), 0); 56 std::fill(raw_key.begin(), raw_key.end(), 0);
57 return result; 57 return result;
58 } 58 }
59 59
60 bool HMAC::Sign(const base::StringPiece& data, 60 bool HMAC::Sign(const base::StringPiece& data,
61 unsigned char* digest, 61 unsigned char* digest,
62 size_t digest_length) const { 62 size_t digest_length) const {
63 DCHECK(initialized_); 63 DCHECK(initialized_);
64 64
65 ScopedOpenSSLSafeSizeBuffer<EVP_MAX_MD_SIZE> result(digest, digest_length); 65 ScopedOpenSSLSafeSizeBuffer<EVP_MAX_MD_SIZE> result(digest, digest_length);
66 return !!::HMAC(hash_alg_ == SHA1 ? EVP_sha1() : EVP_sha256(), 66 return !!::HMAC(hash_alg_ == SHA1 ? EVP_sha1() : EVP_sha256(), key_.data(),
67 key_.data(), key_.size(), 67 key_.size(),
68 reinterpret_cast<const unsigned char*>(data.data()), 68 reinterpret_cast<const unsigned char*>(data.data()),
69 data.size(), result.safe_buffer(), NULL); 69 data.size(), result.safe_buffer(), nullptr);
70 } 70 }
71 71
72 bool HMAC::Verify(const base::StringPiece& data, 72 bool HMAC::Verify(const base::StringPiece& data,
73 const base::StringPiece& digest) const { 73 const base::StringPiece& digest) const {
74 if (digest.size() != DigestLength()) 74 if (digest.size() != DigestLength())
75 return false; 75 return false;
76 return VerifyTruncated(data, digest); 76 return VerifyTruncated(data, digest);
77 } 77 }
78 78
79 bool HMAC::VerifyTruncated(const base::StringPiece& data, 79 bool HMAC::VerifyTruncated(const base::StringPiece& data,
80 const base::StringPiece& digest) const { 80 const base::StringPiece& digest) const {
81 if (digest.empty()) 81 if (digest.empty())
82 return false; 82 return false;
83 size_t digest_length = DigestLength(); 83 size_t digest_length = DigestLength();
84 std::unique_ptr<unsigned char[]> computed_digest( 84 std::unique_ptr<unsigned char[]> computed_digest(
85 new unsigned char[digest_length]); 85 new unsigned char[digest_length]);
86 if (!Sign(data, computed_digest.get(), digest_length)) 86 if (!Sign(data, computed_digest.get(), digest_length))
87 return false; 87 return false;
88 88
89 return SecureMemEqual(digest.data(), computed_digest.get(), 89 return SecureMemEqual(digest.data(), computed_digest.get(),
90 std::min(digest.size(), digest_length)); 90 std::min(digest.size(), digest_length));
91 } 91 }
92 92
93 } // namespace crypto 93 } // namespace crypto
OLDNEW
« no previous file with comments | « crypto/encryptor.cc ('k') | crypto/hmac_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698