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

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: 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 hash_result_length_(0) {
21 }
22
23 WebCryptoDigest::~WebCryptoDigest() {
24 if (context_) {
25 HASH_Destroy(context_);
26 }
27 }
28
29 bool WebCryptoDigest::Initialize(
30 const WebKit::WebCryptoAlgorithm& algorithm) {
31 crypto::EnsureNSSInit();
32
33 HASH_HashType hash_type = HASH_AlgNULL;
34
35 switch (algorithm.id()) {
36 case WebKit::WebCryptoAlgorithmIdSha1:
37 hash_type = HASH_AlgSHA1;
38 hash_result_length_ = SHA1_LENGTH;
39 break;
40 case WebKit::WebCryptoAlgorithmIdSha224:
41 hash_type = HASH_AlgSHA224;
42 hash_result_length_ = SHA224_LENGTH;
43 break;
44 case WebKit::WebCryptoAlgorithmIdSha256:
45 hash_type = HASH_AlgSHA256;
46 hash_result_length_ = SHA256_LENGTH;
47 break;
48 case WebKit::WebCryptoAlgorithmIdSha384:
49 hash_type = HASH_AlgSHA384;
50 hash_result_length_ = SHA384_LENGTH;
51 break;
52 case WebKit::WebCryptoAlgorithmIdSha512:
53 hash_type = HASH_AlgSHA512;
54 hash_result_length_ = SHA512_LENGTH;
eroman 2013/08/02 21:41:39 I think the only member variable you need is the H
Ryan Sleevi 2013/08/02 23:29:27 +1
Bryan Eyler 2013/08/03 00:16:45 Done.
55 break;
56 default:
57 NOTREACHED();
eroman 2013/08/02 21:41:39 Can you also add a defensive "return false;" here?
Ryan Sleevi 2013/08/02 23:29:27 +1. This would be a BUG to let it fall down into t
Bryan Eyler 2013/08/03 00:16:45 Done.
58 }
59
60 context_ = HASH_Create(hash_type);
61 if (!context_) {
62 LOG(ERROR) << "Could not create digest context for hash algorithm: "
63 << hash_type;
64 return false;
65 }
66
67 HASH_Begin(context_);
68
69 return true;
70 }
71
72 void WebCryptoDigest::process(const unsigned char* bytes, size_t size) {
73 DCHECK(context_);
74 HASH_Update(context_, bytes, size);
75 }
76
77 void WebCryptoDigest::abort() {
78 delete this;
79 }
80
81 void WebCryptoDigest::finish() {
82 DCHECK(context_);
83
84 WebKit::WebArrayBuffer buffer(
85 WebKit::WebArrayBuffer::create(hash_result_length_, 1));
86
87 unsigned char* digest = reinterpret_cast<unsigned char*>(buffer.data());
88 DCHECK(digest);
89
90 unsigned int result_length = 0;
91 HASH_End(context_, digest, &result_length, hash_result_length_);
92 if (result_length != hash_result_length_) {
93 LOG(ERROR) << "Result length invalid; expected " << hash_result_length_
94 << ", got " << result_length;
95 result_.completeWithError();
96 delete this;
97 return;
98 }
99
100 result_.completeWithArrayBuffer(buffer);
101
102 delete this;
103 }
104
105 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698