| 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..aeeb27c442d836de8fee072e0b3a23365781d8e3
|
| --- /dev/null
|
| +++ b/content/renderer/webcrypto_digest_nss.cc
|
| @@ -0,0 +1,101 @@
|
| +// 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) {
|
| +}
|
| +
|
| +WebCryptoDigest::~WebCryptoDigest() {
|
| +}
|
| +
|
| +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;
|
| + 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:
|
| + NOTREACHED();
|
| + return false;
|
| + }
|
| +
|
| + context_.reset(HASH_Create(hash_type));
|
| + if (!context_) {
|
| + LOG(ERROR) << "Could not create digest context for hash algorithm: "
|
| + << hash_type;
|
| + return false;
|
| + }
|
| +
|
| + HASH_Begin(context_.get());
|
| +
|
| + return true;
|
| +}
|
| +
|
| +void WebCryptoDigest::process(const unsigned char* bytes, size_t size) {
|
| + DCHECK(context_);
|
| + HASH_Update(context_.get(), bytes, size);
|
| +}
|
| +
|
| +void WebCryptoDigest::abort() {
|
| + delete this;
|
| +}
|
| +
|
| +void WebCryptoDigest::finish() {
|
| + DCHECK(context_);
|
| +
|
| + unsigned int hash_result_length = HASH_ResultLenContext(context_.get());
|
| + 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_.get(), 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;
|
| +}
|
| +
|
| +void WebCryptoDigest::HASHDeleter::operator()(void* ptr) const {
|
| + HASH_Destroy(static_cast<HASHContext*>(ptr));
|
| +}
|
| +
|
| +} // namespace content
|
|
|