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

Side by Side Diff: crypto/hmac_openssl.cc

Issue 1841863002: Update monet. (Closed) Base URL: https://github.com/domokit/monet.git@master
Patch Set: Created 4 years, 8 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/hmac_nss.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
(Empty)
1 // Copyright (c) 2011 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.
4
5 #include "crypto/hmac.h"
6
7 #include <openssl/hmac.h>
8
9 #include <algorithm>
10 #include <vector>
11
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/stl_util.h"
15 #include "crypto/openssl_util.h"
16
17 namespace crypto {
18
19 struct HMACPlatformData {
20 std::vector<unsigned char> key;
21 };
22
23 HMAC::HMAC(HashAlgorithm hash_alg) : hash_alg_(hash_alg) {
24 // Only SHA-1 and SHA-256 hash algorithms are supported now.
25 DCHECK(hash_alg_ == SHA1 || hash_alg_ == SHA256);
26 }
27
28 bool HMAC::Init(const unsigned char* key, size_t key_length) {
29 // Init must not be called more than once on the same HMAC object.
30 DCHECK(!plat_);
31 plat_.reset(new HMACPlatformData());
32 plat_->key.assign(key, key + key_length);
33 return true;
34 }
35
36 HMAC::~HMAC() {
37 if (plat_) {
38 // Zero out key copy.
39 plat_->key.assign(plat_->key.size(), 0);
40 STLClearObject(&plat_->key);
41 }
42 }
43
44 bool HMAC::Sign(const base::StringPiece& data,
45 unsigned char* digest,
46 size_t digest_length) const {
47 DCHECK(plat_); // Init must be called before Sign.
48
49 ScopedOpenSSLSafeSizeBuffer<EVP_MAX_MD_SIZE> result(digest, digest_length);
50 return !!::HMAC(hash_alg_ == SHA1 ? EVP_sha1() : EVP_sha256(),
51 vector_as_array(&plat_->key), plat_->key.size(),
52 reinterpret_cast<const unsigned char*>(data.data()),
53 data.size(), result.safe_buffer(), NULL);
54 }
55
56 } // namespace crypto
OLDNEW
« no previous file with comments | « crypto/hmac_nss.cc ('k') | crypto/hmac_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698