Index: content/renderer/webcrypto_digest_nss.cc |
diff --git a/content/renderer/webcrypto_digest_nss.cc b/content/renderer/webcrypto_digest_nss.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..64e20dd94ace1f431d395fe402db8075bc8a6db5 |
--- /dev/null |
+++ b/content/renderer/webcrypto_digest_nss.cc |
@@ -0,0 +1,105 @@ |
+// 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_digest.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 { |
+ |
+WebCryptoDigest::WebCryptoDigest( |
+ const WebKit::WebCryptoOperationResult& result) |
+ : result_(result), |
+ context_(NULL), |
+ hash_result_length_(0) { |
+} |
+ |
+WebCryptoDigest::~WebCryptoDigest() { |
+ if (context_) { |
+ HASH_Destroy(context_); |
+ } |
+} |
+ |
+bool WebCryptoDigest::Initialize( |
+ const WebKit::WebCryptoAlgorithm& algorithm) { |
+ crypto::EnsureNSSInit(); |
+ |
+ HASH_HashType hash_type = HASH_AlgNULL; |
+ |
+ switch (algorithm.id()) { |
+ case WebKit::WebCryptoAlgorithmIdSha1: |
+ hash_type = HASH_AlgSHA1; |
+ hash_result_length_ = SHA1_LENGTH; |
+ break; |
+ case WebKit::WebCryptoAlgorithmIdSha224: |
+ hash_type = HASH_AlgSHA224; |
+ hash_result_length_ = SHA224_LENGTH; |
+ break; |
+ case WebKit::WebCryptoAlgorithmIdSha256: |
+ hash_type = HASH_AlgSHA256; |
+ hash_result_length_ = SHA256_LENGTH; |
+ break; |
+ case WebKit::WebCryptoAlgorithmIdSha384: |
+ hash_type = HASH_AlgSHA384; |
+ hash_result_length_ = SHA384_LENGTH; |
+ break; |
+ case WebKit::WebCryptoAlgorithmIdSha512: |
+ hash_type = HASH_AlgSHA512; |
+ 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.
|
+ break; |
+ default: |
+ 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.
|
+ } |
+ |
+ context_ = HASH_Create(hash_type); |
+ if (!context_) { |
+ LOG(ERROR) << "Could not create digest context for hash algorithm: " |
+ << hash_type; |
+ return false; |
+ } |
+ |
+ HASH_Begin(context_); |
+ |
+ return true; |
+} |
+ |
+void WebCryptoDigest::process(const unsigned char* bytes, size_t size) { |
+ DCHECK(context_); |
+ HASH_Update(context_, bytes, size); |
+} |
+ |
+void WebCryptoDigest::abort() { |
+ delete this; |
+} |
+ |
+void WebCryptoDigest::finish() { |
+ DCHECK(context_); |
+ |
+ WebKit::WebArrayBuffer buffer( |
+ WebKit::WebArrayBuffer::create(hash_result_length_, 1)); |
+ |
+ unsigned char* digest = reinterpret_cast<unsigned char*>(buffer.data()); |
+ DCHECK(digest); |
+ |
+ unsigned int result_length = 0; |
+ HASH_End(context_, digest, &result_length, hash_result_length_); |
+ if (result_length != hash_result_length_) { |
+ LOG(ERROR) << "Result length invalid; expected " << hash_result_length_ |
+ << ", got " << result_length; |
+ result_.completeWithError(); |
+ delete this; |
+ return; |
+ } |
+ |
+ result_.completeWithArrayBuffer(buffer); |
+ |
+ delete this; |
+} |
+ |
+} // namespace content |