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

Side by Side Diff: content/renderer/webcrypto_digest_nss.cc

Issue 19757011: WebCrypto: Implement digest() using NSS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove overrides, add documentation to digest interface, and small fix to hash result length retrieā€¦ Created 7 years, 4 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
OLDNEW
(Empty)
1 // Copyright (c) 2013 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 "content/renderer/webcrypto_digest.h"
6
7 #include <sechash.h>
8
9 #include "base/logging.h"
10 #include "crypto/nss_util.h"
11 #include "third_party/WebKit/public/platform/WebArrayBuffer.h"
12 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
13
14 namespace content {
15
16 WebCryptoDigest::WebCryptoDigest(
17 const WebKit::WebCryptoOperationResult& result)
18 : result_(result),
19 context_(NULL) {
20 }
21
22 WebCryptoDigest::~WebCryptoDigest() {
23 if (context_) {
24 HASH_Destroy(context_);
jamesr 2013/08/09 20:04:55 you could also define a HASHDeleter that overrides
Bryan Eyler 2013/08/13 18:22:21 Ah, very cool. Done.
eroman 2013/08/13 18:32:24 I will defer to sleevi, however if we go this rout
25 }
26 }
27
28 bool WebCryptoDigest::Initialize(
29 const WebKit::WebCryptoAlgorithm& algorithm) {
30 crypto::EnsureNSSInit();
31
32 HASH_HashType hash_type = HASH_AlgNULL;
33
34 switch (algorithm.id()) {
35 case WebKit::WebCryptoAlgorithmIdSha1:
36 hash_type = HASH_AlgSHA1;
37 break;
38 case WebKit::WebCryptoAlgorithmIdSha224:
39 hash_type = HASH_AlgSHA224;
40 break;
41 case WebKit::WebCryptoAlgorithmIdSha256:
42 hash_type = HASH_AlgSHA256;
43 break;
44 case WebKit::WebCryptoAlgorithmIdSha384:
45 hash_type = HASH_AlgSHA384;
46 break;
47 case WebKit::WebCryptoAlgorithmIdSha512:
48 hash_type = HASH_AlgSHA512;
49 break;
50 default:
51 NOTREACHED();
52 return false;
53 }
54
55 context_ = HASH_Create(hash_type);
56 if (!context_) {
57 LOG(ERROR) << "Could not create digest context for hash algorithm: "
58 << hash_type;
59 return false;
60 }
61
62 HASH_Begin(context_);
63
64 return true;
65 }
66
67 void WebCryptoDigest::process(const unsigned char* bytes, size_t size) {
68 DCHECK(context_);
69 HASH_Update(context_, bytes, size);
70 }
71
72 void WebCryptoDigest::abort() {
73 delete this;
jamesr 2013/08/09 20:04:55 this is very unusual. what's the lifecycle of this
Bryan Eyler 2013/08/13 18:22:21 The way the interface is defined, this object need
74 }
75
76 void WebCryptoDigest::finish() {
77 DCHECK(context_);
78
79 unsigned int hash_result_length = HASH_ResultLenContext(context_);
80 WebKit::WebArrayBuffer buffer(
81 WebKit::WebArrayBuffer::create(hash_result_length, 1));
82
83 unsigned char* digest = reinterpret_cast<unsigned char*>(buffer.data());
84 DCHECK(digest);
85
86 unsigned int result_length = 0;
87 HASH_End(context_, digest, &result_length, hash_result_length);
88 if (result_length != hash_result_length) {
89 LOG(ERROR) << "Result length invalid; expected " << hash_result_length
90 << ", got " << result_length;
91 result_.completeWithError();
92 delete this;
93 return;
94 }
95
96 result_.completeWithArrayBuffer(buffer);
97
98 delete this;
99 }
100
101 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698