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

Unified Diff: content/renderer/webcrypto_impl_nss.cc

Issue 19757011: WebCrypto: Implement digest() using NSS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix leak of hash context. 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 side-by-side diff with in-line comments
Download patch
Index: content/renderer/webcrypto_impl_nss.cc
diff --git a/content/renderer/webcrypto_impl_nss.cc b/content/renderer/webcrypto_impl_nss.cc
new file mode 100644
index 0000000000000000000000000000000000000000..c584e5f813c8ddcb48937127d4fb95e72f939ae8
--- /dev/null
+++ b/content/renderer/webcrypto_impl_nss.cc
@@ -0,0 +1,76 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/renderer/webcrypto_impl.h"
+
+#include <sechash.h>
+
+#include "base/logging.h"
+#include "crypto/nss_util.h"
+#include "third_party/WebKit/public/platform/WebArrayBuffer.h"
+#include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
+
+namespace content {
+
+void WebCryptoImpl::digest(
+ const WebKit::WebCryptoAlgorithm& algorithm,
+ const unsigned char* data,
+ size_t data_size,
+ WebKit::WebCryptoResult result) {
+ HASH_HashType hash_type = HASH_AlgNULL;
+
+ switch (algorithm.id()) {
+ case WebKit::WebCryptoAlgorithmIdSha1:
+ hash_type = HASH_AlgSHA1;
+ break;
+ case WebKit::WebCryptoAlgorithmIdSha224:
+ hash_type = HASH_AlgSHA224;
+ break;
+ case WebKit::WebCryptoAlgorithmIdSha256:
+ hash_type = HASH_AlgSHA256;
+ break;
+ case WebKit::WebCryptoAlgorithmIdSha384:
+ hash_type = HASH_AlgSHA384;
+ break;
+ case WebKit::WebCryptoAlgorithmIdSha512:
+ hash_type = HASH_AlgSHA512;
+ break;
+ default:
+ // Not a digest algorithm.
+ result.completeWithError();
+ return;
+ }
+
+ crypto::EnsureNSSInit();
+
+ HASHContext* context = HASH_Create(hash_type);
+ if (!context) {
+ result.completeWithError();
+ return;
+ }
+
+ HASH_Begin(context);
+
+ HASH_Update(context, data, data_size);
+
+ size_t hash_result_length = HASH_ResultLenContext(context);
+ DCHECK(hash_result_length <= HASH_LENGTH_MAX);
jamesr 2013/08/23 01:27:40 DCHECK_LE()
Bryan Eyler 2013/08/23 17:45:31 Done.
+
+ WebKit::WebArrayBuffer buffer(
+ WebKit::WebArrayBuffer::create(hash_result_length, 1));
+
+ unsigned char* digest = reinterpret_cast<unsigned char*>(buffer.data());
+
+ uint32 result_length = 0;
+ HASH_End(context, digest, &result_length, hash_result_length);
+ if (result_length != hash_result_length) {
+ result.completeWithError();
+ } else {
+ result.completeWithBuffer(buffer);
+ }
+
+ HASH_Destroy(context);
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698